40

I have included the header netdb.h, where getaddrinfo is included, but gcc issues this warning:

warning: Using 'getaddrinfo' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking

gcc -m32 -static -s -O2 -std=c99 -D_POSIX_C_SOURCE=200112L myprogram.c

How can I statically compile whatever file is missing ?

Possible solutions:

  1. It could be that the glibc installation is missing the corresponding object file necessary for static compilation. If that is the case, create the corresponding object file and link it at compilation.

  2. Try EGLIBC instead of glibc.

  3. I succesfully compiled my program with dietlibc which compiled without any errors plus the resulting binary was much smaller than what glibc makes.

Ralf Stubner
  • 26,263
  • 3
  • 40
  • 75
Neeladri Vishweswaran
  • 1,695
  • 5
  • 22
  • 40

4 Answers4

28

glibc uses libnss to support a number of different providers for address resolution services. Unfortunately, you cannot statically link libnss, as exactly what providers it loads depends on the local system's configuration.

bdonlan
  • 224,562
  • 31
  • 268
  • 324
  • 12
    Meanwhile in version 2.20 there is the `--enable-static-nss` flag of `configure` which seems to do exactly this. Note that static linking introduces some disadvantages (see @pixelbeat's answer and the comments made to it). – Kalle Richter Sep 20 '14 at 18:18
  • 3
    I found a solution that uses musl instead of glibc. See my answer below: http://stackoverflow.com/a/37245653/425758 – zhanxw May 16 '16 at 19:18
11

You can use musl library to replace glibc. To use musl, you can either install it and build your software using musl-gcc, or you can use a Linux distribution that uses musl, e.g. Alpine Linux.

In my case, to save time, I chose Alpine Linux to build my program (https://github.com/zhanxw/rvtests), as I don't want to build multiple compilers (gcc, g++ and gfortran).

Matthieu
  • 2,736
  • 4
  • 57
  • 87
zhanxw
  • 3,159
  • 3
  • 34
  • 32
  • Do you know of a solution for C++? – JC1 Oct 12 '17 at 23:38
  • Thanks. Do you know if binaries compiled in Alpine are portable? I am building a binary to run as a lambda on Amazon AWS and I think compiling it on CentOS (the OS used for lambdas) is highly recommended. – JC1 Oct 13 '17 at 16:46
  • 1
    @JC1 Yes, I compiled on Alpine Linux and can run it on Ubuntu 16.04 and CentOS7. – zhanxw Oct 14 '17 at 18:50
2

I think certain features are dependent on the dynamic loader to work things out at run time. static linking is no longer practical unfortunately http://people.redhat.com/drepper/no_static_linking.html

pixelbeat
  • 30,615
  • 9
  • 51
  • 60
  • 3
    I think it depends on what you need to do. Static compiling is a valid solution if binary-portability is a must. – Neeladri Vishweswaran May 07 '10 at 08:49
  • 1
    Well the above mentions that some things are not possible if you statically link. I've some notes on binary compatibility here: http://www.pixelbeat.org/programming/linux_binary_compatibility.html – pixelbeat May 07 '10 at 13:08
0

Another solution is to find the missing files and use a symlink to the directory that is missing the files. For instance.

In my particular scenario, I was missing /usr/lib/x86_64-linux-gnu/libm-2.31.a but we found that the files were actually located at /usr/x86_64-linux-gnu/lib/libm-2.31.a instead (the lib is shifted). So creating a symlink for that missing files did the trick.

ln -s /usr/x86_64-linux-gnu/lib/* /usr/lib/x86_64-linux-gnu/