Duke Dougal
Duke Dougal

Reputation: 26326

Why is my Go application not statically linked?

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?

Upvotes: 4

Views: 552

Answers (2)

Duke Dougal
Duke Dougal

Reputation: 26326

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.

Upvotes: 0

elithrar
elithrar

Reputation: 24260

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/

Upvotes: 4

Related Questions