Cross compiling go on Mac OS X

After a lot of fun with this I thought I would write a quick note on setting up your mac for cross compiling with go lang.

I make 2 assumptions here, firstly you use homebrew and secondly you are only writing pure go code (no calling c functions etc). With that in mind the complex process boils down to this:

brew install go --with-cc-all

Yep, that’s it. With that one line you get all the go compilers, if you only want common platforms you can save yourself some space and just run:

brew install go --with-cc-common

Personally I like to couple this with a makefile that allows me to generally build and test on my mac, but when I really want to I can create cross compiled binaries. I like to run a makefile that looks a bit like this:

all: install

build:
    go build ./...

install:
    go install ./...

c-install:
    GOOS=linux GOARCH=amd64 go install
    GOOS=darwin GOARCH=amd64 go install
    GOOS=windows GOARCH=amd64 go install
    GOOS=windows GOARCH=386 go install

And that should be about it to be running a cross compiling go setup!

One thought on “Cross compiling go on Mac OS X

Leave a Reply