Golang在Mac、Linux、Windows下如何交叉编译

by LauCyun Jan 10,2018 11:35:44 28,330 views

Golang 支持交叉编译,在一个平台上生成另一个平台的可执行程序,最近使用了一下,非常好用,这里备忘一下。

Mac 下编译 Linux 和 Windows 64位可执行程序

# Linux
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -i -o hello hello.go
# Windows
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -i -o hello hello.go

Linux 下编译 Mac 和 Windows 64位可执行程序

# Darwin (Mac OS)
CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -i -o hello hello.go
# Windows
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -i -o hello hello.go

Windows 下编译 Mac 和 Linux 64位可执行程序

:: Darwin (Mac OS)
SET CGO_ENABLED=0
SET GOOS=darwin
SET GOARCH=amd64
go build -i -o hello hello.go

:: Liunx
SET CGO_ENABLED=0
SET GOOS=linux
SET GOARCH=amd64
go build -i -o hello hello.go

说明:

  • GOOS:目标平台的操作系统(darwin、freebsd、linux、netbsd、openbsd、windows) 
  • GOARCH:目标平台的体系架构(386、amd64、arm、mips、mips64、mips64le、mipsle) 
  • 交叉编译不支持 CGO 所以要禁用它

上面的命令编译 64 位可执行程序,你当然应该也会使用 386 编译 32 位可执行程序 。

多博客都提到要先增加对其它平台的支持,但是我跳过那一步,上面所列的命令也都能成功,且得到我想要的结果,可见那一步应该是非必须的,或是我所使用的 Go 版本已默认支持所有平台。

最后,给出两份脚本:

Linux:

#!/usr/bin/env bash

# Darwin (Mac OS)
CGO_ENABLED=0 GOOS=darwin GOARCH=386 $(which go) build -i -o hello-darwin-i386 hello.go
CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 $(which go) build -i -o hello-darwin-amd64 hello.go

# FreeBSD
CGO_ENABLED=0 GOOS=freebsd GOARCH=386 $(which go) build -i -o hello-freebsd-i386 hello.go
CGO_ENABLED=0 GOOS=freebsd GOARCH=amd64 $(which go) build -i -o hello-freebsd-amd64 hello.go
CGO_ENABLED=0 GOOS=freebsd GOARCH=arm $(which go) build -i -o hello-freebsd-arm hello.go

# Linux
CGO_ENABLED=0 GOOS=linux GOARCH=386 $(which go) build -i -o hello-linux-i386 hello.go
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 $(which go) build -i -o hello-linux-amd64 hello.go
CGO_ENABLED=0 GOOS=linux GOARCH=arm $(which go) build -i -o hello-linux-arm hello.go
CGO_ENABLED=0 GOOS=linux GOARCH=mips $(which go) build -i -o hello-linux-mips hello.go
CGO_ENABLED=0 GOOS=linux GOARCH=mips64 $(which go) build -i -o hello-linux-mips64 hello.go
CGO_ENABLED=0 GOOS=linux GOARCH=mips64le $(which go) build -i -o hello-linux-mips64le hello.go
CGO_ENABLED=0 GOOS=linux GOARCH=mipsle $(which go) build -i -o hello-linux-mipsle hello.go

# NetBSD
CGO_ENABLED=0 GOOS=netbsd GOARCH=386 $(which go) build -i -o hello-netbsd-i386 hello.go
CGO_ENABLED=0 GOOS=netbsd GOARCH=amd64 $(which go) build -i -o hello-netbsd-amd64 hello.go
CGO_ENABLED=0 GOOS=netbsd GOARCH=arm $(which go) build -i -o hello-netbsd-arm hello.go

# OpenBSD
CGO_ENABLED=0 GOOS=openbsd GOARCH=386 $(which go) build -i -o hello-openbsd-i386 hello.go
CGO_ENABLED=0 GOOS=openbsd GOARCH=amd64 $(which go) build -i -o hello-openbsd-amd64 hello.go

# Windows
CGO_ENABLED=0 GOOS=windows GOARCH=386 $(which go) build -i -o hello-windows-i386.exe hello.go
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 $(which go) build -i -o hello-windows-amd64.exe hello.go

Windows:

:: Darwin (Mac OS)
SET CGO_ENABLED=0
SET GOOS=darwin
SET GOARCH=386
go build -i -o hello-darwin-i386 hello.go

SET CGO_ENABLED=0
SET GOOS=darwin
SET GOARCH=amd64
go build -i -o hello-darwin-amd64 hello.go


:: FreeBSD
SET CGO_ENABLED=0
SET GOOS=freebsd
SET GOARCH=386
go build -i -o hello-freebsd-i386 hello.go

SET CGO_ENABLED=0
SET GOOS=freebsd
SET GOARCH=amd64
go build -i -o hello-freebsd-amd64 hello.go

SET CGO_ENABLED=0
SET GOOS=freebsd
SET GOARCH=arm
go build -i -o hello-freebsd-arm hello.go


:: Linux
SET CGO_ENABLED=0
SET GOOS=linux
SET GOARCH=386
go build -i -o hello-linux-i386 hello.go

SET CGO_ENABLED=0
SET GOOS=linux
SET GOARCH=amd64
go build -i -o hello-linux-amd64 hello.go

SET CGO_ENABLED=0
SET GOOS=linux
SET GOARCH=arm
go build -i -o hello-linux-arm hello.go

SET CGO_ENABLED=0
SET GOOS=linux
SET GOARCH=mips
go build -i -o hello-linux-mips hello.go

SET CGO_ENABLED=0
SET GOOS=linux
SET GOARCH=mips64
go build -i -o hello-linux-mips64 hello.go

SET CGO_ENABLED=0
SET GOOS=linux
SET GOARCH=mips64le
go build -i -o hello-linux-mips64le hello.go

SET CGO_ENABLED=0
SET GOOS=linux
SET GOARCH=mipsle
go build -i -o hello-linux-mipsle hello.go



:: NetBSD
SET CGO_ENABLED=0
SET GOOS=netbsd
SET GOARCH=386
go build -i -o hello-netbsd-i386 hello.go

SET CGO_ENABLED=0
SET GOOS=netbsd
SET GOARCH=amd64
go build -i -o hello-netbsd-amd64 hello.go

SET CGO_ENABLED=0
SET GOOS=netbsd
SET GOARCH=arm
go build -i -o hello-netbsd-arm hello.go


:: OpenBSD
SET CGO_ENABLED=0
SET GOOS=openbsd
SET GOARCH=386
go build -i -o hello-openbsd-i386 hello.go

SET CGO_ENABLED=0
SET GOOS=openbsd
SET GOARCH=amd64
go build -i -o hello-openbsd-amd64 hello.go


:: Windows
SET CGO_ENABLED=0
SET GOOS=windows
SET GOARCH=386
go build -i -o hello-windows-i386.exe hello.go

SET CGO_ENABLED=0
SET GOOS=windows
SET GOARCH=amd64
go build -i -o hello-windows-amd64.exe hello.go

(全文完)

本文转载于:https://blog.csdn.net/panshiqu/article/details/53788067

Tags