问题描述
此问题的标题是 ,但是这个问题的答案不能帮助我。
the title of this question is an exact dupe, but the answers in that question don't help me.
我有一堆静态库中的对象文件:
I have a bunch of object files packed in a static library:
% g++ -std=c++98 -fpic -g -O1 -c -o foo.o foo.cpp
% g++ -std=c++98 -fpic -g -O1 -c -o bar.o bar.cpp
% ar -rc libsome.a foo.o bar.o
我想从libsome.a而不是目标文件生成libsome.so,但是该库是真的 barebones:
I'd like to generate libsome.so from libsome.a instead of the object files, but the library is really barebones:
% g++ -std=c++98 -fpic -g -O1 -shared -o libsome.so libsome.a
% nm -DC libsome.so
0000xxxx A _DYNAMIC
0000xxxx A _GLOBAL_OFFSET_TABLE_
w _Jv_RegisterClasses
0000xxxx A __bss_start
w __cxa_finalize
0000xxxx A _edata
0000xxxx A _end
0000xxxx T _fini
0000xxxx T _init
静态库确定,或至少我完全能够链接到一个可执行文件,并让它运行所包含的功能。如果我从foo.o和bar.o创建libsome.so,一切都很好。
the static library is ok, or at least I'm perfectly able to link it to an executable and have it run the contained functionality. also, everything is fine if I create libsome.so from foo.o and bar.o.
推荐答案
GNU链接器,需要指定--whole-archive选项,以便获取静态归档的所有内容。因为这是一个链接器选项,你需要告诉gcc将它传递给链接器:
Assuming you're using the GNU linker, you need to specify the --whole-archive option so that you'll get all the contents of the static archive. Since that's an linker option, you'll need -Wl to tell gcc to pass it through to the linker:
g++ -std=c++98 -fpic -g -O1 -shared -o libsome.so -Wl,--whole-archive libsome.a
如果你在做一些更复杂的事情,你想要所有的库一些,但只需要libsome所需的库支持的一部分,你会想要关闭整个存档后,你使用它libsome:
If you were doing something more complicated where you want all of library some but only the part of library support needed by libsome, you would want to turn off whole archive after you've used it on libsome:
... -Wl,--whole-archive libsome.a -Wl,--no-whole-archive libsupport.a
如果你不使用GNU链接器,你需要看看你的链接器是否支持它和它叫什么。在Sun连接器上,它被称为 -z allextract
和 -z defaultextract
。
If you're not using the GNU linker, you'll need to see if your linker supports it and what it's called. On the Sun linker, it's called -z allextract
and -z defaultextract
.
这篇关于将静态库转换为共享库(从libsome.a创建libsome.so):我的符号在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!