I’m using stdio.h , string.h , math.h in a simple math tool written in C.
I hear that -static in gcc does not actually always statically compile libraries (i hear this from Linux-pro jargon which i don’t understand) and I also hear from the same sources that software development for Linux is a pain due to fragmentation and different distributions requiring re-compilation for each one.
Since i’m not doing a GUI or a custom library , or anything too complex, I hypothesise that i dont have to compile again for each distro and statically linking with -static in gcc while compiling on my Ubuntu 20.04 system will create an executable binary compatible with nearly all x86_64 Linux systems. Am I right ? Will it work across at least Debian, Ubuntu , Arch and their derivatives ?
I saw a video of Garry from Android Authority who statically compiled his C program he made on a Raspberry Pi and ran it on Android, so i’m guessing this is possible.
2
Answers
The glibc library still uses some dynamic features even when linked statically. That might work on older distributions, since things like NSS (name service switch) have been around a long time.
If you want to be really static, you should use uclibc or other similar tiny C libraries. But then, if the system used features to override DNS, user IDs (like looking up users in LDAP), home directory locations, etc, they won’t work.
There’s another problem then: kernel support. If you build your static library on a very new kernel, the library might have been built to use system calls from the new version, and those system calls might not exist if you go to an old enough distro. A kernel like 2.4, 2.6 or 3.X would definitely have missing system calls.
From the headers you list, the only dependency on
dlopen
is implied by the mode argument offopen
: If the,ccs=
parameter is used there,fopen
will try todlopen
a gconv module, for character set conversion.Apart from that, none of the functions declared in
<stdio.h>
,<string.h>
,<math.h>
will perform an implicitdlopen
, so static linking should be safe in this particular case.