How can I cross-compile Golang software using Zig?

When trying to cross-compile applications for different operating systems and architectures, you can often hit some niche configurations that are difficult to accomodate.

Thankfully Zig can help to alleviate this pain point as it bundles standard libraries for all major platforms ie; GNU libc, musl libc and so on.

Assuming you have Zig already installed, you can cross compile a Go application like so:

$ CGO_ENABLED=1 GOOS=linux GOARCH=amd64 CC="zig cc -target x86_64-linux" CXX="zig c++ -target x86_64-linux" go build --tags extended

🏏 There are some custom names at play here

You should note that Zig uses the term amd64 rather than x86_64.

While the list of supported targets is very long, here are some most common combinations:

  • x86_64-linux-gnu
  • x86_64-windows-gnu
  • x86_64-macos-musl
  • aarch64-macos-gnu

As you can tell, the format followed is <arch>-<os>-<libc>