basic introduction
Cross-compilation is to compile programs for other platforms on different platforms, such as compiling Windows programs on Linux, compiling Linux programs on Windows, and compiling 64-bit programs on 32-bit systems. Gox introduced today is one of them. tool.
Configuration Environment
First configure the environment variables of the Go language and ~/.bash_profile
set them in , and briefly explain why they are added to the file. First, the following code is only valid for the current session after the terminal is executed, and the variables are invalid when the terminal is closed .bash_profile
. Each time you log in, it will be executed once, and the environment variables will be set to this file, and the environment variables will be initialized each time you log in. Of course, it ~/.bashrc
is also possible to put it in, it will be executed not only when you log in, but also every time you open the terminal.
export GOPATH=${HOME}/go
export GOROOT=/usr/local/go
export GOBIN=${GOPATH}/bin
export PATH=${PATH}:${GOBIN}
复制代码
GOROOT and GOPATH should be set according to your own situation, do not follow blindly. After the setting is completed, if you want the file to take effect immediately, you can execute the source
command.
source ~/.bash_profile
复制代码
If your terminal is installed zsh
, it may still fail after reopening the terminal, so you can append the command ~/.zshrc
to the last line of the file .source
source ~/.bash_profile
复制代码
installation of gox
Execute the following command in the terminal to install.
go get github.com/mitchellh/gox
复制代码
After the installation is complete, execute gox -h
it. If there is help information displayed, it means the installation is successful.
➜ ~ 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.
......
复制代码
Use of gox
By convention, we first offer hello,world
the demo code.
package main
import "fmt"
func main() {
fmt.Print("hello,world")
}
复制代码
At this point, enter the working directory in the project ($GOPATH/src/[your project name]), and execute the gox
command directly, which will generate executable files for up to 21 different platforms, spanning linux, windows, freebsd, darwin and other systems .
➜ 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
复制代码
But I don't want to generate programs for all platforms at once. At this time, I need to specify the parameters of gox. As shown below, the os
parameters specify the name of the system to be generated and arch
the architecture of the CPU.
gox -os "windows" -arch amd64
复制代码
In fact, it supports more than 21 models. These are only generated by default. The following is gox's definition of various systems. Interested students can understand by themselves.
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},
}...)
复制代码
In addition to the command just now, there is another generation method, which combines the system and the architecture with slashes and generates them in batches.
gox -osarch "windows/amd64 linux/amd64"
复制代码
Hurry up and send the program you generated to your friends to try it out. The above is the whole content of this article, thank you for reading.
Reprinted in: https://juejin.im/post/5d0261eff265da1b67210925