Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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.

like image 571
Calin Avatar asked Aug 31 '15 10:08

Calin


People also ask

Can I compile a Windows EXE on Linux?

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.

Can go cross-compile?

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.

Does GCC support cross compilation?

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.


2 Answers

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.

like image 82
Dave Cheney Avatar answered Nov 05 '22 15:11

Dave Cheney


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.

like image 36
Astockwell Avatar answered Nov 05 '22 14:11

Astockwell