Compiling with -static-libgcc -static-libstdc++ still results in dynamic dependency on libc.so

36,048

Solution 1

GNU libc is not designed to be statically linked. Important functions, e.g. gethostbyname and iconv, will malfunction or not work at all in a static binary. Arguably even worse, under some conditions a static binary will attempt to dynamically open and use libc.so.6, even though the whole point of static linkage is to avoid such dependencies.

You should compile your program against uClibc or musl libc instead.

(This has been true for at least 15 years.)

Solution 2

First be aware that static linking of libc might not improve portability of your program, as libc might depend on other parts of your system e.g. kernel version.

If you want to try complete static linking just using -static should the trick. Provided that there are static versions of all used libraries installed.

You can check if your program has only linked static libraries by using:

ldd binary_name

EDIT:

Another way that provides useful information for debugging this problem would be to add --verbose to your linker flags.

Share:
36,048

Related videos on Youtube

Claudiu
Author by

Claudiu

Graduated from Brown University. E-mail: [email protected]

Updated on May 22, 2020

Comments

  • Claudiu
    Claudiu almost 4 years

    I'm trying to make an executable that's as portable as possible. After removing a few dependencies, I came across the following when running the binary on another system:

    /lib/x86_64-linux-gnu/libm.so.6: version `GLIBC_2.15' not found (required by foob)
    /lib/x86_64-linux-gnu/libc.so.6: version `GLIBC_2.15' not found (required by foob)
    /lib/x86_64-linux-gnu/libc.so.6: version `GLIBC_2.14' not found (required by foob)
    

    I'd prefer my binary not to require the user to upgrade their version of libc, so I'd like to remove this dependency as well.

    The linker flags that produced the above binary already included -static-libgcc -static-libstdc++. How come the binary still requires on the shared libc.so.6?

    I tried adding the -static flag as well, however when I try to run that binary the result is very strange:

    $ ls -l foob
    -rwxr-xr-x 1 claudiu claudiu 13278191 Oct 10 13:03 foob
    $ ./foob
    bash: ./foob: No such file or directory
    

    What to do?

    EDIT:

    $ file foob
    foob: ELF 64-bit LSB  executable, x86-64, version 1 (GNU/Linux), dynamically linked (uses shared libs), for GNU/Linux 2.6.24, BuildID[sha1]=5adee9a598b9261a29f1c7b0ffdadcfc72197cd7, not stripped
    $ strace -f ./foob
    execve("./foob", ["./foob"], [/* 64 vars */]) = -1 ENOENT (No such file or directory)
    write(2, "strace: exec: No such file or di"..., 40strace: exec: No such file or directory
    ) = 40
    exit_group(1)                           = ?
    +++ exited with 1 +++
    

    Interestingly, if I ldd the version without -static, it has two less entries than the version with -static, namely:

    libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f4f420c1000)
    libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f4f41636000)
    
    • ggiroux
      ggiroux over 9 years
      can you show strace -f -v for that static binary? What about "file foob" ? You may want to include the specific source and target (linux?) distributions + architectures you're using.
    • Admin
      Admin over 9 years
      As suggested, run file foob. Make sure it's a 64-bit executable.
    • Hack Saw
      Hack Saw over 9 years
      I just compiled a simplish program with only the -static flag: "gcc -o ts --std=c99 --static test.c". Works fine.
    • nos
      nos over 9 years
      run strace -f ./foob so we can see what the "No such file or directory" is about.
    • Chris Stratton
      Chris Stratton over 9 years
      possible duplicate of Linux static linking is dead? - static linking is well known to not be completely workable on a typical modern desktop linux
    • Claudiu
      Claudiu over 9 years
      @nos: Done, see edited question.
    • Z boson
      Z boson over 9 years
      @ChrisStratton, I have never had a problem with -static on multiple linux systems. I may have had to install some static libraries but that was it.
    • Z boson
      Z boson over 9 years
      What's your OS and version ? Ubuntu 14.04? Redhat? What kind of libc are you using and what's it's version? I'm using Ubuntu 14.04 and Eglibc 2.19 and static linking works fine for me.
    • Claudiu
      Claudiu over 9 years
      @Zboson: I'm using Ubuntu. The libc version is "Ubuntu EGLIBC 2.19-0ubuntu6"
    • Z boson
      Z boson over 9 years
      @Claudiu, have you tried a simple hello world? See my updated answer.
    • Chris Stratton
      Chris Stratton over 9 years
      objdump -T on your result, and then grepping for the "UND" will let you see what specific functionality is still being pulled from dynamic libraries - likely that which is simply no longer supported with a static alternative.
  • crash
    crash over 9 years
    Well that last error is really strange. -static does exactly what he wants to do. And -static-libgcc -static-libstdc++ can be left out, but that shouldn't change anything.
  • Chris Stratton
    Chris Stratton over 9 years
    Static linking is well known to not be completely workable on a typical modern desktop linux.
  • Janus Troelsen
    Janus Troelsen about 9 years
    @ChrisStratton: Only if you use glibc!
  • Chris Stratton
    Chris Stratton about 9 years
    Sure, but that is what the poster is using. And what else would you use on a modern desktop linux?
  • sholsapp
    sholsapp over 8 years
    Hi @zwol, can you help me understand why it's best to link against uClibc or musl libc instead? If one's goal is to statically link against libc to accomplish customizing libc (e.g., memory allocation behavior) for the shared library but not the target application, would one still follow this advice? See stackoverflow.com/questions/35072500/….
  • zwol
    zwol over 8 years
    @sholsapp That depends on which C library functions are used by the shared library in question. I can't give you any more specific answer without knowing a great deal more about what your shared library does, what it needs out of the C library, and why you want to do this rather peculiar thing in the first place.
  • zhaorufei
    zhaorufei about 7 years
    I just tried the following command whose warning message confirmed that: gcc-4.8 --static ... netcat.c warning: Using 'gethostbyaddr' in statically link applications requires at runtime the shared libraries from the glibc version used for linking
  • shevy
    shevy almost 4 years
    Musl these days for example, Chris Stratton. Voidlinux uses it. It's quite interesting - I don't think it is competitive feature-wise, but more competition is a good thing IMO. Also note that "partial" static linking is fine; I keep a static version of busybox around, but you can statically compile make, sed, grep, coreutils, etc... and these work. Some folks even managed with GCC; I haven't yet. It can be useful if you manage to break a library somehow (and there are ways how one can do this accidentally).