英文:
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'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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论