這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。
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 也沒有強制指定編譯二元檔案時輸出結果的格式。
翻譯整理:SegmentFault