golang交叉编译
weidao · · 1625 次点击 · · 开始浏览本地开发环境是os x 服务器是linux,所以需要编译linux用的go程序,方法如下:
各平台的GOOS和GOARCH参考
OS ARCH OS versionv
linux 386 / amd64 / arm >= Linux 2.6
darwin 386 / amd64 OS X (Snow Leopard + Lion)
freebsd 386 / amd64 >= FreeBSD 7
windows 386 / amd64 >= Windows 2000
$ cd /usr/local/go/src
$ sudo CGO_ENABLED=0 GOOS=linux GOARCH=amd64 ./make.bash
这里并不是重新编译Go,因为安装Go的时候,只是编译了本地系统需要的东西;而需要跨平台交叉编译,需要在Go中增加对其他平台的支持。所以,有 ./make.bash 这么一个过程。
执行结果类似如下:
sudo CGO_ENABLED=0 GOOS=linux GOARCH=amd64 ./make.bash
Password:
Building C bootstrap tool.
cmd/dist
Building compilers and Go bootstrap tool for host, darwin/amd64.
lib9
libbio
libmach
misc/pprof
cmd/addr2line
cmd/cov
cmd/nm
cmd/objdump
cmd/pack
cmd/prof
cmd/cc
......
pkg/text/template/parse
pkg/text/template
pkg/go/doc
pkg/go/build
cmd/go
pkg/runtime (linux/amd64)
Building packages and commands for host, darwin/amd64.
runtime
errors
sync/atomic
unicode
unicode/utf8
math
sync
unicode/utf16
crypto/subtle
io
syscall
..........
net/rpc/jsonrpc
testing/iotest
testing/quick
Building packages and commands for linux/amd64.
runtime
errors
sync/atomic
unicode
unicode/utf8
math
sync
unicode/utf16
........
testing
net/rpc/jsonrpc
testing/iotest
testing/quick
Installed Go for linux/amd64 in /usr/local/go
Installed commands in /usr/local/go/bin
到源代码目录下执行:
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build sina.go
不带前面参数的 go build 只是编译出开发环境适用的执行文件。
有疑问加站长微信联系(非本文作者)
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
关注微信- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码` - 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传
收入到我管理的专栏 新建专栏
本地开发环境是os x 服务器是linux,所以需要编译linux用的go程序,方法如下:
各平台的GOOS和GOARCH参考
OS ARCH OS versionv
linux 386 / amd64 / arm >= Linux 2.6
darwin 386 / amd64 OS X (Snow Leopard + Lion)
freebsd 386 / amd64 >= FreeBSD 7
windows 386 / amd64 >= Windows 2000
$ cd /usr/local/go/src
$ sudo CGO_ENABLED=0 GOOS=linux GOARCH=amd64 ./make.bash
这里并不是重新编译Go,因为安装Go的时候,只是编译了本地系统需要的东西;而需要跨平台交叉编译,需要在Go中增加对其他平台的支持。所以,有 ./make.bash 这么一个过程。
执行结果类似如下:
sudo CGO_ENABLED=0 GOOS=linux GOARCH=amd64 ./make.bash
Password:
Building C bootstrap tool.
cmd/dist
Building compilers and Go bootstrap tool for host, darwin/amd64.
lib9
libbio
libmach
misc/pprof
cmd/addr2line
cmd/cov
cmd/nm
cmd/objdump
cmd/pack
cmd/prof
cmd/cc
......
pkg/text/template/parse
pkg/text/template
pkg/go/doc
pkg/go/build
cmd/go
pkg/runtime (linux/amd64)
Building packages and commands for host, darwin/amd64.
runtime
errors
sync/atomic
unicode
unicode/utf8
math
sync
unicode/utf16
crypto/subtle
io
syscall
..........
net/rpc/jsonrpc
testing/iotest
testing/quick
Building packages and commands for linux/amd64.
runtime
errors
sync/atomic
unicode
unicode/utf8
math
sync
unicode/utf16
........
testing
net/rpc/jsonrpc
testing/iotest
testing/quick
Installed Go for linux/amd64 in /usr/local/go
Installed commands in /usr/local/go/bin
到源代码目录下执行:
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build sina.go
不带前面参数的 go build 只是编译出开发环境适用的执行文件。