3

I have the following problem, which seems not to have a nice solution.

For example, I have an CLI utility which uses libtiff and libX11. I want to produce two versions of this utility: dynamically linked and statically linked (with as many dependences compiled-in as possible).

For dynamic linking everything works as a charm: one need to specify -ltiff -lX11 and linker will produce a nice executable referring libtiff.so and libX11.so.

The situation becomes worse for static linking. When I use the following command:

g++ -static-libgcc -Wl,-static -o my.out *.o -lc -ltiff -lX11

it ends with missed symbols in libtiff.a and libX11.a. OK, I can put all libraries they depend on into row:

g++ -static-libgcc -Wl,-static -o my.out *.o -lc -ltiff -ljpeg -lz -lX11 -lXau -lxcb -lXdmcp

but is there any tool that makes this discovery for me? Can libtool help here (I see /usr/lib/libtiff.la but no /usr/lib/libX11.la)? Can somebody provide a basic example for libtool please? The situation is critical, if on some platform libtiff provides a narrower functionality and does not link to libjpeg which will not be available on that platform at all, so the above command for linking will fail due to unsatisfied library dependency.

The second problem is due to that warning (I believe):

/usr/lib/libX11.a(xim_trans.o): In function `_XimXTransSocketUNIXConnect':
(.text+0xcda): warning: Using 'getaddrinfo' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking

the utility is not linked statically against libc, which is still displayed in ldd output. How to correctly link statically with libX11 and libc in this case?

This question is relevant, but I think re-packing system .a files is not good idea.

Community
  • 1
  • 1
dma_k
  • 10,431
  • 16
  • 76
  • 128
  • @nos: When I put libraries before the object files e.g. `$(LD) $(LDFLAGS) -o $@ $(LIBS) $(OBJ)` then I cannot link anymore: several screens of "undefined references". – dma_k Oct 14 '10 at 22:59
  • Have you found the solution? I am basically struggling with the same problem. – augustin Oct 28 '10 at 10:10
  • @augustin: No. As I mentioned, libtool looks to target this problem, but I have no time to look into it and integrate on the top of autoconf. Maybe you'll succeed. – dma_k Oct 29 '10 at 11:05

2 Answers2

3

For libraries that provide .pc files for pkg-config like the X11R7 libraries do:

% pkg-config --static --libs x11
-lX11 -lpthread -lxcb -lXau  

Unfortunately, pkg-config --list-all doesn't seem to show libtiff as one of the libraries delivering those, but you can at least turn your link command into:

g++ -static-libgcc -Wl,-static -o my.out *.o -lc -ltiff -ljpeg -lz `pkg-config --static --libs x11`

and then not have to worry about keeping track of what libraries that version of libX11 needs (since libxcb was optional until recently, so older Linux distros may not have it).

alanc
  • 4,102
  • 21
  • 24
  • Thanks for the information! Indeed in some cases the information is missing or incomplete. For example, `pkg-config --static --libs GraphicsMagick++` will return only GraphicsMagick libraries, while the correct list is longer (see `GraphicsMagick++-config --libs`). Is it package maintaner task or the package developer to provide a correct information about the libraries? – dma_k Nov 15 '10 at 15:05
  • For most packages with pkg-config information, it comes from the upstream developers, though package maintainers may patch it when building their packages. – alanc Nov 15 '10 at 18:35
  • Thanks. Maybe you can help me with how to correctly link statically with libX11 and libc (see the original post)? I would very appreciate. – dma_k Nov 16 '10 at 15:32
  • Sorry, I'm not sure I can add much more. The system I use declared static linking an obsolete relic of the 1970's and stopped shipping static libraries about 5 years ago, so I don't deal with it. One thing to be very careful of is that static linking requires getting the library order just right - the -lc generally has to be at the very end, since it will only pull in the functions required by the objects and libraries already processed at the point the -l flag is handled. – alanc Nov 16 '10 at 20:52
0

In your case static linking of glibc is a bad idea. I recommend to use mixed linking.

vitaly.v.ch
  • 2,485
  • 4
  • 26
  • 36
  • It could be a good idea if you are compiling using Cygwin `gcc` and you want to get a monolith executable for e.g. Win32. What is mixed linking? Partial dynamic / partial static? Well, that is clear. – dma_k Oct 03 '12 at 15:37