链接到库取决于第三方库 [英] Linking to a library depending on third party libs

查看:179
本文介绍了链接到库取决于第三方库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建依赖于其他库的库(的libpng 的libx11 等)。我想知道如果有可能(有一些标志,如)用户二进制不希望通过我的lib直接链接到第三方库,而是间接的。

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.

下面是一个例子:

int get21()
{ return 21; }

liba.c (我的LIB)

liba.c (as my lib)

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

的main.c (作为用户code)

main.c (as the user code)

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

编辑

$ 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

通常情况下,我需要用 -lb 的主要环节太多。但我不希望为最终用户不得不对所有库链接,因为它是繁琐,在未来可能会改变。是否有避免的可能?

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?

推荐答案

我想你问有关动态库,而不是静态的(按照大多数的意见)。

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

如果是这样,是的,这是可能的。

If so, yes, this is possible.

假设你有一个动态库(的.so )被称为A,又使用其他动态链接库B和C的双重X其中希望使用库中的只需要链接到库中的一个,和库B和C都被自动拉。请注意,十大将需要显式地链接到B或C(并包括其头文件)为X用在直接B或C任何东西(如经A反对)。

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

下面是一个活生生的例子。正如你所看到的 XML2-配置表示要链接到的libxml2 的正确方法是仅仅使用 -lxml2 。然而, LDD 显示,它反过来链接到其他各种库,包括 liblzma (例如)。使用程序的libxml2 不需要指定 -llzma 链接行上,除非它使用 liblzma 直接

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)

如果你问的如何的做到这一点,我已经找到了关键的事情就是说服 LDD ,它使用的库正确。我倾向于的libtool 该链接。

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.

这篇关于链接到库取决于第三方库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆