用MacOS主机上的Windows目标交叉编译Golang



我在MacOS上,尝试使用来自Homebrew安装的MINGW-W64的Windows跨编译Golang(CGO 1.10(。

我的Golang软件包只有一个功能(SimpleQrdecode(,该功能从我的C 源调用其他函数(FindandDecodeqr,使用ZXing C 端口(。

  1. 编译Zxing源是可以的(X86_64-W64-MINGW32-G (
  2. 编译QRCODE.CPP是良好的(x86_64-w64-mingw32-g (
  3. 成功将所有对象(Zxing MY(合并到静态库(x86_64-w64-mingw32-ar(
  4. 在静态库(x86_64-w64-mingw32-ranlib(上成功称为ranlib
  5. 失败致电CGO_ENABLED=1 CXX="x86_64-w64-mingw32-g++" CXX_FOR_TARGET="x86_64-w64-mingw32-g++" CC="x86_64-w64-mingw32-gcc" CC_FOR_TARGET="x86_64-w64-mingw32-gcc" GOOS=windows GOARCH=amd64 go build -x

go build输出由大量未解决的符号消息组成,例如

libmyqr.a(qrcode.o):qrcode.cpp:(.text+0x79): undefined reference to `operator new(unsigned long long)'
libmyqr.a(qrcode.o):qrcode.cpp:(.text+0x2cb): undefined reference to `std::string::length() const'
libmyqr.a(qrcode.o):qrcode.cpp:(.text+0x4a7): undefined reference to `__cxa_begin_catch'
libmyqr.a(BinaryBitmap.cpp.obj):BinaryBitmap.cpp:(.text+0x5c2): undefined reference to `std::ios_base::Init::~Init()'
libmyqr.a(BinaryBitmap.cpp.obj):BinaryBitmap.cpp:(.xdata+0xc): undefined reference to `__gxx_personality_seh0'

qrcode.go:

package qrcode
/*
#cgo CFLAGS: -I../../native/prefix/include -I../../libmyqr -fPIC
#cgo CXXFLAGS: -I../../native/prefix/include -I../../libmyqr -fPIC
#cgo LDFLAGS: -L../../libmyqr -lmyqr -lstdc++
#include <stdlib.h>
#include <qrcode.h>
*/
import "C"
import (
    "errors"
    "fmt"
    "unsafe"
)
func SimpleQRDecode(rasterData []byte, width, height int) (string, bool, error) {
    res := C.FindAndDecodeQR((*C.char)(unsafe.Pointer(&data[0])), C.int(len(data)), C.int(width), C.int(height))

qrcode.h:

#ifdef __cplusplus
extern "C" {
#endif
const char* FindAndDecodeQR(char *raster, int size, int width, int height);

#ifdef __cplusplus
}
#endif

qrcode.cpp:

#include "qrcode.h"
#include <sstream>
#include <zxing/ZXing.h>
#include <zxing/Binarizer.h>
#include <zxing/BinaryBitmap.h>
#include <zxing/Result.h>
#include <zxing/qrcode/QRCodeReader.h>
#include <zxing/common/GreyscaleRotatedLuminanceSource.h>
#include <zxing/common/GlobalHistogramBinarizer.h>
const char* FindAndDecodeQR(char *raster, int size, int width, int height) {
    // GreyscaleLuminanceSource
    std::ostringstream resStream;
    try {
        zxing::ArrayRef<char> image(raster, size);
        zxing::Ref<zxing::GreyscaleRotatedLuminanceSource> source(new zxing::GreyscaleRotatedLuminanceSource(image, width, height, 0, 0, width, height));

libmyqr.a-由所有ZXing编译的对象 qrcode.cpp.o

制成的静态库

它可能不是您想听的,但是交叉编译到Mac上的最简单方法是从c/c++代码构建DLL库,并从go代码处理此DLL库。在这种情况下,您甚至不需要CGO_ENABLED=1

go代码看起来像这样(未测试(:

// +build windows
package qrcode
import (
    "errors"
    "fmt"
    "syscalls"
    "unsafe"
)
var modYourPkg = syscall.NewLazyDLL("yourpkg.dll")
var procFindAndDecodeQR = modYourPkg.NewProc("FindAndDecodeQR")
func SimpleQRDecode(rasterData []byte, width, height int) (string, bool, error) {
    res, _, _ := procFindAndDecodeQR.Call(
        uintptr(unsafe.Pointer(&rasterData[0])),
        uintptr(len(data)),
        uintptr(width),
        uintptr(height),
    )
    ...
}

请参阅SYSCALL软件包的文档中的更多信息。

最新更新