Go: Cross-Compilation including Cgo
Table of Contents
With the release of Go v1.5, it is possible to cross compile to various targets, such as Windows, Mac, Linux, Solaris, and Android. Please check the following table for a full list at Go version 1.19.3.
Each entry represents a GOOS and GOARCH environment variable combination. The first one for example, aix/ppc64, would be defined as GOOS=aix GOARCH=ppc64 before the actual compile command.
> go tool dist list | column -c 35 | column -t
aix/ppc64 linux/mips64le
android/386 linux/mipsle
android/amd64 linux/ppc64
android/arm linux/ppc64le
android/arm64 linux/riscv64
darwin/amd64 linux/s390x
darwin/arm64 netbsd/386
dragonfly/amd64 netbsd/amd64
freebsd/386 netbsd/arm
freebsd/amd64 netbsd/arm64
freebsd/arm openbsd/386
freebsd/arm64 openbsd/amd64
illumos/amd64 openbsd/arm
ios/amd64 openbsd/arm64
ios/arm64 openbsd/mips64
js/wasm plan9/386
linux/386 plan9/amd64
linux/amd64 plan9/arm
linux/arm solaris/amd64
linux/arm64 windows/386
linux/loong64 windows/amd64
linux/mips windows/arm
linux/mips64 windows/arm64
Cross-compile examples
Windows
# 32 bit
GOOS=windows GOARCH=386 go build
# 64 bit
GOOS=windows GOARCH=amd64 go build
# arm
GOOS=windows GOARCH=arm go build
# arm 64 bit
GOOS=windows GOARCH=arm64 go build
Mac
# 64 bit
GOOS=darwin GOARCH=amd64 go build
# arm 64 bit
GOOS=darwin GOARCH=arm64 go build
Linux
# 32 bit
GOOS=linux GOARCH=386 go build
# 64 bit
GOOS=linux GOARCH=amd64 go build
# arm
GOOS=linux GOARCH=arm go build
# arm 64 bit
GOOS=linux GOARCH=arm64 go build
Cross-compile with Cgo
Some dependencies, such as SQLite, require Cgo instead of just pure Go to compile.
Mac
# 64 bit
GOOS=darwin GOARCH=amd64 CGO_ENABLED=1 go build
# arm 64 bit
GOOS=darwin GOARCH=arm64 CGO_ENABLED=1 go build
Windows
At first, make sure to have Windows compiler setup, such as MinGW.
brew install mingw-w64
Then compile:
Windows
# 32 bit
GOOS=windows GOARCH=386 CGO_ENABLED=1 CC="i686-w64-mingw32-gcc" go build
# 64 bit
GOOS=windows GOARCH=amd64 CGO_ENABLED=1 CC="x86_64-w64-mingw32-gcc" go build
For more information regarding Cgo, please follow up here .
Read other posts