Golang二进制文件是否可移植?

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

Are Golang binaries portable?

问题

假设我是一个主要使用Linux的用户,但我正在使用Go开发一个跨平台的应用程序。我已经搜索过了,但似乎找不到以下问题的解答:

  1. 如果我在我的amd64 Ubuntu系统上使用go install安装一个二进制文件,它是否也能在任何其他人的64位Ubuntu/Debian系统上运行?
  2. 如何使用go install构建一个x86_64二进制文件,以便它也可以在32位的Debian类系统上直接运行?
  3. 如果我必须使用Windows来制作一个在Windows上运行的二进制文件,如何确保即使我的Windows系统是64位的,可执行文件也将构建为x86_64?

我的问题实际上归结为,“Go的链接器/编译器有多静态/可移植?”

英文:

Suppose I'm a primarily Linux user, but I'm developing an application in Go that I want to be cross platform. I've searched around, but I can't seem to find information to absolve the following:

  1. If I go install a binary on my amd64 Ubuntu system, will it also work on anyone else's 64-bit Ubuntu/Debian system?
  2. How can I use go install to build an x86_64 binary that will also run out-of-the-box on 32-bit Debianlikes?
  3. If I must use Windows to make a binary which will run on Windows, how can I also ensure that even if my Windows system is 64-bit the executable will be built for x86_64?

My questions in effect boil down to, "how static/portable is go's linker/compiler?"

答案1

得分: 50

  1. 是的,这对于基本上所有为64位Linux编译的二进制文件都是正确的,不仅限于使用Go语言编写的二进制文件(除了Go不依赖的共享库)。
  2. 在构建之前,您可以设置GOOSGOARCH环境变量:GOOS=windows GOARCH=386 go build(或go install或其他命令)等等。
  3. 默认情况下,二进制文件将为您正在运行的系统构建,但这并非必需-请参见第2点。
英文:
  1. Yes it will; this is true of basically all binaries compiled for 64-bit Linux, not just those written in Go (except for shared libraries, which Go doesn't rely on)
  2. You can set the GOOS and GOARCH environment variables before building: GOOS=windows GOARCH=386 go build (or go install or whatever), etc
  3. By default a binary will be built for the system you're running, but this isn't necessary - see 2

huangapple
  • 本文由 发表于 2015年11月23日 21:44:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/33872612.html
匿名

发表评论

匿名网友

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

确定