Go cross-compiler (cross compiler under the Go language Mac / Linux / Windows)

Go cross-compiler (cross compiler under the Go language Mac / Linux / Windows)

2019/11/21 Chenxin

In many cases, due facilitate the development of such a scenario would occur, or the use of Mac developers use Windows development, needs to be compiled into executable file Linux system, then how to do? Go language provides a very convenient command-line operation, can be realized.

1.Mac compiled under 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
如: CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -o helloworld-windows helloworld.go

2.Linux compiled 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 compiled 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. Parameter Description

View in:

$> go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/chanix/Library/Caches/go-build"
GOENV="/Users/chanix/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/chanix/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/ln/v9zzjzys4rsbfnsw4_8l2cgm0000gn/T/go-build896214833=/tmp/go-build -gno-record-gcc-switches -fno-common"

Guess you like

Origin www.cnblogs.com/chanix/p/11942575.html