Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
236 views
in Technique[技术] by (71.8m points)

c++ - Linking to a library depending on third party libs

I’m creating a library that depends on other libraries (libpng, libX11, etc.). I would like to know if it is possible (with some flags, for example) for the user binary not to directly link to the third party libraries but rather indirectly via my lib.

Here is an example:

libb.c (as the third party lib)

int get21()
{ return 21; }

liba.c (as my lib)

int get21();
int get42()
{ return get21() * 2; }

main.c (as the user code)

int get21();
int get42();
int main()
{
  printf("42 = %d
21 = %d
", get42(), get21());
  return 0;
}

Compilation

$ gcc -fPIC -shared libb.c -o libb.so
$ gcc -fPIC -shared liba.c -L. -lb -Wl,-rpath=. -o liba.so
$ gcc main.c -L. -la -Wl,-rpath=.
/usr/bin/ld: /tmp/ccVm8exQ.o: undefined reference to symbol 'get21'
./libb.so: error adding symbols: DSO missing from command line

Normally, I would need to link the main with -lb too. But I don’t want to final user to have to link against all libraries, as it is cumbersome and might change in the future. Is there a possibility of avoiding that?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I think you are asking about dynamic libraries, not static ones (as per the majority of the comments).

If so, yes, this is possible.

Suppose you have a dynamic library (.so) called A, which in turn uses other dynamic link libraries B and C. A binary X which wishes to use library A only needs to link to library A, and libraries B and C will be automatically pulled in. Note that X would need to link explicitly to B or C (and include their header files) for X to use anything in B or C directly (as opposed to via A).

Here's a live example. As you can see xml2-config says the right way to link to libxml2 is merely to use -lxml2. However, ldd shows that it in turn is linked to various other libraries, including liblzma (for instance). A program using libxml2 does not need to specify -llzma on the link line unless it uses liblzma directly.

$ xml2-config --libs
-lxml2
$ ldd /usr/lib/x86_64-linux-gnu/libxml2.so
    linux-vdso.so.1 =>  (0x00007fff157c9000)
    libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f7c51805000)
    libz.so.1 => /lib/x86_64-linux-gnu/libz.so.1 (0x00007f7c515ec000)
    liblzma.so.5 => /lib/x86_64-linux-gnu/liblzma.so.5 (0x00007f7c513c9000)
    libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f7c510c3000)
    libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f7c50cfd000)
    /lib64/ld-linux-x86-64.so.2 (0x00007f7c51d93000)

If you are asking how to do this, the key thing I've found is to persuade ldd that the libraries it uses are properly linked in. I tend to libtool for that.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...