I've seen a bunch of questions here and on other forums where the suggestion is to use -static
or sometimes even -static -static-libgcc
along with the compile arguments. This never works on Alpine, but woks fine on Ubuntu and Fedora.
I wrote a simple hello-world program in C, then had is compiled as gcc -static test.c
. And the resulting binary still lights up ldd
. See,
$ gcc -s test.c -static
$ ldd ./a.out
/lib/ld-musl-x86_64.so.1 (0x7f043eae8000)
$ file ./a.out
./a.out: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, stripped
Running the same on Ubuntu shows:
$ gcc -s test.c -static
$ file ./a.out
./a.out: ELF 64-bit LSB executable, x86-64, version 1 (GNU/Linux), statically linked, for GNU/Linux 3.2.0, BuildID[sha1]=bf6bfa1c78c541ae4e81586bcd050923bca9e34a, stripped
What is the correct and consistent way to static link libc itself on any platform? Is this something to do with the way GCC itself is compiled?