混合静态库和共享库 [英] Mixing static libraries and shared libraries

查看:124
本文介绍了混合静态库和共享库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我的猜测是,这是因为libhelper.a并未使用-fPIC进行编译,而libtestlib.so是.使用共享库(也依赖于静态库)来构建程序的正确方法是什么?

谢谢!

解决方案

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

好的.这应该做:

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

libhelper.a未使用-fPIC编译

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

使用共享库(也依赖于静态库)来构建程序的正确方法是什么?

如果如上所述将libhelper.a包含在libtestlib.so中,则很简单:

gcc main.c -ltestlib

是您所需要的.如果您坚持要与libhelper.a链接,则必须告知最终用户他必须与例如

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

链接.

gcc main.c -ltestlib -lhelper

无法指定libtestlib.so取决于libhelper.a.

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:

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

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?

Thanks!

解决方案

My goal is to link libhelper.a into libtestlib.so. Is that possible on 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

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.

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:

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.

gcc main.c -ltestlib -lhelper

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

这篇关于混合静态库和共享库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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