适用于所有平台的Go二进制文件。

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

Go binary file for all platform

问题

我有一个.go文件,并使用Mac上的go build命令生成了二进制文件。有没有一种方法可以构建在Windows、Linux和iOS上运行的二进制文件?

我知道我们可以通过更改GOOS和GOARCH参数为每个平台构建二进制文件,但我希望有一个单独的go二进制文件可以在所有平台上运行。请帮我解决这个问题。

提前感谢!

英文:

I have a .go file and produced the binary file using go build command from Mac. Is there a way to build a binary file which runs in windows,linux,IOS ?

I am aware we can build binary file for each of them by changing the GOOS,GOARCH params but i would like to have a single go binary file which should run in all the platforms . Please help me out of this.

Thanks in advance

答案1

得分: 3

不,无论是Go还是其他编程语言,都不可能直接生成适用于不同平台和架构的可执行文件。

然而,有一些工具可以进行交叉编译,例如 gox,它可以为你进行交叉编译。

这篇文章 post 解释了如何使用Golang进行交叉编译(目前非常容易)。

此外,Unix StackExchange 上有一个问题,https://unix.stackexchange.com/a/298283/177527,解释了为什么不同的架构需要不同的二进制文件:

原因是代码被编译为特定架构的机器码,而不同处理器系列之间的机器码非常不同(例如,ARM和x86非常不同)。

二进制文件还依赖于操作系统,如此处所述 https://softwareengineering.stackexchange.com/a/251255

二进制格式:可执行文件必须符合特定的二进制格式,以便操作系统能够正确加载、初始化和启动程序。Windows主要使用可移植可执行文件格式(Portable Executable format),而Linux使用ELF格式。

系统API:程序可能使用库,这些库必须存在于执行系统中。如果程序使用了Windows API的函数,则无法在Linux上运行。在Unix世界中,中央操作系统API已经标准化为POSIX:只使用POSIX函数的程序将能够在任何符合POSIX标准的Unix系统上运行,例如Mac OS X和Solaris。

英文:

No, it is not at all possible in Go or any other programming language (the executable is necessarily tailored to individual platforms and architectures).

However, to cross-compile, some tools do exist which do the cross compiling for you.

This post helps explain how to cross compile with Golang (which is pretty easy at this point).


There's also a Unix StackExchange question, https://unix.stackexchange.com/a/298283/177527, which explains why different architectures require different binaries:

> The reason is because the code is compiled to machine code for a specific architecture, and machine code is very different between most processor families (ARM and x86 for instance are very different).

The binary also depends on the OS, as explained here https://softwareengineering.stackexchange.com/a/251255:

> Binary Format: The executable has to conform to a certain binary format, which allows the operating system to correctly load, initialize, and start the program. Windows mainly uses the Portable Executable format, while Linux uses ELF.
>
> System APIs: The program may be using libraries, which have to be present on the executing system. If a program uses functions from Windows APIs, it can't be run on Linux. In the Unix world, the central operating system APIs have been standardized to POSIX: a program using only the POSIX functions will be able to run on any conformant Unix system, such as Mac OS X and Solaris.

答案2

得分: 1

对于 Mac(而不是 Windows),您可以使用类似 randall77/makefat 的工具来关联交叉编译,生成一个“通用二进制文件”,该文件可以在任何支持其中一个输入可执行文件的架构上运行。

这个功能目前已经在 goreleaser/goreleaser PR 2572 中实现,这意味着整个过程将完全自动化。

适用于所有平台的Go二进制文件。

英文:

For Mac (not Windows), you can associate cross-compilation with a tool like randall77/makefat to generate a "universal binary", which will run on any architecture supported by one of the input executables.

This is currently implemented in goreleaser/goreleaser PR 2572, which means the process would be completely automated.

适用于所有平台的Go二进制文件。

huangapple
  • 本文由 发表于 2017年1月18日 00:31:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/41702246.html
匿名

发表评论

匿名网友

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

确定