我有一个项目,其中有一个静态库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 -ltestlibis 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 -lhelperThere is no way to specify that libtestlib.so depends on libhelper.a.
更多推荐
混合静态库和共享库
发布评论