2017年1月23日 星期一

golang cross-compiler on windows to...

Golang 本身就有提供cross-compile相關的工具

以下紀錄的是在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), dragonflyfreebsdlinuxnetbsdopenbsdplan9solaris 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
androidarm
darwin386
darwinamd64
darwinarm
darwinarm64
dragonflyamd64
freebsd386
freebsdamd64
freebsdarm
linux386
linuxamd64
linuxarm
linuxarm64
linuxppc64
linuxppc64le
linuxmips64
linuxmips64le
netbsd386
netbsdamd64
netbsdarm
openbsd386
openbsdamd64
openbsdarm
plan9386
plan9amd64
solarisamd64
windows386
windowsamd64


ref:

沒有留言:

張貼留言