go在各个平台交叉编译的介绍

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

Mac 下的交叉编译

Mac 下编译 LinuxWindows 64 位可执行程序

CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build main.go 
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build main.go

Linux 下的交叉编译

Linux 下编译 MacWindows 64位可执行程序

CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build main.go 
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build main.go

Windows 下的交叉编译

Windows 下编译 MacLinux 64位可执行程序

SET CGO_ENABLED=0 
SET GOOS=darwin 
SET GOARCH=amd64 
go build main.go 
SET CGO_ENABLED=0 
SET GOOS=linux 
SET GOARCH=amd64 
go build main.go

参数解释

GOOS:目标平台的操作系统(darwin、freebsd、linux、windowsGOARCH:目标平台的体系架构(386、amd64、arm) 交叉编译不支持 CGO 所以要禁用它

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

获取go已经支持的平台

To execute this code go tool dist list,you will get a list that all supported platforms.

获取结果如下:

Husee@Jermine-PC MINGW64 ~/Desktop
$ go tool dist list
android/386
android/amd64
android/arm
android/arm64
darwin/386
darwin/amd64
darwin/arm
darwin/arm64
dragonfly/amd64
freebsd/386
freebsd/amd64
freebsd/arm
linux/386
linux/amd64
linux/arm
linux/arm64
linux/mips
linux/mips64
linux/mips64le
linux/mipsle
linux/ppc64
linux/ppc64le
linux/s390x
nacl/386
nacl/amd64p32
nacl/arm
netbsd/386
netbsd/amd64
netbsd/arm
openbsd/386
openbsd/amd64
openbsd/arm
plan9/386
plan9/amd64
plan9/arm
solaris/amd64
windows/386
windows/amd64

go对各个操作系统和指令集的支持详情如下

Go (Golang) GOOS and GOARCH

All of the following information is based on go version go1.8.3 darwin/amd64.

A list of valid GOOS values

(Bold = supported by go out of the box, ie. without the help of a C compiler, etc.)

  • android
  • darwin
  • dragonfly
  • freebsd
  • linux
  • nacl
  • netbsd
  • openbsd
  • plan9
  • solaris
  • windows
  • zos

A list of valid GOARCH values

(Bold = supported by go out of the box, ie. without the help of a C compiler, etc.)

  • 386
  • amd64
  • amd64p32
  • arm
  • armbe
  • arm64
  • arm64be
  • ppc64
  • ppc64le
  • mips
  • mipsle
  • mips64
  • mips64le
  • mips64p32
  • mips64p32le
  • ppc
  • s390
  • s390x
  • sparc
  • sparc64

A list of valid 32-bit GOARCH values

(Bold = supported by go out of the box, ie. without the help of a C compiler, etc.)

  • 386
  • amd64p32
  • arm
  • armbe
  • mips
  • mipsle
  • mips64p32
  • mips64p32le
  • ppc
  • s390
  • sparc

A list of valid 64-bit GOARCH values

(Bold = supported by go out of the box, ie. without the help of a C compiler, etc.)

  • amd64
  • arm64
  • arm64be
  • ppc64
  • ppc64le
  • mips64
  • mips64le
  • s390x
  • sparc64

A list of GOOS/GOARCH supported by go out of the box

  • darwin/386
  • darwin/amd64
  • dragonfly/amd64
  • freebsd/386
  • freebsd/amd64
  • freebsd/arm
  • linux/386
  • linux/amd64
  • linux/arm
  • linux/arm64
  • linux/ppc64
  • linux/ppc64le
  • linux/mips
  • linux/mipsle
  • linux/mips64
  • linux/mips64le
  • linux/s390x
  • nacl/386
  • nacl/amd64p32
  • nacl/arm
  • netbsd/386
  • netbsd/amd64
  • netbsd/arm
  • openbsd/386
  • openbsd/amd64
  • openbsd/arm
  • plan9/386
  • plan9/amd64
  • plan9/arm
  • solaris/amd64
  • windows/386
  • windows/amd64

A list of 32-bit GOOS/GOARCH supported by go out of the box

  • darwin/386
  • freebsd/386
  • freebsd/arm
  • linux/386
  • linux/arm
  • linux/mips
  • linux/mipsle
  • nacl/386
  • nacl/amd64p32
  • nacl/arm
  • netbsd/386
  • netbsd/arm
  • openbsd/386
  • openbsd/arm
  • plan9/386
  • plan9/arm
  • windows/386

A list of 64-bit GOOS/GOARCH supported by go out of the box

  • darwin/amd64
  • dragonfly/amd64
  • freebsd/amd64
  • linux/amd64
  • linux/arm64
  • linux/ppc64
  • linux/ppc64le
  • linux/mips64
  • linux/mips64le
  • linux/s390x
  • netbsd/amd64
  • openbsd/amd64
  • plan9/amd64
  • solaris/amd64
  • windows/amd64

Support Grid

a m d f l c n o p s w z
386 A O O O O O O O O
amd64 A O O O O O O O O O
amd64p32 O
arm A B O O O O O O
armbe
arm64 A C O
arm64be
ppc64 O
ppc64le O
mips O
mipsle O
mips64 O
mips64le O
mips64p32
mips64p32le
ppc
s390
s390x O
sparc
sparc64
Key

a = android, m = darwin (macos), d = dragonfly, f = freebsd, l = linux, c = nacl, n = netbsd, o = openbsd, p = plan9, s = solaris, w = windows, z = zos

(blank): Unsupported

O: Supported

A:

warning: unable to find runtime/cgo.a
/usr/local/go/pkg/tool/darwin_amd64/link: running clang failed: exit status 1
ld: unknown option: -z
clang: error: linker command failed with exit code 1 (use -v to see invocation)

B:

warning: unable to find runtime/cgo.a
/usr/local/go/pkg/tool/darwin_amd64/link: running clang failed: exit status 1
ld: warning: ignoring file /var/folders/dd/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/T/go-link-xxxxxxxxx/go.o, file was built for armv7 which is not the architecture being linked (x86_64): /var/folders/dd/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/T/go-link-xxxxxxxxx/go.o
Undefined symbols for architecture x86_64:
  "_main", referenced from:
     implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

C:

warning: unable to find runtime/cgo.a
/usr/local/go/pkg/tool/darwin_amd64/link: running clang failed: exit status 1
ld: warning: ignoring file /var/folders/dd/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/T/go-link-xxxxxxxxx/go.o, file was built for unsupported file format ( 0xCF 0xFA 0xED 0xFE 0x0C 0x00 0x00 0x01 0x00 0x00 0x00 0x00 0x01 0x00 0x00 0x00 ) which is not the architecture being linked (x86_64): /var/folders/dd/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/T/go-link-xxxxxxxxx/go.o
Undefined symbols for architecture x86_64:
  "_main", referenced from:
     implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Source 1

main.go
1package main
2
3func main() {}
make.sh
 1#!/bin/sh
 2
 3os_archs=()
 4
 5# Reference:
 6# https://github.com/golang/go/blob/master/src/go/build/syslist.go
 7for goos in android darwin dragonfly freebsd linux nacl netbsd openbsd plan9 solaris windows zos
 8do
 9    for goarch in 386 amd64 amd64p32 arm armbe arm64 arm64be ppc64 ppc64le mips \
10        mipsle mips64 mips64le mips64p32 mips64p32le ppc s390 s390x sparc sparc64
11    do
12        GOOS=${goos} GOARCH=${goarch} go build -o /dev/null main.go >/dev/null 2>&1
13        if [ $? -eq 0 ]
14        then
15            os_archs+=("${goos}/${goarch}")
16        fi
17    done
18done
19
20for os_arch in "${os_archs[@]}"
21do
22    echo ${os_arch}
23done

Source 2

main.go
1package main
2
3const (
4	hello uint = 0xfedcba9876543210
5)
6
7func main() {}
make.sh
 1#!/bin/bash
 2
 3# Reference:
 4# https://github.com/golang/go/blob/master/src/go/build/syslist.go
 5os_archs=(
 6    darwin/386
 7    darwin/amd64
 8    dragonfly/amd64
 9    freebsd/386
10    freebsd/amd64
11    freebsd/arm
12    linux/386
13    linux/amd64
14    linux/arm
15    linux/arm64
16    linux/ppc64
17    linux/ppc64le
18    linux/mips
19    linux/mipsle
20    linux/mips64
21    linux/mips64le
22    linux/s390x
23    nacl/386
24    nacl/amd64p32
25    nacl/arm
26    netbsd/386
27    netbsd/amd64
28    netbsd/arm
29    openbsd/386
30    openbsd/amd64
31    openbsd/arm
32    plan9/386
33    plan9/amd64
34    plan9/arm
35    solaris/amd64
36    windows/386
37    windows/amd64
38)
39
40os_archs_32=()
41os_archs_64=()
42
43for os_arch in "${os_archs[@]}"
44do
45    goos=${os_arch%/*}
46    goarch=${os_arch#*/}
47    GOOS=${goos} GOARCH=${goarch} go build -o /dev/null main.go >/dev/null 2>&1
48    if [ $? -eq 0 ]
49    then
50        os_archs_64+=(${os_arch})
51    else
52        os_archs_32+=(${os_arch})
53    fi
54done
55
56echo "32-bit:"
57for os_arch in "${os_archs_32[@]}"
58do
59    printf "\t%s\n" "${os_arch}"
60done
61echo
62
63echo "64-bit:"
64for os_arch in "${os_archs_64[@]}"
65do
66    printf "\t%s\n" "${os_arch}"
67done
68echo