混合静态库和共享库

时间:2021-02-18 13:25:20

I have a project where I have one static library libhelper.a and another with my actual shared object library, libtestlib.so. My goal is to link libhelper.a into libtestlib.so. Is that possible on Linux/BSD? When I tried and created a test program I got the following errors:

我有一个项目,我有一个静态库libhelper.a和另一个我的实际共享对象库libtestlib.so。我的目标是将libhelper.a链接到libtestlib.so。这可能在Linux / BSD上吗?当我尝试创建测试程序时,我收到以下错误:

./prog1:/usr/local/lib/libtestlib.so.1.0: undefined symbol ''

./prog1:/usr/local/lib/libtestlib.so.1.0:未定义的符号''

My guess is that this is occurring because libhelper.a was not compiled with -fPIC while libtestlib.so was. What is the proper way to build programs that use shared libraries that also have dependancies on static libraries?

我的猜测是,这是因为libhelper.a没有使用-fPIC编译,而libtestlib.so是。构建使用共享库的程序的正确方法是什么?这些库也具有静态库的依赖性?

Thanks!

1 个解决方案

#1


11  

My goal is to link libhelper.a into libtestlib.so. Is that possible on Linux?

我的目标是将libhelper.a链接到libtestlib.so。这可能在Linux上吗?

Sure. This should do:

当然。这应该做:

gcc -shared -fPIC -o libtestlib.so $(OBJS) \
  -Wl,--whole-archive -lhelper -Wl,--no-whole-archive

libhelper.a was not compiled with -fPIC

libhelper.a未使用-fPIC编译

It's best to rebuild libhelper.a with -fPIC. If that's not possible, above command will still work on Linux/ix86, but not on e.g. Linux/x86_64.

最好用-fPIC重建libhelper.a。如果这不可能,则上面的命令仍可在Linux / ix86上运行,但不能在例如Linux / ix86上运行。 Linux的/ x86_64的。

What is the proper way to build programs that use shared libraries that also have dependancies on static libraries?

构建使用共享库的程序的正确方法是什么?这些库也具有静态库的依赖性?

If you include libhelper.a into libtestlib.so as above, then simple:

如果你将libhelper.a包含在libtestlib.so中,那么就简单了:

gcc main.c -ltestlib

is all you need. If you insist on linking with libhelper.a, then you must tell the end-user that he must link with e.g.

是你所需要的全部。如果你坚持用libhelper.a链接,那么你必须告诉最终用户他必须与例如

gcc main.c -ltestlib -lhelper

There is no way to specify that libtestlib.so depends on libhelper.a.

没有办法指定libtestlib.so依赖于libhelper.a。

#1


11  

My goal is to link libhelper.a into libtestlib.so. Is that possible on Linux?

我的目标是将libhelper.a链接到libtestlib.so。这可能在Linux上吗?

Sure. This should do:

当然。这应该做:

gcc -shared -fPIC -o libtestlib.so $(OBJS) \
  -Wl,--whole-archive -lhelper -Wl,--no-whole-archive

libhelper.a was not compiled with -fPIC

libhelper.a未使用-fPIC编译

It's best to rebuild libhelper.a with -fPIC. If that's not possible, above command will still work on Linux/ix86, but not on e.g. Linux/x86_64.

最好用-fPIC重建libhelper.a。如果这不可能,则上面的命令仍可在Linux / ix86上运行,但不能在例如Linux / ix86上运行。 Linux的/ x86_64的。

What is the proper way to build programs that use shared libraries that also have dependancies on static libraries?

构建使用共享库的程序的正确方法是什么?这些库也具有静态库的依赖性?

If you include libhelper.a into libtestlib.so as above, then simple:

如果你将libhelper.a包含在libtestlib.so中,那么就简单了:

gcc main.c -ltestlib

is all you need. If you insist on linking with libhelper.a, then you must tell the end-user that he must link with e.g.

是你所需要的全部。如果你坚持用libhelper.a链接,那么你必须告诉最终用户他必须与例如

gcc main.c -ltestlib -lhelper

There is no way to specify that libtestlib.so depends on libhelper.a.

没有办法指定libtestlib.so依赖于libhelper.a。