Following this blog post I tried to compile stdlib.so to link other code with it. Unfortunately, stdlib.so itself is a dynamically linked binary:
# ldd /usr/local/go/pkg/linux_amd64_dynlink/libstd.so
ldd: warning: you do not have execution permission for `/usr/local/go/pkg
/linux_amd64_dynlink/libstd.so'
linux-vdso.so.1 (0x00007ffd611d7000)
libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f82bb2c5000)
libc.so.6 => /lib64/libc.so.6 (0x00007f82baf04000)
/lib64/ld-linux-x86-64.so.2 (0x000055e99ffd7000)
I tried building it with different flags, but that doesn't seem to do anything / not the result I expected. I.e. I tried:
go install -x -buildmode=shared --ldflags '-extldflags "-static"' std
for example, but that translates into a wrong set of compiling / linking instructions, so libstd.so isn't produced.
I want to do this because I have several Go programs I need to build and to put them into Docker images. Statically linking a Go executable takes a long time (roughly a minute, give or take 20 seconds), so I was hoping to compile all the infrequently changing dependencies into a shared library, while only recompiling the actual code of the application (that would decrease the compilation time from about 10 minutes to few seconds). I would also prefer not to have to base Docker images on some Linux image (that would make them 600+ MB big vs 60 MB of only Go code).
Here is another blog post about statically compiling Go programs that use CGo: Linking Golang Statically
It says:
But for that example, we just have to add the '-static' flag to gcc (and ensure that glibc-static package is available).
meaning that your C libraries need to be statically linked as well.