go 1.5 cross compile using cgo on OS X to linux and windows

go 1.5 cross compile using cgo on OS X to linux and windows

将 go 1.4.2 升级到 go 1.5 后,我无法将 OS X 上的 git2go 库编译为 linux amd64。

我认为这是关于交叉编译任何使用 C 代码和 go 1.5 的 go 应用程序。

使用 CGO_ENABLED=1,我得到:

$ CGO_ENABLED=1 GOOS=linux GOARCH=amd64 ./script/with-static.sh go install ./...
# runtime/cgo
ld: unknown option: --build-id=none
clang: error: linker command failed with exit code 1 (use -v to see invocation)

使用-compiler=gccgo,我得到:

$ GOOS=linux GOARCH=amd64 ./script/with-static.sh go install -compiler gccgo ./...
go build github.com/libgit2/git2go: : fork/exec : no such file or directory

如果不提供其中任何一个,我得到:

$ GOOS=linux GOARCH=amd64 ./script/with-static.sh go install ./...
can't load package: package github.com/libgit2/git2go: C source files not allowed when not using cgo or SWIG: wrapper.c

我使用自制软件安装了 go,并且 $GOPATH 指向默认的 ~/go 位置,没什么特别的。

交叉编译时默认不启用cgo。如果你启用 cgo,CGO_ENABLED=1 你将需要有一个用于目标机器的交叉编译 c 编译器。这很重要。

如果你需要cgo,我建议原生编译。

如果您需要 cgo 交叉编译,我会向您指出 xgo,我发现它非常有帮助。它在我 100% 的用例中都不起作用,但是对我的代码进行一些小的更改(与维护本机 VM 进行交叉编译相比)就足够了。