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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值