英文:
Are Golang binaries portable?
问题
假设我是一个主要使用Linux的用户,但我正在使用Go开发一个跨平台的应用程序。我已经搜索过了,但似乎找不到以下问题的解答:
- 如果我在我的amd64 Ubuntu系统上使用
go install
安装一个二进制文件,它是否也能在任何其他人的64位Ubuntu/Debian系统上运行? - 如何使用
go install
构建一个x86_64二进制文件,以便它也可以在32位的Debian类系统上直接运行? - 如果我必须使用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:
- If I
go install
a binary on my amd64 Ubuntu system, will it also work on anyone else's 64-bit Ubuntu/Debian system? - How can I use
go install
to build an x86_64 binary that will also run out-of-the-box on 32-bit Debianlikes? - 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
- 是的,这对于基本上所有为64位Linux编译的二进制文件都是正确的,不仅限于使用Go语言编写的二进制文件(除了Go不依赖的共享库)。
- 在构建之前,您可以设置
GOOS
和GOARCH
环境变量:GOOS=windows GOARCH=386 go build
(或go install
或其他命令)等等。 - 默认情况下,二进制文件将为您正在运行的系统构建,但这并非必需-请参见第2点。
英文:
- 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)
- You can set the
GOOS
andGOARCH
environment variables before building:GOOS=windows GOARCH=386 go build
(orgo install
or whatever), etc - By default a binary will be built for the system you're running, but this isn't necessary - see 2
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论