混合静态库和共享库

编程入门 行业动态 更新时间:2024-10-28 19:26:39
本文介绍了混合静态库和共享库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个项目,其中有一个静态库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.

更多推荐

混合静态库和共享库

本文发布于:2023-08-07 22:34:52,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1322184.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:静态

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!