diff --git a/Software/Development/Language/Go/Basic/Go_交叉编译.md b/Software/Development/Language/Go/Basic/Go_交叉编译.md new file mode 100644 index 0000000..5600e76 --- /dev/null +++ b/Software/Development/Language/Go/Basic/Go_交叉编译.md @@ -0,0 +1,51 @@ +# Go 交叉编译 + +Golang 的交叉编译是通过设置编译宏变量来实现的,主要是 GOOS 和 GOARCH,列表如下: + +| GOOS - Target Operating System | GOARCH - Target Platform | +|--------------------------------|--------------------------| +| android | arm | +| darwin | 386 | +| darwin | amd64 | +| darwin | arm | +| darwin | arm64 | +| dragonfly | amd64 | +| freebsd | 386 | +| freebsd | amd64 | +| freebsd | arm | +| linux | 386 | +| linux | amd64 | +| linux | arm | +| linux | arm64 | +| linux | ppc64 | +| linux | ppc64le | +| linux | mips | +| linux | mipsle | +| linux | mips64 | +| linux | mips64le | +| netbsd | netbsd | +| netbsd | amd64 | +| netbsd | arm | +| openbsd | 386 | +| openbsd | amd64 | +| openbsd | arm | +| plan9 | 386 | +| plan9 | 386 | +| solaris | amd64 | +| windows | 386 | +| windows | amd64 | + +使用示例如下: + +```bash +GOOS=linux GOARCH=arm64 go build +``` + +交叉编译是不支持 CGO 的,也就是说如果你的代码中存在 C 代码,是编译不了的。,比如说你的程序中使用了 sqlite 数据库,在编译 go-sqlite 驱动时按照上面的做法是编译不通过的。 + +需要 CGO 支持的,要将 CGO_ENABLED 的 0 改为 1,也就是“CGO_ENABLED=1”,此外还需要设置编译器,例如我想在 linux 上编译 arm 版的二进制,需要这样做: + +```bash +# Build for arm +CGO_ENABLED=1 GOOS=linux GOARCH=arm CC=arm-linux-gnueabi-gcc go build +```