Go语言Mac、Linux、Windows 下交叉编译

在很多时候,由于开发的方便,会有这样的场景出现,使用Mac开发或使用Windows开发,需要编译成Linux系统的执行文件,那么如何做到?Go语言提供了非常方便的命令行操作,即可实现。

1、Mac下编译Linux, Windows

# Linux
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build filename.go
 
# Windows
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build filename.go

2、Linux下编译Mac, Windows

# Mac
CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build filename.go
 
# Windows
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build filename.go

3、Windows下编译Mac, Linux

# Mac
SET CGO_ENABLED=0
SET GOOS=darwin
SET GOARCH=amd64
go build filename.go
 
# Linux
SET CGO_ENABLED=0
SET GOOS=linux
SET GOARCH=amd64
go build filename.go

4、参数说明

查看环境:

    $> go env
    set GOARCH=amd64
    set GOBIN=
    set GOCACHE=C:\Users\bboyHan\AppData\Local\go-build
    set GOEXE=
    set GOHOSTARCH=amd64
    set GOHOSTOS=windows
    set GOOS=linux
    set GOPATH=C:\Go\workspace\study\src\
    set GORACE=
    set GOROOT=C:\Go
    set GOTMPDIR=
    set GOTOOLDIR=C:\Go\pkg\tool\windows_amd64
    set GCCGO=gccgo
    set CC=gcc
    set CXX=g++
    set CGO_ENABLED=0
    set CGO_CFLAGS=-g -O2
    set CGO_CPPFLAGS=
    set CGO_CXXFLAGS=-g -O2
    set CGO_FFLAGS=-g -O2
    set CGO_LDFLAGS=-g -O2
    set PKG_CONFIG=pkg-config
    set GOGCCFLAGS=-fPIC -m64 -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=C:\Users\bboyHan\AppData\Local\Temp\go-build162645406=/tmp/go-build -gno-record-gcc-switches

[Haima的博客] http://www.cnblogs.com/haima/
posted @ 2019-12-15 07:47  HaimaBlog  阅读(565)  评论(0编辑  收藏  举报