Last Updated: February 25, 2016
·
2.979K
· pcrawfor

Go Cross Compilation on Mavericks

(Updated March 2014 for go 1.2.1)

I just updated to Mavericks and my go cross compilation environment stopped working correctly.

With Mavericks Apple has removed gcc from the command line developer tools (it is now clang just symlinked to gcc).

In order to get the go 1.2.1 source to compile I had to install gcc on my Mavericks machine, since it was failing with clang. I have seen some discussion in the go mailing list about this but as it stands it fails to build.

Here's what I did to get it working

First I installed gcc via homebrew:

# installing gcc on mavericks using brew
brew tap homebrew/dupes
brew install apple-gcc42

Then I symlinked the newly installed apple-gcc42 into my /usr/bin folder

cd /usr/bin
sudo mv gcc gcc_mavs
sudo ln -s /usr/local/Cellar/apple-gcc42/4.2.1-5666.3/bin/gcc-4.2 gcc

Now running 'gcc' should show you:

i686-apple-darwin11-gcc-4.2.1: no input files

gcc is now good and you can compile go from sources.

For fun I'll run through the rest of my cross compilation setup.

Setting up the Cross compilation environment

First download the latest go source ( at this time Mar 2014 that is version 1.2.1)

curl -O https://go.googlecode.com/files/go1.2.1.src.tar.gz

Untar it

tar -zxvf go1.2.1.src.tar.gz

Then cd into the go directory and compile it

cd go/src

./all.bash

Once go has finished compiling you can setup the cross compilation by building the different toolchains for the platforms you want to support.

I personally like to us goxc to do this.

First install goxc:

go get github.com/laher/goxc

Then run the toolchain build:

goxc -t

When this completes you'll have all the platform toolchains built and available to use on your system.

In order to use them you can use goxc or I prefer to just run build commands directly or via scripts.

You're ready to do cross platform builds :)

For instance to build a project for linux 64 bit:

GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -o binary_name source_file.go

2 Responses
Add your response

How are people deploying to production? Go is owned by root and then packages are owned by a normal user? Or maybe everything is owned by an app user?

I mean, sudo goxc -t is going to cause all kind of chaos.

over 1 year ago ·

In production we deploy binaries only - building the toolchains is only on dev machines and allows us to cross compile for linux targets on our servers

over 1 year ago ·