简体   繁体   English

使用OS X上的cgo到linux和windows进行1.5交叉编译

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

I'm having trouble compiling the git2go library on OS X to linux amd64 after upgrading go 1.4.2 to go 1.5. 在将1.4.2升级到1.5之后,我无法将OS X上的git2go库编译为linux amd64。

I think this is about cross compiling any go app that uses C code with go 1.5. 我认为这是关于交叉编译任何使用C代码和go 1.5的应用程序。

Using CGO_ENABLED=1 , I get: 使用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)

Using -compiler=gccgo , I get: 使用-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

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. 我用自制软件安装了,我有$GOPATH指向默认的~/go位置,没什么特别的。

cgo is not enabled by default when cross compiling. 交叉编译时,默认情况下不启用cgo。 If you enable cgo, with CGO_ENABLED=1 you will need to have a cross compiling c compiler for the target machine. 如果启用cgo,CGO_ENABLED = 1,则需要为目标机器交叉编译c编译器。 This is non trivial. 这不是一件轻而易举的事。

I recommend, if you need cgo, to compile natively. 如果你需要cgo,我建议你本地编译。

If you need cgo cross compilation, I'd point you to xgo , which I found immensely helpful. 如果您需要cgo cross编译,我会指向xgo ,我发现非常有帮助。 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. 它在100%的用例中都不起作用,但是对于我的代码有一些小的(与维护本机VM进行交叉编译相比),这已经足够了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM