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

时间:2021-07-04 06:21:42

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。所以,它使用了另一个。所以(在/usr/local/lib中)。现在我正在使用我的共享lib libabc。所以在我的应用程序。说我的程序。我想知道如何连接这些二进制文件?我不想把my-app和lib123连接起来。所以直接。my-app应该只与libabc.so链接。我该怎么做呢?

Thanks in advance. I am using g++ compiler

提前谢谢。我使用的是g++编译器。

2 个解决方案

#1


18  

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.

假设libabc。从位置独立的对象代码文件abc1.pic中获得。o和abc2.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.so with

和你建立libabc。所以,

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

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

我在-l123之前添加了-L/usr/local/lib,因为我假设您有一个/usr/local/ lib123。所以共享库。

Read also the Program Library HowTo.

还要阅读程序库。

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

如您所见,您可以链接共享库lib123。进入你自己的共享库libabc

Then check with ldd libabc.so

然后检查ldd libabc.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.

不要为lib123使用静态库。a(应该是PIC)。如果您将非pic代码链接到一个共享对象中,那么您将失去共享对象的大部分优势,并且动态链接器ld.因此必须进行大量的重新定位。

#2


1  

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

共享-o $b/$libfile nt_*.o

#1


18  

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.

假设libabc。从位置独立的对象代码文件abc1.pic中获得。o和abc2.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.so with

和你建立libabc。所以,

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

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

我在-l123之前添加了-L/usr/local/lib,因为我假设您有一个/usr/local/ lib123。所以共享库。

Read also the Program Library HowTo.

还要阅读程序库。

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

如您所见,您可以链接共享库lib123。进入你自己的共享库libabc

Then check with ldd libabc.so

然后检查ldd libabc.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.

不要为lib123使用静态库。a(应该是PIC)。如果您将非pic代码链接到一个共享对象中,那么您将失去共享对象的大部分优势,并且动态链接器ld.因此必须进行大量的重新定位。

#2


1  

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

共享-o $b/$libfile nt_*.o