基本介绍

交叉编译是为了在不同平台编译出其他平台的程序,比如在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"

赶紧把你生成的程序发给小伙伴执行试试吧,以上就是本文全部内容,感谢阅读。

最新文章

  1. Extend Volume 操作 - 每天5分钟玩转 OpenStack(56)
  2. *HDU2510 DFS
  3. 排球比赛计分规则(P205页)
  4. 浅谈 block(1) – clang 改写后的 block 结构
  5. Hyper-V 上的android
  6. linux -redhat rpm 和zabbix和各种rpm包下载地址
  7. Codeforces 219D. Choosing Capital for Treeland (树dp)
  8. python之量的概念
  9. java 发布和逸出
  10. mongodb日志服务器方案
  11. java 包中的一款经典的singleton模式
  12. oracle正则截取字符串的函数
  13. 阿里云的esc
  14. [Angular 2] Using Pipes to Filter Data
  15. 功能:使用QQ号登陆,并加上微信和短信提醒,是否增量备份可选,阿里大鱼短信发送开发与测试,聚合数据(用JSON发短信,比较清楚)
  16. 【USACO】接住苹果
  17. Java中常用类(包装类扩展知识)
  18. C# to IL 6 Reference and Value Types(引用类型和值类型)
  19. Superset 制作图表
  20. Base64的好处

热门文章

  1. K8S 1.16 [plugin flannel does not support config version
  2. 自己总结numpy用法
  3. Redis 命令执行全过程分析
  4. GC与内存分配策略
  5. HTML入门(列表、表单、常用表单控件、浮动框架、iframe、 摘要与细节、度量标签)
  6. Cocos2d-x.3.0开发环境搭建
  7. A:mysql数据库章节导航
  8. 系统重装/装Anaconda后,Windows开始菜单缺少快捷方式解决方案
  9. office2019专业版激活秘钥 激活码
  10. idea 出现Can't load AMD 64-bit .dll on a IA 32-bit platform错误