mac 下编译其余平台的执行文件方式请参看这篇文章,http://www.cnblogs.com/ghj1976/archive/2013/04/19/3030703.html html
本篇文章是win下的跨平台编译。linux
咱们须要GCC编译环境,这里我选择的是:MinGW。git
mac 下安装了Xcode自动就带了GCC,win下没有,因此咱们用MinGW这个最小化安装的GCC。github
MinGW官网提供了一种自动在线下载安装的gui小程序,http://sourceforge.net/projects/mingw/files/Installer/mingw-get-inst/ 。这个程序只须要简单的一步步跟着提示,就会自动下载每一个组件的最新版本而且安装在电脑里面。golang
安装完成后,须要把gcc放在PATH全局参数下,这样才能被直接使用。json
我这里安装在 C:\MinGW\bin 目录下。小程序
MinGW全称Minimalist GNU For Windows,是个精简的Windows平台C/C++、ADA及Fortran编译器,相比Cygwin而言,体积要小不少,使用较为方便。MinGW提供了一套完整的开源编译工具集,以适合Windows平台应用开发,且不依赖任何第三方C运行时库。windows
MinGW包括:
一套集成编译器,包括C、C++、ADA语言和Fortran语言编译器
用于生成Windows二进制文件的GNU工具的(编译器、连接器和档案管理器)
用于Windows平台安装和部署MinGW和MSYS的命令行安装器(mingw-get)
用于命令行安装器的GUI打包器(mingw-get-inst)api
参考资料:架构
http://www.metsky.com/archives/588.html
在Go的安装目录的src目录下,执行脚本: all.bat
执行结果相似以下:省略了部分信息。
crypto/sha1
crypto/hmac
strconv
os
math/rand
crypto/sha256
os/user
runtime/cgo
runtime/debug
runtime/race
net/rpc/jsonrpc
testing
testing/iotest
testing/quick
# Testing packages.
ok cmd/api 1.328s
? cmd/cgo [no test files]
ok cmd/fix 1.674s
ok cmd/go 2.560s
? cmd/godoc [no test files]
ok cmd/gofmt 0.570s
ok unicode 0.254s
ok unicode/utf16 0.147s
ok unicode/utf8 0.190s
? unsafe [no test files]
# runtime -cpu=1,2,4
ok runtime 47.297s
# sync -cpu=10
ok sync 0.831s
# ..\misc\dashboard\builder ..\misc\goplay
# ..\test\bench\go1
ok _/C_/Go/test/bench/go1 3.727s
# ..\misc\cgo\life
# ..\misc\cgo\stdio
# ..\misc\cgo\test
ok _/C_/Go/misc/cgo/test 3.024s
# ..\misc\cgo\testso
# ..\doc\progs
# ..\test
# Checking API compatibility.
Go version is "go1.1.1", ignoring -next ..\api\next.txt
~pkg net, func ListenUnixgram(string, *UnixAddr) (*UDPConn, error)
~pkg syscall (darwin-386), func Fchflags(string, int) error
~pkg syscall (darwin-386-cgo), func Fchflags(string, int) error
~pkg syscall (darwin-amd64), func Fchflags(string, int) error
~pkg syscall (darwin-amd64-cgo), func Fchflags(string, int) error
~pkg syscall (freebsd-386), func Fchflags(string, int) error
~pkg syscall (freebsd-amd64), func Fchflags(string, int) error
~pkg text/template/parse, type DotNode bool
~pkg text/template/parse, type Node interface { Copy, String, Type }
ALL TESTS PASSED
---
Installed Go for windows/386 in C:\Go
Installed commands in C:\Go\bin
C:\Go\src>
在命令行,依次执行,或者把下面放在一个bat文件中,一块儿执行:
# build linux amd64
set GOOS=linux
set GOARCH=amd64
set CGO_ENABLED=0
注意,
当心空格, 上面环境变量设置最后都是不该该有空格的,不然会报相似: go tool dist: unknown $GOOS linux 的错误,
若是须要准备那个平台,那个架构的编译器和链接器,修改对应参数便可。
注意,这里必定要 make.bat --no-clean
执行的结果相似以下:
pkg/regexp
pkg/go/token
pkg/go/scanner
pkg/go/ast
pkg/go/parser
pkg/os/exec
pkg/os/signal
pkg/net/url
pkg/text/template/parse
pkg/text/template
pkg/go/doc
pkg/go/build
cmd/go
pkg/runtime (linux/amd64)
移动了 1 个文件。
# Building tools for local system. windows/386
runtime
errors
sync/atomic
unicode
os/user
runtime/debug
testing
net/rpc/jsonrpc
testing/iotest
testing/quick
# Building packages and commands.
runtime
errors
sync/atomic
unicode
net/mail
net/rpc
net/smtp
os/user
runtime/debug
net/rpc/jsonrpc
testing
testing/iotest
testing/quick
---
Installed Go for linux/amd64 in C:\Go
Installed commands in C:\Go\bin
C:\Go\src>
在命令行,依次执行,或者把下面放在一个bat文件中,一块儿执行,即完成对应平台的编译:
set GOOS=linux
set GOARCH=amd64
set CGO_ENABLED=0
go build
Cross compile for darwin from windows
https://groups.google.com/forum/#!topic/golang-nuts/IBJt_HkHJIw
64位WIN7中golang交叉编译环境 (这篇文章不能看,误导了我好久)
https://github.com/xgene/study/blob/master/README.md
Windows下利用MinGW配置GCC纯净编译环境的简单方法
http://www.cnblogs.com/apollius/archive/2012/11/21/2781069.html