golang 跨平台编译——go 在windows上编译Linux平台的程序(Cross Compilation from Windows to Linux/Ubuntu)
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:
set
GOOS
andGOARCH
to be the values for the target operating system and architecture.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
, notgo install
. This is the one of the few cases wherego build
is preferable togo install
.The reason for this is
go install
always caches compiled packages,.a
files, into thepkg/
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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现