问题描述
这个问题的标题是完全欺骗,但该问题的答案对我没有帮助.
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,但该库是真的准系统:
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 选项,以便获得静态存档的所有内容.由于这是一个链接器选项,您需要 -Wl 告诉 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):我的符号在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!