Skip to content

Cross compiling

Matt Holt edited this page Jul 17, 2019 · 2 revisions

Timeliner is written in Go, but depends on sqlite, which uses cgo. This makes cross-compilation tricky, but possible.

I often x-compile from my Mac to my Raspberry Pi, so these are my notes how to do so.

I learned mostly from https://blog.filippo.io/easy-windows-and-linux-cross-compilers-for-macos/.

On Mac:

Install musl for Linux; include support for hardware-float ARM (Raspberry Pi) and 64-bit ARM (why not):

$ brew install FiloSottile/musl-cross/musl-cross --with-arm-hf --with-aarch64

(Wait about 85 minutes!! Better be plugged in!)

  • For Windows support, consider brew install mingw-w64. "The resulting GCC toolchain has prefixes x86_64-w64-mingw32- and i686-w64-mingw32-."

That step is only required once.

To cross-compile, open new Terminal session, or specify full path to the Cross-Compiler/CC variable value, which should be in /usr/local/Cellar/musl-cross/0.9.7_1/bin - or with whatever the version is.

For ARM Linux (Raspberry Pi):

$ CGO_ENABLED=1 CC=arm-linux-musleabihf-gcc GOOS=linux GOARCH=arm go build

For 64-bit Linux:

$ CGO_ENABLED=1 CC=x86_64-linux-musl-gcc GOOS=linux go build

For 64-bit Windows:

$ GOOS=windows CC=x86_64-w64-mingw32-gcc go build

Then, on Raspberry Pi:

I wasn't able to immediately run timeliner because my Pi was missing the musl libraries, I think.

Not sure if necessary, but did fix ldd timeliner:

$ cd /lib/arm-linux-gnueabihf/
$ sudo ln -s libc.so.6 libc.so

This definitely helped:

$ sudo apt-get install musl-dev

(Note that, before this command, /lib/ld-musl* doesn't exist, but /lib/ld-linux* does, so we had to install musl library.)