在linux中将共享库与另一个共享库链接 [英] Linking a shared library with another shared lib in linux

查看:11
本文介绍了在linux中将共享库与另一个共享库链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

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

Thanks in advance. I am using g++ compiler

推荐答案

假设 libabc.so 是从 位置无关 目标代码文件 abc1.pic.oabc2.pic.o ;然后你用例如构建它们

Suppose that libabc.so is obtained from posiition independent object code files abc1.pic.o and abc2.pic.o ; then you have built them with e.g.

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

然后你用

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

我添加了 -L/usr/local/lib before -l123 因为我假设你有一个 /usr/local/lib/lib123.so 共享库.

I added -L/usr/local/lib before -l123 because I am assuming you have a /usr/local/lib/lib123.so shared library.

另请阅读程序库操作方法.

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

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

然后检查 ldd libabc.so

您可能希望在 libabc.so<中设置一些 rpath/code> 通过将 -Wl,-rpath-Wl,$RPATHDIR 添加到链接命令.

You may want to set up some rpath in your libabc.so by adding -Wl,-rpath and -Wl,$RPATHDIR to the linking command.

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

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

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

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.so has to do zillions of relocations.

这篇关于在linux中将共享库与另一个共享库链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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