golang can't compile a static go compiler

178 views
Skip to first unread message

Dave Goehrig

unread,
Sep 28, 2016, 1:58:23 PM9/28/16
to golang-nuts

On x86_64-linux, golang 1.7.1 in cmd/cgo/out.go  lines 248-277 explicitly creates a dynamically linked elf file when compiling via go (but not gccgo, cf line 3359 of cmd/go/build.go.
It appears that there is no way to build a static /bin/go as a result of this code.  Is this correct?

I ask because the environment I'm trying to build go for doesn't support shared libraries or dynamic loading (no ld-linux), and would love to know if there is a way to do it.

Dave

Ian Lance Taylor

unread,
Sep 28, 2016, 2:01:07 PM9/28/16
to Dave Goehrig, golang-nuts
The code you mention in the cgo tool is used when doing a dynamic
link, but not when doing a static link.

If you don't need cgo, set CGO_ENABLED=0 when you build the go tools,
and you will get statically linked binaries.

If you do need cgo, you will to pass -extldflags=-static to tell the
external linker to do a static link.

Ian
Reply all
Reply to author
Forward
0 new messages