可以使用Go编译器来为其他操作系统分发可执行文件吗?

huangapple go评论92阅读模式
英文:

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

Here's the go-wiki page on building a windows exe on linux.

答案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

huangapple
  • 本文由 发表于 2012年12月20日 15:47:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/13967470.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定