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.
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.
Go does not currently support 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.
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.
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.
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" ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With