Convert a Static Library to a Shared Library (create libsome.so from libsome.a): where#39;s my symbols?(将静态库转换为共享库(从 libsome.a 创建 libsome.so):我的符号在哪里?)
问题描述
这个问题的标题是完全欺骗,但该问题的答案对我没有帮助.
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):我的符号在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:将静态库转换为共享库(从 libsome.a 创建 libsome.s


基础教程推荐
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 初始化列表*参数*评估顺序 2021-01-01
- CString 到 char* 2021-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05