英文:
Why is my Go application not statically linked?
问题
我在使用Go时,本来期望得到一个静态二进制文件。
ubuntu@ugbuildserver:~/gospace$ go build src/runk/runk.go
ubuntu@ugbuildserver:~/gospace$ file runk
runk: ELF 64-bit LSB 可执行文件,x86-64,版本 1 (SYSV),动态链接 (使用共享库),未剥离
ubuntu@ugbuildserver:~/gospace$
有什么关于这个问题的建议吗?
英文:
I had expected a static binary when using Go.
ubuntu@ugbuildserver:~/gospace$ go build src/runk/runk.go
ubuntu@ugbuildserver:~/gospace$ file runk
runk: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), not stripped
ubuntu@ugbuildserver:~/gospace$
Any suggestions for what is wrong?
答案1
得分: 3
Go在使用net
和os/user
等包时仍然会动态链接到一些共享库函数。
Go 1.5进一步减少了net
包的这些要求:https://golang.org/doc/go1.5#net
net
包中的DNS解析器几乎总是使用cgo来访问系统接口。Go 1.5中的一项更改意味着在大多数Unix系统上,DNS解析将不再需要cgo,这简化了在这些平台上的执行。现在,如果系统的网络配置允许,原生的Go解析器就足够了。这个改变的重要影响是每个DNS解析占用一个goroutine而不是一个线程,因此具有多个未完成的DNS请求的程序将消耗更少的操作系统资源。
进阶阅读:http://dominik.honnef.co/posts/2015/06/statically_compiled_go_programs__always__even_with_cgo__using_musl/
英文:
Go still dynamically links to some shared library functions when using packages like net
and os/user
.
Go 1.5 further reduced these requirements for the net
package: https://golang.org/doc/go1.5#net
> The DNS resolver in the net package has almost always used cgo to access the system interface. A change in Go 1.5 means that on most Unix systems DNS resolution will no longer require cgo, which simplifies execution on those platforms. Now, if the system's networking configuration permits, the native Go resolver will suffice. The important effect of this change is that each DNS resolution occupies a goroutine rather than a thread, so a program with multiple outstanding DNS requests will consume fewer operating system resources.
Advanced reading: http://dominik.honnef.co/posts/2015/06/statically_compiled_go_programs__always__even_with_cgo__using_musl/
答案2
得分: 0
我将使用 @DaveCheney 在其他地方提到的答案:
如果你想始终进行静态编译,我建议从源代码安装 Go
env CGO_ENABLED=0 ./all.bash
这将永久禁用 cgo。
英文:
The answer that I'm going with it what @DaveCheney suggested elsewhere:
If you want static compilation always then I recommend installing Go from
source
env CGO_ENABLED=0 ./all.bash
That will disable cgo permanently.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论