Cross-compilation of Go programs
2016-07-28
Instead of just building a program with
$ go build
in the package directory. To cross-compile a pure Go program to run on a Raspberry Pi 2 machine running Linux run
$ GOOS=linux GOARCH=arm GOARM=7 go build
To cross-compile a Go program to run on a Raspberry Pi 2 machine running Linux but for the program involving CGO run
$ GOOS=linux GOARCH=arm GOARM=7 CGO_ENABLED=1 \
CC=arm-linux-gnueabi-gcc go build
For the above command to work you need (on Debian) to install
gcc-arm-linux-gnueabi
.
While to cross-compile from an amd64 machine running Linux to the corresponding 32-bit i386 machine running Linux run
$ GOOS=linux GOARCH=386 go build
for the same cross-compilation but for the program involving CGO run
$ GOOS=linux GOARCH=386 CGO_ENABLED=1 go build
For the above command to work you need (on Debian) to install
gcc-multilib
(or if needed also g++-multilib
).