win下 golang 跨平臺編譯

mac 下編譯其餘平臺的執行文件方式請參看這篇文章,http://www.cnblogs.com/ghj1976/archive/2013/04/19/3030703.html html

本篇文章是win下的跨平臺編譯。linux

安裝GCC編譯器(MinGW)

咱們須要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編譯環境

在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

相關文章
相關標籤/搜索