Cross compiling golang with cgo for Windows target on macOS host

https://stackoverflow.com/questions/49492589/cross-compiling-golang-with-cgo-for-windows-target-on-macos-host

I am on macOS and trying to cross compile golang (CGO 1.10) executable for Windows using mingw-w64 installed from homebrew.

I have golang package with only one function (SimpleQRDecode) that calls other function from my c++ source (FindAndDecodeQR, uses zxing c++ port).

  1. Compiling ZXing sources is fine (x86_64-w64-mingw32-g++)
  2. Compiling qrcode.cpp is fine (x86_64-w64-mingw32-g++)
  3. Successfully merged all objects (zxing + my) into static library (x86_64-w64-mingw32-ar)
  4. Successfully called ranlib on static library (x86_64-w64-mingw32-ranlib)
  5. FAILED to call 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 output consists of tons of unresolved symbol messages like

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

qrcode.go:

  1. package qrcode
  2. /*
  3. #cgo CFLAGS: -I../../native/prefix/include -I../../libmyqr -fPIC
  4. #cgo CXXFLAGS: -I../../native/prefix/include -I../../libmyqr -fPIC
  5. #cgo LDFLAGS: -L../../libmyqr -lmyqr -lstdc++
  6. #include <stdlib.h>
  7. #include <qrcode.h>
  8. */
  9. import "C"
  10. import (
  11. "errors"
  12. "fmt"
  13. "unsafe"
  14. )
  15. func SimpleQRDecode(rasterData []byte, width, height int) (string, bool, error) {
  16. res := C.FindAndDecodeQR((*C.char)(unsafe.Pointer(&data[0])), C.int(len(data)), C.int(width), C.int(height))

qrcode.h:

  1. #ifdef __cplusplus
  2. extern "C" {
  3. #endif
  4. const char* FindAndDecodeQR(char *raster, int size, int width, int height);
  5. #ifdef __cplusplus
  6. }
  7. #endif

qrcode.cpp:

  1. #include "qrcode.h"
  2. #include <sstream>
  3. #include <zxing/ZXing.h>
  4. #include <zxing/Binarizer.h>
  5. #include <zxing/BinaryBitmap.h>
  6. #include <zxing/Result.h>
  7. #include <zxing/qrcode/QRCodeReader.h>
  8. #include <zxing/common/GreyscaleRotatedLuminanceSource.h>
  9. #include <zxing/common/GlobalHistogramBinarizer.h>
  10. const char* FindAndDecodeQR(char *raster, int size, int width, int height) {
  11. // GreyscaleLuminanceSource
  12. std::ostringstream resStream;
  13. try {
  14. zxing::ArrayRef<char> image(raster, size);
  15. zxing::Ref<zxing::GreyscaleRotatedLuminanceSource> source(new zxing::GreyscaleRotatedLuminanceSource(image, width, height, 0, 0, width, height));

libmyqr.a - static library made of all zxing compiled objects + qrcode.cpp.o


It is probably not what you want to hear but the easiest way to cross compile to windows on Mac is to build a DLL library from your c/c++ code and handle this DLL library from your go code. In this situation you even don’t need CGO_ENABLED=1.

The go code can look like this (not tested):

  1. // +build windows
  2. package qrcode
  3. import (
  4. "errors"
  5. "fmt"
  6. "syscalls"
  7. "unsafe"
  8. )
  9. var modYourPkg = syscall.NewLazyDLL("yourpkg.dll")
  10. var procFindAndDecodeQR = modYourPkg.NewProc("FindAndDecodeQR")
  11. func SimpleQRDecode(rasterData []byte, width, height int) (string, bool, error) {
  12. res, _, _ := procFindAndDecodeQR.Call(
  13. uintptr(unsafe.Pointer(&rasterData[0])),
  14. uintptr(len(data)),
  15. uintptr(width),
  16. uintptr(height),
  17. )
  18. ...
  19. }

See more in documentation to the syscall package.


Thank you for suggestion, but I am looking for the way to build one solid binary. - The Architect

ft_authoradmin  ft_create_time2019-11-18 12:24
 ft_update_time2019-11-18 12:24