Golang交叉编译工具Gox的安装使用

Gox简介

gox是一个简单易用的交叉编译工具,当然它只能用于 Golang.

Gox安装

1
go get github.com/mitchellh/gox

Gox使用

可以使用 -h 调出帮助

1
gox -h

如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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.

Options:

-arch="" Space-separated list of architectures to build for
-build-toolchain Build cross-compilation toolchain
-ldflags="" Additional '-ldflags' value to pass to go build
-tags="" Additional '-tags' value to pass to go build
-os="" Space-separated list of operating systems to build for
-osarch="" Space-separated list of os/arch pairs to build for
-output="foo" Output path template. See below for more info
-parallel=-1 Amount of parallelism, defaults to number of CPUs
-verbose Verbose mode

Output path template:

The output path for the compiled binaries is specified with the
"-output" flag. The value is a string that is a Go text template.
The default value is "{{.Dir}}_{{.OS}}_{{.Arch}}". The variables and
their values should be self-explanatory.

Platforms (OS/Arch):

The operating systems and architectures to cross-compile for may be
specified with the "-arch" and "-os" flags. These are space separated lists
of valid GOOS/GOARCH values to build for, respectively. You may prefix an
OS or Arch with "!" to negate and not build for that platform. If the list
is made up of only negations, then the negations will come from the default
list.

Additionally, the "-osarch" flag may be used to specify complete os/arch
pairs that should be built or ignored. The syntax for this is what you would
expect: "darwin/amd64" would be a valid osarch value. Multiple can be space
separated. An os/arch pair can begin with "!" to not build for that platform.

The "-osarch" flag has the highest precedent when determing whether to
build for a platform. If it is included in the "-osarch" list, it will be
built even if the specific os and arch is negated in "-os" and "-arch",
respectively.

第一次使用要进行构建交叉编译的库,这一步很慢,我的机器大概需要5分钟左右.

1
gox -build-toolchain

构建好就可以使用了,如果你想构建全平台(主流)的文件,那么直接 gox ,目录下会生成对应平台的文件.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
vagrant@vagrant:/vagrant$ gox
Number of parallel builds: 1

--> darwin/386: _/vagrant
--> darwin/amd64: _/vagrant
--> linux/386: _/vagrant
--> linux/amd64: _/vagrant
--> linux/arm: _/vagrant
--> freebsd/386: _/vagrant
--> freebsd/amd64: _/vagrant
--> openbsd/386: _/vagrant
--> openbsd/amd64: _/vagrant
--> windows/386: _/vagrant
--> windows/amd64: _/vagrant
--> freebsd/arm: _/vagrant
--> netbsd/386: _/vagrant
--> netbsd/amd64: _/vagrant
--> netbsd/arm: _/vagrant
--> plan9/386: _/vagrant

当然,如果要指定平台可以使用如下命令:

1
2
3
4
5
$ gox -os="linux"
or
$ gox -osarch="linux/amd64"
or
$ gox -osarch="linux/amd64 windows/amd64"

如果要指定输出目录和文件名可以使用:

1
$ gox -osarch="darwin/amd64" -output="output_path"

备注: gox交叉编译不支持 CGO