使用cgo在OS X上进行go 1.5的交叉编译到Linux和Windows。

huangapple go评论84阅读模式
英文:

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

问题

我在将git2go库从OS X编译到Linux amd64时遇到了问题,这是在将go 1.4.2升级到go 1.5之后的情况。

我认为这与使用go 1.5编译任何使用C代码的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

我使用homebrew安装了go,并且将$GOPATH指向默认的~/go位置,没有其他特殊设置。

英文:

I'm having trouble compiling the git2go library on OS X to linux amd64 after upgrading go 1.4.2 to go 1.5.

I think this is about cross compiling any go app that uses C code with go 1.5.

<!-- language-all: sh -->

Using CGO_ENABLED=1, I get:

$ 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)

Using -compiler=gccgo, I get:

$ 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

If not supplying any of those, I get:

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

I installed go using homebrew, and I have the $GOPATH pointing to the default ~/go location, nothing fancy.

答案1

得分: 12

cgo在交叉编译时默认是禁用的。如果你启用了cgo,使用CGO_ENABLED=1,你需要为目标机器准备一个交叉编译的C编译器。这并不是一件简单的事情。

我建议,如果你需要cgo,最好进行本地编译。

英文:

cgo is not enabled by default when cross compiling. If you enable cgo, with CGO_ENABLED=1 you will need to have a cross compiling c compiler for the target machine. This is non trivial.

I recommend, if you need cgo, to compile natively.

答案2

得分: 1

如果你需要进行cgo交叉编译,我会推荐你使用xgo,我发现它非常有帮助。虽然它在我所有的使用情况下并不都能百分之百地工作,但只需对我的代码进行一些小的修改(相比于维护本地虚拟机进行交叉编译),就足够了。

英文:

If you need cgo cross compilation, I'd point you to xgo, which I found immensely helpful. It didn't work in 100% of my use cases, but with some minor (compared to maintaining native VMs for cross-compiling) changes to my code, it was sufficient.

huangapple
  • 本文由 发表于 2015年8月31日 18:20:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/32309030.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定