0
点赞
收藏
分享

微信扫一扫

win下 golang 跨平台编译

非衣所思 2023-06-22 阅读 77


本篇文章是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纯净编译环境的简单方法

举报

相关推荐

0 条评论