英文:
Is it possible to use the Go compiler to distribute a executable file for other os?
问题
我目前正在使用Mac OS X工作,现在我需要构建一个.exe文件,以便Go程序可以在Windows上运行。
所以问题是,如何在MacOS amd64下为Win X86构建可执行文件?或者这是不可能的吗?
英文:
I am currently working on Mac OS X, now I need to build a .exe file so that the Go program can run on windows.
So here is the question, how to build a executable file for Win X86 under MacOS amd64 ? or Is it impossible to do so ?
答案1
得分: 33
如果您不使用CGo而是纯Go,那么这是完全可行和标准的。
首先,您需要在开发计算机上为目标创建Go环境。假设您的Go安装在~/var/go
中,可能是这样的:
cd ~/var/go/src
CGO_ENABLED=0 GOOS=windows GOARCH=386 ./make.bash
然后,您使用正确的GOOS和GOARCH进行编译:
GOOS=windows GOARCH=386 go build -o hello.exe hello.go
这是go-wiki页面上关于在Linux上构建Windows可执行文件的说明。
英文:
If you don't use CGo but pure Go, then it's perfectly doable and standard.
First you have to make the Go environment on your development computer for the targets. Supposing your Go installation is in ~/var/go
, this may be this :
cd ~/var/go/src
CGO_ENABLED=0 GOOS=windows GOARCH=386 ./make.bash
Then you compile with the good GOOS and GOARCH :
GOOS=windows GOARCH=386 go build -o hello.exe hello.go
答案2
得分: 8
我使用了Dave Cheney的教程和相应的shell脚本在Linux上构建了适用于Linux、Windows和OS X的二进制文件(用于我的stressdisk项目)。
这与dystroy描述的方法完全相同,只是提供了逐步说明和一些有用的shell别名。
2021年2月更新
如果你想将某些东西插入到你的CI中,我推荐使用goreleaser - 它可以自动进行跨平台编译,并构建软件包。如果你想看一个例子,可以查看完全使用goreleaser构建的stressdisk发布页面 - 参考配置文件和说明。
英文:
I've used Dave Cheney's tutorial and accompanying shell scripts to build binaries for linux, windows and OS X on linux just fine. (Used by my stressdisk project)
It is exactly the same method as described by dystroy just with step by step instructions and a few helpful shell aliases.
Update Feb 2021
If you want to plug something into your CI then I recommend goreleaser - this can automatically cross compile for a variety of OSes and build packages too. If you want to see an example check out the stressdisk releases page built entirely with goreleaser - see the config and the instructions
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论