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.
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.
You can compile the code on windows and but can't execute on linux as the compiler usually generates executables and libraries for the operating system it runs.
This is called cross-compiling, and it's easy to do with Go. Programs written in Go can easily be compiled for a wide variety of target operating systems such as Windows, macOS, and Linux by using the GOARCH and GOOS environmental variables.
GCC, a free software collection of compilers, can be set up to cross compile. It supports many platforms and languages. Cross-compiling GCC requires that a portion of the target platform's C standard library be available on the host platform.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With