Skip to main content

06 Golang多平台交叉编译和Makefile配置

查看当前Go所支持的平台和相关指令cpu

$ go tool dist list
aix/ppc64
android/386
android/amd64
android/arm
android/arm64
darwin/amd64
darwin/arm64
dragonfly/amd64
freebsd/386
freebsd/amd64
freebsd/arm
freebsd/arm64
illumos/amd64
ios/amd64
ios/arm64
js/wasm
linux/386
linux/amd64
linux/arm
linux/arm64
linux/mips
linux/mips64
linux/mips64le
linux/mipsle
linux/ppc64
linux/ppc64le
linux/riscv64
linux/s390x
netbsd/386
netbsd/amd64
netbsd/arm
netbsd/arm64
openbsd/386
openbsd/amd64
openbsd/arm
openbsd/arm64
openbsd/mips64
plan9/386
plan9/amd64
plan9/arm
solaris/amd64
windows/386
windows/amd64
windows/arm
windows/arm64

以上就是go支持的平台系统和对应cpu类型

2 Makefile示例

通常编译为了方便会相关的配置写在一个Makefile文件中,再由make工具来读取这一配置来进行配置,编译和安装等工作,以下就是编译的示例:

VERSION = 0.0.1
DIST_DIR = dist/${VERSION}/notificationTool

build:
# Compiled for Linux OS platform.
GOOS=linux GOARCH=386 go build -o ${DIST_DIR}-linux-386

GOOS=linux GOARCH=arm GOARM=5 go build -o ${DIST_DIR}-linux-arm32

# Compiled for mac OS platform.
GOOS=darwin GOARCH=amd64 go build -o ${DIST_DIR}-darwin-amd64

# Compiled for window OS platform.
GOOS=windows GOARCH=amd64 go build -o ${DIST_DIR}-windows-amd64.exe

参考资料