[參數]
GOOS:作業系統(android、ios、linux、windows...) GOARCH:作業系統架構(arm、amd64...) CGO_ENABLED:是否啟用CGO (不能啟用CGO,故設為0)
例1:windows 上編譯出 Linux 執行檔(僅用SET,沒用-w設定的環境變數,僅在此次開啟的termial有效)
SET CGO_ENABLED=0 SET GOOS=linux SET GOARCH=amd64 go build main.go
例2:Linux 上編譯出 windows 執行檔
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build main.go
[查詢支援的平台]
>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
參考:
- https://pkg.go.dev/cmd/dist
dist · pkg.go.dev - https://mileslin.github.io/2020/01/Golang/Go-%E7%94%A2%E7%94%9F%E4%B8%8D%E5%90%8C%E5%B9%B3%E5%8F%B0%E7%9A%84%E5%9F%B7%E8%A1%8C%E6%AA%94/
[Go] Go 產生不同平台的執行檔 - Miles's Journey - https://segmentfault.com/a/1190000013989448
Gin实践 番外 Golang交叉编译 - SegmentFault 思否 - https://ithelp.ithome.com.tw/articles/10225188
Go 交叉編譯 與 Docker <3 - iT 邦幫忙::一起幫忙解決難題,拯救 IT 人的一天 - https://johng.cn/cgo-enabled-affect-go-static-compile/
CGO_ENABLED环境变量对Go静态编译机制的影响 - 碎言碎语 - https://chai2010.cn/advanced-go-programming-book/ch2-cgo/ch2-01-hello-cgo.html
2.1 快速入门 · Go语言高级编程 - CGO编程 - https://wizardforcel.gitbooks.io/gopl-zh/content/ch13/ch13-04.html
通過cgo調用C代碼 | Go 语言圣经 中文版
沒有留言:
張貼留言