Go语言交叉编译工具gox
基本介绍
交叉编译是为了在不同平台编译出其他平台的程序,比如在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"
赶紧把你生成的程序发给小伙伴执行试试吧,以上就是本文全部内容,感谢阅读。
Go语言交叉编译工具gox的更多相关文章
- 交叉编译工具链介绍《Building Embedded Linux Systems》
1.前言 配置和编译一个合适的GNU工具链是相对复杂的并且需要很精细的操作,包括你需要对不同软件库之间的依赖关系.它们的各自的任务,不同软件库版本情况都有比较好的了解,编译工具链是一个乏味的工作. 2 ...
- Linux ARM交叉编译工具链制作过程【转】
本文转载自:http://www.cnblogs.com/Charles-Zhang-Blog/archive/2013/02/21/2920999.html 一.下载源文件 源代码文件及其版本与下载 ...
- atitit.编程语言 程序语言 的 工具性 和 材料性 双重性 and 语言无关性 本质
atitit.编程语言 程序语言 的 工具性 和 材料性 双重性 and 语言无关性 本质 #---语言的 工具和材料双重性 有的人说语言是个工具,有的人说语言是个材料..实际上语言同时属于两个属性. ...
- CROSSTOOL-NG建立交叉编译工具链
CROSSTOOL-NG建立交叉编译工具链 因为考试和学习的原因我已经一段时间没有玩我的JZ2440,现在终于考完试了,我再次找出了我的JZ2440.我之前学习的时候使用的是韦东山老师提供的开发工具, ...
- Crosstool-ng制作交叉编译工具链
Crosstool-ng制作交叉编译工具链 交叉编译器可以用现成的,比如CodeSourcery制作的交叉编译器,也可以自己制作,一般是用kernel+gcc+glibc+binutils的源码包来编 ...
- LInux系统的C语言开发工具笔记
常用的C语言开发工具有很多,每个开发工具所支持的库函数和对标准的实现都有差异.对于初学者,选择一款使用广泛.上手容易的开发工具才是王道.在Windows 上很多从事C和C++开发的人员都选择VS作为开 ...
- 【转】ARM交叉编译工具链
原文网址:http://www.veryarm.com/cross-tools 为什么要用交叉编译器? 交叉编译通俗地讲就是在一种平台上编译出能运行在体系结构不同的另一种平台上的程序,比如在PC平台( ...
- 【linux】UBUNTU 12.04下傻瓜式简单安装arm-linux-gcc等gnu arm toolchain交叉编译工具
欢迎转载,转载时请保留作者信息,谢谢. 邮箱:tangzhongp@163.com 博客园地址:http://www.cnblogs.com/embedded-tzp Csdn博客地址:http:// ...
- ARM交叉编译工具链分类说明
转载整理自:http://www.veryarm.com/cross-tools 从授权上,ARM交叉编译工具链分为免费授权版和付费授权版. 免费版目前有三大主流工具商提供,第一是GNU(提供源码,自 ...
随机推荐
- opencv检测图像直线
#include<opencv2/opencv.hpp> #include<iostream> using namespace std; using namespace cv; ...
- IT兄弟连 HTML5教程 CSS3属性特效 2D变换2
3 scale() 方法 通过scale() 方法,元素的尺寸会增加或减少,根据给定的宽度(X轴)和高度(Y轴)参数.缩放scale()函数让元素根据中心原点对对象进行缩放.默认值是1,因此0.01 ...
- Spring Boot Failed to load resource: the server responded with a status of 404 ()
出现错误: Failed to load resource: the server responded with a status of 404 () 但是其他页面正常显示: 原因: 浏览器看一下: ...
- LinuxProbe小结
1.修改主机名: /etc/hostname 2.配置 yum 软件仓库: (1)进入到 /etc/yum.repos.d/目录下,创建一个linuxprobe.repo的新文件(文件名称任意,结尾必 ...
- English: Class GXX
######################################## GGGGG GG GG GG GG GGGG GG GGG GGGGGG author:enomothem date: ...
- WebSessionStore: Could not obtain reference to HttpContext
IBatis.net在多线程中报错“WebSessionStore: Could not obtain reference to HttpContext” 分析: 因为ibatis的ISqlMapSe ...
- iOS----------jenkins
错误日志: ERROR: Error fetching remote repo 'origin' Finished: FAILURE ERROR: Error cloning remote repo ...
- subprocess之check_out用法
在python3中使用subprocess的check_out方法时,因为该输出为byte类型,所以如果要查看具体的内容时需要进行转码,如果转码不对话,会影响内容输出的可读性,如下: #1,输出解码不 ...
- leetcode题解:回文数
判断一个整数是否是回文数.回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数. 示例 1: 输入: 121 输出: true 示例 2: 输入: -121 输出: false 解释: 从左向 ...
- 删除文件linux
bool LxDeleteFile(const char* src){ int32_t iRe = remove(src); ) return true; else return false; }