开发者

Static library auto-discovery and linking

开发者 https://www.devze.com 2023-01-19 19:55 出处:网络
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 li

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开发者_StackOverflow中文版 -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.


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).


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

0

精彩评论

暂无评论...
验证码 换一张
取 消