【发布时间】:2018-09-04 15:56:52
【问题描述】:
我在 macOS 上并尝试使用从 homebrew 安装的 mingw-w64 交叉编译适用于 Windows 的 golang (CGO 1.10) 可执行文件。
我有一个只有一个函数(SimpleQRDecode)的 golang 包,它从我的 c++ 源调用其他函数(FindAndDecodeQR,使用 zxing c++ 端口)。
- 编译 ZXing 源代码没问题 (x86_64-w64-mingw32-g++)
- 编译 qrcode.cpp 没问题 (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 输出包含大量未解析的符号消息,例如
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
【问题讨论】:
标签: c++ macos go cross-compiling cgo