mac 下編譯其他平台的執行文件方式請參看這篇文章,http://www.cnblogs.com/ghj1976/archive/2013/04/19/3030703.html
本篇文章是win下的跨平台編譯。
安裝GCC編譯器(MinGW)
我們需要GCC編譯環境,這里我選擇的是:MinGW。
mac 下安裝了Xcode自動就帶了GCC,win下沒有,所以我們用MinGW這個最小化安裝的GCC。
MinGW官網提供了一種自動在線下載安裝的gui小程序,http://sourceforge.net/projects/mingw/files/Installer/mingw-get-inst/ 。這個程序只需要簡單的一步步跟着提示,就會自動下載每個組件的最新版本並且安裝在電腦里面。
安裝完成后,需要把gcc放在PATH全局參數下,這樣才能被直接使用。
我這里安裝在 C:\MinGW\bin 目錄下。
MinGW全稱Minimalist GNU For Windows,是個精簡的Windows平台C/C++、ADA及Fortran編譯器,相比Cygwin而言,體積要小很多,使用較為方便。MinGW提供了一套完整的開源編譯工具集,以適合Windows平台應用開發,且不依賴任何第三方C運行時庫。
MinGW包括:
一套集成編譯器,包括C、C++、ADA語言和Fortran語言編譯器
用於生成Windows二進制文件的GNU工具的(編譯器、鏈接器和檔案管理器)
用於Windows平台安裝和部署MinGW和MSYS的命令行安裝器(mingw-get)
用於命令行安裝器的GUI打包器(mingw-get-inst)
參考資料:
http://www.metsky.com/archives/588.html
編譯本機系統go編譯環境
在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