go: cmd/go: With Go 1.4, cannot statically link a program which imports net

In Go 1.3.x, it was possible to statically link a program which imports the net package. This is important when building programs for cut-down environments, for example busybox.

The problem can be reproduced on Ubuntu as reported by Vasiliy Tolstov:

package main 
import "net" 
func main() { 
        _, _ = net.ResolveIPAddr("ip", "google.com") 
} 

It is possible to statically link this program in Go 1.3.x by issuing:

CGO_ENABLED=0 go build -a -x

but with Go 1.4, the same technique creates the following runtime dependencies:

linux-vdso.so.1 (0x00007fff603fe000) 
libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f3bd0608000) 
libc.so.6 => /lib64/libc.so.6 (0x00007f3bd0260000) 
/lib64/ld-linux-x86-64.so.2 (0x00007f3bd0825000) 

About this issue

  • Original URL
  • State: closed
  • Created 10 years ago
  • Comments: 26 (12 by maintainers)

Commits related to this issue

Most upvoted comments

CGO_ENABLED=0 go build -a -installsuffix cgo -ldflags '-s' src/myapp/myapp.go

Go 1.5.1

LGTM

If you want static compilation always then I recommend installing Go from source

env CGO_ENABLED=0 ./all.bash

That will disable cgo permanently.

On Tue, Oct 20, 2015 at 4:55 PM, dukedougal notifications@github.com wrote:

I’m really puzzled about this. The primary reason I built my app was for static linking which I thought was the default behaviour and now I find that forever more I’ll need to use what seems like a kludge to compile:

CGO_ENABLED=0 go build -a -installsuffix cgo -ldflags ‘-s’ src/myapp/myapp.go

I’m using Go 1.5.1

— Reply to this email directly or view it on GitHub https://github.com/golang/go/issues/9344#issuecomment-149441532.