将共享库与 linux 中的另一个共享库链接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19424494/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 01:06:46  来源:igfitidea点击:

Linking a shared library with another shared lib in linux

c++clinuxlinkershared-libraries

提问by A R

I am trying to build a shared library. Let us say libabc.so .It uses another .so file , say lib123.so (a lib in /usr/local/lib) .Now i am using my shared lib libabc.so in my application. say my-app.I want to know how i should link these binaries??i don't want to link my-app with lib123.so directly. my-app should be linked with only libabc.so. How can i do this?

我正在尝试构建一个共享库。让我们说 libabc.so 。它使用另一个 .so 文件,比如说 lib123.so(/usr/local/lib 中的一个库)。现在我在我的应用程序中使用我的共享库 libabc.so。说 my-app。我想知道我应该如何链接这些二进制文件??我不想直接将 my-app 与 lib123.so 链接。my-app 应该只与 libabc.so 链接。我怎样才能做到这一点?

Thanks in advance. I am using g++ compiler

提前致谢。我正在使用 g++ 编译器

采纳答案by Basile Starynkevitch

Suppose that libabc.sois obtained from posiition independentobject code files abc1.pic.oand abc2.pic.o; then you have built them with e.g.

假设libabc.so是从获得posiition独立目标代码文件abc1.pic.oabc2.pic.o; 那么你已经用例如构建它们

 gcc -Wall -fPIC -O -g abc1.c -c -o abc1.pic.o
 gcc -Wall -fPIC -O -g abc2.c -c -o abc2.pic.o

and you build libabc.sowith

libabc.so

gcc -shared  abc1.pic.o  abc2.pic.o -L/usr/local/lib -l123 -o libabc.so

I added -L/usr/local/libbefore-l123because I am assuming you have a /usr/local/lib/lib123.soshared library.

-L/usr/local/lib之前添加-l123是因为我假设您有一个/usr/local/lib/lib123.so共享库。

Read also the Program Library HowTo.

另请阅读程序库操作方法

As you see, you may link a shared library lib123.sointo your own shared library libabc.so

如您所见,您可以将共享库链接lib123.so到您自己的共享库libabc.so

Then check with ldd libabc.so

然后检查 ldd libabc.so

You may want to set up some rpathin your libabc.soby adding -Wl,-rpathand -Wl,$RPATHDIRto the linking command.

您可能要设置一些rpath的在你的libabc.so加入-Wl,-rpath,并-Wl,$RPATHDIR以链接命令。

For much more details, read Drepper's paper How to write shared libraries

有关更多详细信息,请阅读 Drepper 的论文如何编写共享库

PS. Don't use a static library for lib123.a(it should be PIC). If you link non-PIC code into a shared object, you lose most of the advantages of shared objects, and the dynamic linker ld.sohas to do zillions of relocations.

附注。不要使用静态库lib123.a(它应该是 PIC)。如果将非 PIC 代码链接到共享对象中,则会失去共享对象的大部分优势,并且动态链接器ld.so必须进行无数次重定位。

回答by user2126571

When trying to create my own shared library that uses Berkeley DB, I found that I have to put the -ldb at the end of the gcc command or else it blew up saying the symbol 'db_create' was not found. This was under Cygwin.

在尝试创建我自己的使用 Berkeley DB 的共享库时,我发现我必须将 -ldb 放在 gcc 命令的末尾,否则它会爆炸,说找不到符号“db_create”。这是在 Cygwin 下。

Specifically, this worked:

具体来说,这有效:

gcc -shared -o $b/$libfile nt_*.o -ldb

gcc -shared -o $b/$libfile nt_*.o -ldb

This did not work:

这不起作用:

gcc -ldb -shared -o $b/$libfile nt_*.o

gcc -ldb -shared -o $b/$libfile nt_*.o

回答by Luighi Anthony Vitón Zorrilla

Following the same procedure pointed out by Basile Starynkevitch, for example, I have a library which depends on libm.so, so the compilation for the library objects are:

遵循Basile Starynkevitch指出的相同过程,例如,我有一个依赖于 的库libm.so,因此库对象的编译是:

gcc -fPIC -Wall -g -I include -I src -c src/wavegen.c  -o build/arm/wavegen.o                                                                                          
gcc -fPIC -Wall -g -I include -I src -c src/serial.c  -o build/arm/serial.o

To compile the library, however, in some versions of gcc the order where library references are placed, is important, so I suggest, to ensure compatibility, placing those references at the end of the command:

然而,为了编译库,在某些版本的 gcc 中,库引用的放置顺序很重要,所以我建议,为了确保兼容性,将这些引用放在命令的末尾:

gcc -shared -Wl,-soname,libserial.so.1 -o lib/libserial.so.1.0 build/arm/wavegen.o build/arm/serial.o -lm

I have tested in PC (gcc v.8.3.0) and in ARM (gcc v.4.6.3).

我已经在 PC (gcc v.8.3.0) 和 ARM (gcc v.4.6.3) 中进行了测试。