Where developers come to connect, share, build and be inspired.

10

Cross-Compiling Golang ...

13680 views


Compiling your go code into a binary is pretty simple:

 $ go install <package>

This will build the binary for your current OS and architecture. But compiling binaries for other operating systems and even architectures is also possible. You just need to modify a few environment variables:

  • GOARCH - the architecture, e.g. amd64, 386 or arm
  • GOOS - the operating system - linux, darwin or windows

Building the compiler first ...

You can take a look at the output of go env to see what your current configuration looks like. But to actually cross compile, you need to first build the compilers.

With Mac OS / Homebrew, just install go with --cross-compile-common or --cross-compile-all. See the recipe for details.

If you have installed Go from source, just set the variables and call ./all.bash in the src folder for each GOARCH/GOOS combination you need.

This guide might help if you are totally lost.

Building your project ...

Back to our project. Similar to building the compiler you just need to modify the environment variables and call go build as before. Go will now create a subfolder in ./bin/ matching your os/arch combination. Here you will find your crosscompiled binary. Obvisouly you can also pass in the varis in one line:

$ GOARCH=arm GOOS=linux go build helloworld

PS: If you want to crosscompile for ARM v5/v6 CPU (e.g. for the raspberry pi), you need to set GOARM=5 to use different set of operations for floating point math.

Comments

  • User-avatar

    Oddur: I've created an issue for your comment here: https://github.com/mxcl/homebrew/issues/22638 Let's see what they say!

  • 0_ndxxktyul9ry5d8-42muki0sf-pkbm8-qhwukiy64ah8zobtvf7ho_ohdsy7xwtobogvxkuumoud

    One thing to note is that homebrew removes the /include folder from the go installation which makes the go dist tool fail.

    so apparently you have to install from source.

Add a comment