apt-get install gcc-mingw-w64 env CGO_ENABLED=1 GOOS=windows GOARCH=amd64 CC=x86_64-w64-mingw32-gcc go build -o ./app/app.exe ./app/main.go env CGO_ENABLED=1 GOOS=windows GOARCH=386 CC=i686-w64-mingw32-gcc go build -o ./app/app.exe ./app/main.go
Gox 是一個簡單的,不花俏的Go平台交叉編譯工具,它的用處就和標准的 go build
一樣。Gox 會並行地為多種平台編譯。Gox 同時也提供了一套交叉編譯工具鏈。
Gox 項目地址:https://github.com/mitchellh/gox
安裝
為了安裝 Gox,請使用 go get
。我們已經為版本打上了標簽,所以可以隨便切換標簽進行編譯:
$ go get github.com/mitchellh/gox
...
$ gox -h
...
用法
在你使用 Gox 之前,你必須先有一套交叉編譯工具鏈。Gox 可以自動幫你完成這個。你需要做的只是運行(每次更新 Go 都要這樣做這步):
$ gox -build-toolchain ...
當你完成這個,你可以已經准備好進行交叉編譯了。
如果你知道怎么去使用 go build
, 那么你也知道怎么去使用 Gox 了。例如,編譯當前的項目,無需提供參數,只需要調用 gox
。Gox 就會根據 CPU 的數量並行地為各個平台編譯:
$ gox Number of parallel builds: 4
--> darwin/386: github.com/mitchellh/gox
--> darwin/amd64: github.com/mitchellh/gox
--> linux/386: github.com/mitchellh/gox
--> linux/amd64: github.com/mitchellh/gox
--> linux/arm: github.com/mitchellh/gox
--> freebsd/386: github.com/mitchellh/gox
--> freebsd/amd64: github.com/mitchellh/gox
--> openbsd/386: github.com/mitchellh/gox
--> openbsd/amd64: github.com/mitchellh/gox
--> windows/386: github.com/mitchellh/gox
--> windows/amd64: github.com/mitchellh/gox
--> freebsd/arm: github.com/mitchellh/gox
--> netbsd/386: github.com/mitchellh/gox
--> netbsd/amd64: github.com/mitchellh/gox
--> netbsd/arm: github.com/mitchellh/gox
--> plan9/386: github.com/mitchellh/gox
或者,你只想編譯某個項目和子項目:
$ gox ./... ...
或者,你想僅僅為 linux 編譯:
$ gox -os="linux" ...
或者,你僅僅只想為 64 位的 linux 編譯:
$ gox -osarch="linux/amd64" ...
還有更多的選項,可以通過 gox -h
查看幫助。
和其他交叉編譯工具的比較
非常感謝這些工具為我們提供了更多的選擇,它們為 go 平台的交叉編譯工具提供做了很多方面的貢獻.
- Dave Cheney的交叉編譯器: Gox 可以為多種平台編譯,所以也能容易地運行在各種 Go 支持的平台上。但Dave的那個需要一個 shell 來運行。Gox 支持並行地編譯,但 Dave 的只是按順序地編譯。Gox 也能非常方便地使用的內置的 arch 系統的內置過濾工具。
- goxc:它是一個功能豐富的工具,能編譯系統項目,上傳二進制文件,產生下載頁面等;相較之下,Gox 在交叉編譯二元文件方面稍稍弱些。但 Gox 能並行地編譯項目,而 goxc 不能。Gox 也沒有強制指定編譯二元文件時輸出結果的格式。
參考資料:
ubuntu 下 golang 跨平台編譯 windows 程序: http://studygolang.com/articles/8167
跨平台編譯Go程序(交叉編譯): http://studygolang.com/topics/21
golang交叉編譯: http://blog.csdn.net/coroutines/article/details/39546491
Gox : 簡單實在的Go平台交叉編譯工具: https://segmentfault.com/a/1190000000346086