golang交叉编译

第三方方案

gox:https://github.com/mitchellh/gox

自带解决方案

版本必须是1.5及以上。

➜  GOOS=windows GOARCH=amd64 go build -ldflags "-w -s" -o web hello_word.go

其中GOOSGOARCH的可选值:

// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package build

const goosList = "aix android darwin dragonfly freebsd hurd illumos js linux nacl netbsd openbsd plan9 solaris windows zos "
const goarchList = "386 amd64 amd64p32 arm armbe arm64 arm64be ppc64 ppc64le mips mipsle mips64 mips64le mips64p32 mips64p32le ppc riscv riscv64 s390 s390x sparc sparc64 wasm "

如果编译arm/android可执行文件:

GOARM=5 GOOS=linux GOARCH=arm go build -ldflags "-w -s"  -o hello hello_word.go

GOARM说明:

  • $GOARM (for arm only; default is auto-detected if building on the target processor, 6 if not)
    This sets the ARM floating point co-processor architecture version the run-time should target. If you are compiling on the target system, its value will be auto-detected.
    • GOARM=5: use software floating point; when CPU doesn’t have VFP co-processor
    • GOARM=6: use VFPv1 only; default if cross compiling; usually ARM11 or better cores (VFPv2 or better is also supported)
    • GOARM=7: use VFPv3; usually Cortex-A cores

If in doubt, leave this variable unset, and adjust it if required when you first run the Go executable. The GoARM page on the Go community wiki contains further details regarding Go’s ARM support.

如果出现编译错误请禁用CGO,CGO_ENABLED=0

来源:https://github.com/golang/go/blob/master/src/go/build/syslist.go
官方说明:https://golang.org/doc/install/source#environment
其他文档:https://gist.github.com/asukakenji/f15ba7e588ac42795f421b48b8aede63
https://dave.cheney.net/2015/08/22/cross-compilation-with-go-1-5

你可能感兴趣的:(Go)