• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

golang跨平台编译——go在windows上编译Linux平台的程序(CrossCompilationfromWindow ...

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

 

Go Cross Compilation from Windows to Linux/Ubuntu

 I have GO 1.7 installed on my Windows 10. I created test program and it works perfectly in Windows. Next step is to try to run it on my docker virtual machine with Ubuntu.

 

Answer:

That other question is a bit old (from 2013).

Cross-platform support evolved a lot, all you need to do is change the environment variables (GOARCH and GOOS) and run go build.

Navigate to the folder of the main package, then:

set GOARCH=amd64
set GOOS=linux
go build

You may change the name of the result binary like this:

go build -o "myapp"

Note that in Linux to compile the app for Windows amd64, a single command is enough (in the folder of the main package):

GOOS=windows GOARCH=amd64 go build

This is also confirmed in blog post: Dave Cheney: Cross compilation with Go 1.5:

To cross compile a Go program using Go 1.5 the process is as follows:

  1. set GOOS and GOARCH to be the values for the target operating system and architecture.

  2. run go build -v YOURPACKAGE

Notes

You don't have to, and you shouldn't run go install, as that will compile and install the packages in your GOPATH, which is often not wanted. Doing cross compilation is not for developing / testing your app and packages. It is to produce a single binary which you will run on another system / computer.

go build will not install anything, it will just produce the executable binary. For details, see What does go build build?

Also confirmed in blog post: Dave Cheney: Cross compilation with Go 1.5:

When cross compiling, you should use go build, not go install. This is the one of the few cases where go build is preferable to go install.

The reason for this is go install always caches compiled packages, .a files, into the pkg/directory that matches the root of the source code.

 

————————————————————————————————————————————————————

[转] 原文:https://studygolang.com/articles/15508?fr=sidebar

ubuntu下golang的交叉编译

先安装go1.4.2

wget https://storage.googleapis.com/golang/go1.4.2.linux-amd64.tar.gz

设置环境变量

 GOROOT_BOOTSTRAP=/home/aikangs/app/go1.4.2
 export GOROOT_BOOTSTRAP
 export PATH=${PATH}:${GOROOT_BOOTSTRAP}

最后进入GOROOT/src目录运行你需要编译的平台的代码


# 如果你想在Windows 32位系统下运行
  ~$CGO_ENABLED=0 GOOS=windows GOARCH=386 ./make.bash

# 如果你想在Windows 64位系统下运行
  ~$CGO_ENABLED=0 GOOS=windows GOARCH=amd64 ./make.

# 如果你想在OS X 32位系统下运行
  ~$CGO_ENABLED=0 GOOS=darwin GOARCH=386 ./make.bash

# 如果你想在OS X 64位系统下运行
  ~$CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 ./make.bash

# 如果你想在Linux 32位系统下运行
  ~$CGO_ENABLED=0 GOOS=linux GOARCH=386 ./make.bash

# 如果你想在Linux 64位系统下运行
  ~$CGO_ENABLED=0 GOOS=linux GOARCH=amd64 ./make.bash

最后编译时,进入你的项目目录 运行

# 如果你想在Windows 32位系统下运行
➜  ~$CGO_ENABLED=0 GOOS=windows GOARCH=386 go build main.go

# 如果你想在Windows 64位系统下运行
➜  ~$CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build main.go

# 如果你想在OS X 32位系统下运行
➜  ~$CGO_ENABLED=0 GOOS=darwin GOARCH=386 go build main.go

# 如果你想在OS X 64位系统下运行
➜  ~$CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build main.go

# 如果你想在Linux 32位系统下运行
➜  ~$CGO_ENABLED=0 GOOS=linux GOARCH=386 go build main.go

# 如果你想在Linux 64位系统下运行
➜  ~$CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build main.go

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
go语言从例子开始之Example16.函数递归发布时间:2022-07-10
下一篇:
$2,800 VS $25,000: Where will Bitcoin price go?发布时间:2022-07-10
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap