Go語言交叉編譯工具gox

基本介紹

交叉編譯是爲了在不一樣平臺編譯出其餘平臺的程序,好比在Linux編譯出Windows程序,在Windows能編譯出Linux程序,32位系統下編譯出64位程序,今天介紹的gox就是其中一款交叉編譯工具。linux

配置環境

首先配置好Go語言的環境變量,並在~/.bash_profile中設置,簡單說明一下爲何要添加至該文件,首先如下代碼在終端執行完成後只對當前會話有效,關閉終端變量就失效了,而.bash_profile文件在用戶每次登陸時都會執行一次,把環境變量設置到該文件中,每次登陸都會初始化環境變量。固然,放在~/.bashrc中也是能夠的,它不只會在登陸時執行,還會在每次打開終端時執行。android

export GOPATH=${HOME}/go
export GOROOT=/usr/local/go
export GOBIN=${GOPATH}/bin
export PATH=${PATH}:${GOBIN}

GOROOT與GOPATH要根據自身狀況設置,不要盲目跟從,設置完成後若要該文件當即生效,能夠執行source命令。git

source ~/.bash_profile

若是你的終端裝了zsh,可能從新打開終端後依然會失效,那麼能夠在~/.zshrc文件的最後一行追加上source指令。github

source ~/.bash_profile

gox的安裝

在終端執行如下指令進行安裝。windows

go get github.com/mitchellh/gox

安裝結束後,執行gox -h,若是有展現幫助信息,表明安裝成功。bash

➜  ~ 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等系統。app

➜  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對各類系統的定義,感興趣的同窗能夠自行了解。ui

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"

趕忙把你生成的程序發給小夥伴執行試試吧,以上就是本文所有內容,感謝閱讀。

相關文章
相關標籤/搜索