samedi, février 07, 2015

Cross compiling Go app



Cross compiling Go app


First build cross compiling toolchain


cd go/src
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 ./make.bash  --no-clean
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 ./make.bash --no-clean

Second build your file


GOOS=windows GOARCH=386 go build -o appname.exe appname.go
GOOS=linux GOARCH=386 CGO_ENABLED=0 go build -o appname.linux appname.go


CGO_ENABLED=0 will deactivate use of CGo. So it will be a portable app. But if your app use CGo (for example by using go package "net"), it will not build.


Option : use gonative


If you want to use CGo and do cross compiling, use gonative which download each binary distribution for each platform and build cross compiling toolchain with theses binaries, so you can use CGo.

https://github.com/inconshreveable/gonative
https://inconshreveable.com/04-30-2014/cross-compiling-golang-programs-with-native-libraries/

And then use gox https://github.com/mitchellh/gox
to cross compile your application


Some links

http://www.goinggo.net/2013/10/cross-compile-your-go-programs.html
http://solovyov.net/en/2012/cross-compiling-go/
http://stackoverflow.com/questions/12168873/cross-compile-go-on-osx

Aucun commentaire: