问题描述
我有一个项目,其中有一个静态库libhelper.a
,另一个有我的实际共享库libtestlib.so
.我的目标是将libhelper.a
链接到libtestlib.so
.在Linux/BSD上有可能吗?尝试创建测试程序时,出现以下错误:
我的猜测是,这是因为libhelper.a并未使用-fPIC
进行编译,而libtestlib.so
是.使用共享库(也依赖于静态库)来构建程序的正确方法是什么?
谢谢!
好的.这应该做:
gcc -shared -fPIC -o libtestlib.so $(OBJS) \
-Wl,--whole-archive -lhelper -Wl,--no-whole-archive
最好用-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:
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!
Sure. This should do:
gcc -shared -fPIC -o libtestlib.so $(OBJS) \
-Wl,--whole-archive -lhelper -Wl,--no-whole-archive
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
.
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
.
这篇关于混合静态库和共享库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!