apt-get install gcc-mingw-w64
env CGO_ENABLED= GOOS=windows GOARCH=amd64 CC=x86_64-w64-mingw32-gcc go build -o ./app/app.exe ./app/main.go
env CGO_ENABLED= GOOS=windows GOARCH= CC=i686-w64-mingw32-gcc go build -o ./app/app.exe ./app/main.go

Gox 是一个简单的,不花俏的Go平台交叉编译工具,它的用处就和标准的 go build 一样。Gox 会并行地为多种平台编译。Gox 同时也提供了一套交叉编译工具链。

Gox 项目地址:https://github.com/mitchellh/gox

安装

为了安装 Gox,请使用 go get。我们已经为版本打上了标签,所以可以随便切换标签进行编译:

$ go get github.com/mitchellh/gox
...
$ gox -h
...

用法

在你使用 Gox 之前,你必须先有一套交叉编译工具链。Gox 可以自动帮你完成这个。你需要做的只是运行(每次更新 Go 都要这样做这步):

$ gox -build-toolchain ...

当你完成这个,你可以已经准备好进行交叉编译了。
如果你知道怎么去使用 go build, 那么你也知道怎么去使用 Gox 了。例如,编译当前的项目,无需提供参数,只需要调用 gox。Gox 就会根据 CPU 的数量并行地为各个平台编译:

$ gox Number of parallel builds: 4

--> darwin/386: github.com/mitchellh/gox
--> darwin/amd64: github.com/mitchellh/gox
--> linux/386: github.com/mitchellh/gox
--> linux/amd64: github.com/mitchellh/gox
--> linux/arm: github.com/mitchellh/gox
--> freebsd/386: github.com/mitchellh/gox
--> freebsd/amd64: github.com/mitchellh/gox
--> openbsd/386: github.com/mitchellh/gox
--> openbsd/amd64: github.com/mitchellh/gox
--> windows/386: github.com/mitchellh/gox
--> windows/amd64: github.com/mitchellh/gox
--> freebsd/arm: github.com/mitchellh/gox
--> netbsd/386: github.com/mitchellh/gox
--> netbsd/amd64: github.com/mitchellh/gox
--> netbsd/arm: github.com/mitchellh/gox
--> plan9/386: github.com/mitchellh/gox

或者,你只想编译某个项目和子项目:

$ gox ./... ...

或者,你想仅仅为 linux 编译:

$ gox -os="linux" ...

或者,你仅仅只想为 64 位的 linux 编译:

$ gox -osarch="linux/amd64" ...

还有更多的选项,可以通过 gox -h 查看帮助。

和其他交叉编译工具的比较

非常感谢这些工具为我们提供了更多的选择,它们为 go 平台的交叉编译工具提供做了很多方面的贡献.

    • Dave Cheney的交叉编译器: Gox 可以为多种平台编译,所以也能容易地运行在各种 Go 支持的平台上。但Dave的那个需要一个 shell 来运行。Gox 支持并行地编译,但 Dave 的只是按顺序地编译。Gox 也能非常方便地使用的内置的 arch 系统的内置过滤工具。
    • goxc:它是一个功能丰富的工具,能编译系统项目,上传二进制文件,产生下载页面等;相较之下,Gox 在交叉编译二元文件方面稍稍弱些。但 Gox 能并行地编译项目,而 goxc 不能。Gox 也没有强制指定编译二元文件时输出结果的格式。

参考资料:

ubuntu 下 golang 跨平台编译 windows 程序:  http://studygolang.com/articles/8167

跨平台编译Go程序(交叉编译):  http://studygolang.com/topics/21

golang交叉编译:  http://blog.csdn.net/coroutines/article/details/39546491

Gox : 简单实在的Go平台交叉编译工具:  https://segmentfault.com/a/1190000000346086

【GoLang】golang 交叉编译 实现&工具的更多相关文章

  1. golang 打包,交叉编译,压缩

    打包,压缩 我们的常规打包方式 $ go build Mac下我们用 ls -lh查看,可以看到我们打包出来的可执行文件会比较大,一般只写几行代码就回又3M以上的文件大小了. 我们的带压缩的打包方式 ...

  2. Golang 如何交叉编译

    Golang 支持交叉编译,即在一个平台上生成另一个平台的可执行程序.方法如下: Mac 下编译 Linux 和 Windows 64位可执行程序 CGO_ENABLED=0 GOOS=linux G ...

  3. [golang]golang如何覆盖输出console,实现进度条;golang一个骚气的进度提示库

    [golang]golang如何覆盖输出console,实现进度条 package main import( "fmt" "os" "time&quo ...

  4. Golang学习--包管理工具glide

    上一篇文章中我们已经成功的运行了go的代码,这是我们迈出的最基础的一步. 一个项目通常会依赖很多外部的库,当依赖的库比较多的时候,手工管理就会比较麻烦,这个时候就需要包管理工具出场了,帮你管理好所有依 ...

  5. Golang Vendor 包管理工具 glide 使用教程

    Glide 是 Golang 的 Vendor 包管理器,方便你管理 vendor 和 verdor 包.类似 Java 的 Maven,PHP 的 Composer. Github:https:// ...

  6. golang shell 交叉编译

    #!/usr/bin/env bash set -e uname_s=`uname -s | awk '{print tolower($0)}'` uname_m=`uname -m` timeTag ...

  7. golang之交叉编译设置

    俺的环境,os x,目的,生成64位linux的elf文件 直接下载osx的包就可以,不需要特意去下载源码包,我的go目录是~/golang/go cd ~/golang/go/srcGOOS=lin ...

  8. [golang] Glide 包管理工具,在windows10 64位系统上的bug修复方案

    bug重现 [ERROR] Unable to export dependencies to vendor directory: Error moving files: exit status 1. ...

  9. nexus 3.17.0 做为golang 的包管理工具

    nexus 3.17.0 新版本对于go 包管理的支持是基于go mod 的,同时我们也需要一个athens server 然后在nexus 中配置proxy 类型的repo 参考配置 来自官方的配置 ...

随机推荐

  1. PetaPoco初体验(转)

    PetaPoco初体验(转) PetaPoco初体验(转) 大部分转自: http://landyer.com/archives/138 PetaPoco C#微型ORM框架,基本无需配置,仅由单个c ...

  2. 微信小程序 教程及示例

    作者:初雪链接:https://www.zhihu.com/question/50907897/answer/128494332来源:知乎著作权归作者所有,转载请联系作者获得授权.微信小程序正式公测, ...

  3. Linux查看软件安装路径

    Linux中查看某 个软件的安装路径(地址)有时显得非常重要.比如某个文件的快速启动项被删除,或者你要建立快速启动项,或者想删除. 添加安装文件等等,很多地方都要用到查案文件安装路径的命令. 这里给大 ...

  4. [MongoDB]入门操作

    摘要 在工作中也经常使用mongodb,每次遇到新的操作都需要去查,比较麻烦,准备在博客中系统的学习一下mongodb.首先在本地安装mongodb环境,可以下载一个windows的版本. 官网地址 ...

  5. mouse scrollings and zooming operations in linux & windows are opposite

    mouse scrollings and zooming operations in linux & windows are opposite. windows中, 鼠标滚动的方向是: 查看页 ...

  6. -bash: sudo: command not found Error and Solution

    文章转自: http://www.cyberciti.biz/faq/debian-ubuntu-rhel-centos-linux-bash-sudo-command-not-found/ 安装su ...

  7. 如何向git账号上提交代码

    官方说明:https://help.github.com/articles/generating-ssh-keys/ 1,为Github账户设置SSH key 文章地址:http://zuyunfei ...

  8. Python程序的常见错误(收集篇)

    关于Python Python是一门解释性的,面向对象的,并具有动态语义的高级编程语言.它高级的内置数据结构,结合其动态类型和动态绑定的特性,使得它在快速应用程序开发(Rapid Applicatio ...

  9. 【Bootstrap】Bootstrap和Java分页-第一篇

    目录 关于此文 pagination BetweenIndex DefaultPagination QueryHandler BookDaoImpl BookServiceImpl BookActio ...

  10. spring学习

    http://blog.csdn.net/chjttony/article/details/6301523 http://blog.csdn.net/chjttony/article/details/ ...