以下紀錄的是在windows x64下將golang cross-compiler 成 linux x86
1.先到 $GOROOT 底下的 src(我個人是 C:/go/src)建立一個build.bat檔
2.edit build.bat
set CGO_ENABLED=0
set GOROOT_BOOTSTRAP=C:/Go
:::::::x86:::::::
set GOARCH=386
set GOOS=windows
call make.bat --no-clean
set GOOS=linux
call make.bat --no-clean
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:::::::x64:::::::
set GOARCH=amd64
set GOOS=linux
call make.bat --no-clean
set GOOS=windows
call make.bat --no-clean
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
set GOARCH=amd64
set GOOS=windows
go get github.com/nsf/gocode
pause
3.執行 build.bat
4.到 $GOROOT/bin 檢查是否有安裝相關檔案(ex:linux_386)
5.到要編譯程式的資料夾底下,我將指令寫成cross-compiler2linux_x86.bat檔
cd %~dp0
set GOOS=linux
set GOARCH=386
go build helloworld.go
pause
6.執行cross-compiler2linuxx86.bat 就會生成 "helloworld"檔案
7.將 "helloworld" 檔案複製到linux x86底下執行確認。
=======================================================================
本篇cross-compile工具只有設定windows x86, x64, linux x86, x64四種,其他平台請參考下列原文敘述進行修改。
$GOOS
and $GOARCH
The name of the target operating system and compilation architecture. These default to the values of
$GOHOSTOS
and $GOHOSTARCH
respectively (described below).
Choices for
$GOOS
are darwin
(Mac OS X 10.7 and above and iOS), dragonfly
, freebsd
, linux
, netbsd
, openbsd
, plan9
, solaris
and windows
. Choices for $GOARCH
are amd64
(64-bit x86, the most mature port), 386
(32-bit x86), arm
(32-bit ARM), arm64
(64-bit ARM), ppc64le
(PowerPC 64-bit, little-endian), ppc64
(PowerPC 64-bit, big-endian), mips64le
(MIPS 64-bit, little-endian), and mips64
(MIPS 64-bit, big-endian). The valid combinations of $GOOS
and $GOARCH
are:$GOOS | $GOARCH | |
---|---|---|
android | arm | |
darwin | 386 | |
darwin | amd64 | |
darwin | arm | |
darwin | arm64 | |
dragonfly | amd64 | |
freebsd | 386 | |
freebsd | amd64 | |
freebsd | arm | |
linux | 386 | |
linux | amd64 | |
linux | arm | |
linux | arm64 | |
linux | ppc64 | |
linux | ppc64le | |
linux | mips64 | |
linux | mips64le | |
netbsd | 386 | |
netbsd | amd64 | |
netbsd | arm | |
openbsd | 386 | |
openbsd | amd64 | |
openbsd | arm | |
plan9 | 386 | |
plan9 | amd64 | |
solaris | amd64 | |
windows | 386 | |
windows | amd64 |
ref: