基本介紹
交叉編譯是為了在不同平台編譯出其他平台的程序,比如在Linux編譯出Windows程序,在Windows能編譯出Linux程序,32位系統下編譯出64位程序,今天介紹的gox就是其中一款交叉編譯工具。
配置環境
首先配置好Go語言的環境變量,並在~/.bash_profile
中設置,簡單說明一下為什么要添加至該文件,首先以下代碼在終端執行完成后只對當前會話有效,關閉終端變量就失效了,而.bash_profile
文件在用戶每次登錄時都會執行一次,把環境變量設置到該文件中,每次登錄都會初始化環境變量。當然,放在~/.bashrc
中也是可以的,它不僅會在登錄時執行,還會在每次打開終端時執行。
export GOPATH=${HOME}/go
export GOROOT=/usr/local/go
export GOBIN=${GOPATH}/bin
export PATH=${PATH}:${GOBIN}
GOROOT與GOPATH要根據自身情況設置,不要盲目跟從,設置完成后若要該文件立即生效,可以執行source
命令。
source ~/.bash_profile
如果你的終端裝了zsh
,可能重新打開終端后依然會失效,那么可以在~/.zshrc
文件的最后一行追加上source
指令。
source ~/.bash_profile
gox的安裝
在終端執行以下指令進行安裝。
go get github.com/mitchellh/gox
安裝結束后,執行gox -h
,如果有展示幫助信息,代表安裝成功。
➜ ~ gox -h
Usage: gox [options] [packages]
Gox cross-compiles Go applications in parallel.
If no specific operating systems or architectures are specified, Gox
will build for all pairs supported by your version of Go.
......
gox的使用
按照慣例,我們先祭出hello,world
的演示代碼。
package main
import "fmt"
func main() {
fmt.Print("hello,world")
}
此時進入項目中的工作目錄($GOPATH/src/[你的項目名]),直接執行gox
命令,會生成多達21個不同平台的可執行文件,橫跨linux、windows、freebsd、darwin等系統。
➜ hello gox
Number of parallel builds: 3
--> linux/amd64: hello
--> openbsd/amd64: hello
--> darwin/386: hello
--> linux/mipsle: hello
--> windows/386: hello
--> windows/amd64: hello
--> darwin/amd64: hello
--> linux/386: hello
--> linux/s390x: hello
--> netbsd/386: hello
--> linux/arm: hello
--> freebsd/386: hello
--> netbsd/amd64: hello
--> freebsd/arm: hello
--> freebsd/amd64: hello
--> openbsd/386: hello
--> linux/mips64: hello
--> linux/mips: hello
--> linux/mips64le: hello
--> netbsd/arm: hello
但我並不想一次生成所有平台的程序,這時就需要gox的參數進行指定,如下所示,os
參數指定要生成的系統名稱,arch
指定CPU的架構。
gox -os "windows" -arch amd64
其實它所支持的並不止21款,這些只是默認生成的,下面是gox對各種系統的定義,感興趣的同學可以自行了解。
Platforms_1_0 = []Platform{
{"darwin", "386", true},
{"darwin", "amd64", true},
{"linux", "386", true},
{"linux", "amd64", true},
{"linux", "arm", true},
{"freebsd", "386", true},
{"freebsd", "amd64", true},
{"openbsd", "386", true},
{"openbsd", "amd64", true},
{"windows", "386", true},
{"windows", "amd64", true},
}
Platforms_1_1 = append(Platforms_1_0, []Platform{
{"freebsd", "arm", true},
{"netbsd", "386", true},
{"netbsd", "amd64", true},
{"netbsd", "arm", true},
{"plan9", "386", false},
}...)
Platforms_1_3 = append(Platforms_1_1, []Platform{
{"dragonfly", "386", false},
{"dragonfly", "amd64", false},
{"nacl", "amd64", false},
{"nacl", "amd64p32", false},
{"nacl", "arm", false},
{"solaris", "amd64", false},
}...)
Platforms_1_4 = append(Platforms_1_3, []Platform{
{"android", "arm", false},
{"plan9", "amd64", false},
}...)
Platforms_1_5 = append(Platforms_1_4, []Platform{
{"darwin", "arm", false},
{"darwin", "arm64", false},
{"linux", "arm64", false},
{"linux", "ppc64", false},
{"linux", "ppc64le", false},
}...)
Platforms_1_6 = append(Platforms_1_5, []Platform{
{"android", "386", false},
{"linux", "mips64", false},
{"linux", "mips64le", false},
}...)
Platforms_1_7 = append(Platforms_1_5, []Platform{
// While not fully supported s390x is generally useful
{"linux", "s390x", true},
{"plan9", "arm", false},
// Add the 1.6 Platforms, but reflect full support for mips64 and mips64le
{"android", "386", false},
{"linux", "mips64", true},
{"linux", "mips64le", true},
}...)
Platforms_1_8 = append(Platforms_1_7, []Platform{
{"linux", "mips", true},
{"linux", "mipsle", true},
}...)
除了剛才的命令外還有另一種生成方式,用斜杠的方式將系統與架構合並批量生成。
gox -osarch "windows/amd64 linux/amd64"
趕緊把你生成的程序發給小伙伴執行試試吧,以上就是本文全部內容,感謝閱讀。