我在macOS上,尝试使用从自制软件安装的mingw-w64交叉编译Windows的golang(CGO 1.10)可执行文件。

我有只带有一个函数(SimpleQRDecode)的golang软件包,该函数从我的c ++源代码(FindAndDecodeQR,使用zxing c ++端口)调用其他函数。

  • 编译ZXing源代码很好(x86_64-w64-mingw32-g ++)
  • 成功将所有对象(zxing + my)合并到静态库(x86_64-w64-mingw32-ar)
  • 在静态库(x86_64-w64-mingw32-ranlib)上成功调用了ranlib
  • 未能呼叫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输出包含大量未解析的符号消息,例如

    1
    2
    3
    4
    5
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    #ifdef __cplusplus
    extern"C" {
    #endif

    const char* FindAndDecodeQR(char *raster, int size, int width, int height);


    #ifdef __cplusplus
    }
    #endif
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20

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

    go代码如下所示(未经测试):

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24

    有关syscall软件包的更多信息,请参见。

    • 谢谢您的建议,但我正在寻找构建一个可靠的二进制文件的方法。