Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to create a statically linked golang executable with go 1.5+

Tags:

go

rkt

golang version < 1.5 - there are plenty of static linking examples, posts and recipes. What about >= 1.5? (google search has returned no useful results for my search terms.) Anyone have any recommendations on how to produce a statically linked binary that can be executed inside a basic rkt (from CoreOS) container?

my go:

$go version
go version go1.5 linux/amd64

when I try to run my container:

sudo rkt --insecure-skip-verify run /tmp/FastBonusReport.aci

I get:

[38049.477658] FastBonusReport[4]: Error: Unable to open "/lib64/ld-linux-x86-64.so.2": No such file or directory

suggesting that the executable in the container is depending on this lib and hence not static.

my manifest looks like:

cat <<EOF > /tmp/${myapp}/manifest
{
    "acKind": "ImageManifest",
    "acVersion": "0.9.0",
    "name": "${lowermyapp}",
    "labels": [
        {"name": "os", "value": "linux"},
        {"name": "arch", "value": "amd64"}
    ],
    "app": {
        "exec": [
            "/bin/${myapp}"
        ],
        "user": "0",
        "group": "0"
    }
}
EOF

my command line to build the binary looks like:

go build ${myapp}.go

This article has a few examples golang < 1.5. And then there is this getting started article on the CoreOS site.

like image 446
Richard Avatar asked Oct 13 '15 21:10

Richard


People also ask

Are Go programs statically linked?

Well, Go code is statically linked, but the runtime may try to dynamically load libc for DNS resolving. Use of cgo of course drastically change everything. By default, nowadays the toolchain also supports generating dynamic libraries.

Does Go support dynamic linking?

Go does not currently support dynamic linking.

What is static linking and dynamic linking?

Definition. Static linking is the process of copying all library modules used in the program into the final executable image. In contrast, dynamic linking is the process of loading the external shared libraries into the program and then binds those shared libraries dynamically to the program.

What is a statically linked binary?

Static linking is the result of the linker copying all library routines used in the program into the executable image. This may require more disk space and memory than dynamic linking, but is both faster and more portable, since it does not require the presence of the library on the system where it is run.


2 Answers

I hate to answer my own question. The comments have been correct CGO_ENABLED=0 go build ./... seems to have have done the trick.

While it was not part of the original question, once the program started executing in the rkt container it could not perform a proper DNS request. So there must be something else going on too.

like image 197
Richard Avatar answered Oct 25 '22 03:10

Richard


Static linking:

Go 1.5:

go build -ldflags "-extldflags -static" ...

With Go 1.6 I had to use:

go build -ldflags "-linkmode external -extldflags -static" ...
like image 24
Bryan Avatar answered Oct 25 '22 04:10

Bryan