GCC -rdynamic 不适用于静态库
为什么 -rdynamic 不导出 .a 文件中的符号,而是导出 .o 文件中的符号?
我有一个应用程序和一个插件,位于 .so 文件中。主应用程序使用一系列目标文件和一个静态库链接,如下所示:
CXXFLAGS = $(CXXFLAGS_COMMON) -rdynamic
STATICLIBS = ../Utilities/Utilities.a
...
all:
$(CXX) $(CXXFLAGS) -o $(SAMPLE) main.o $(STATICLIBS) $(SHAREDLIBS) $(INCLUDES)
(CXX 是 Ubunut 上的 g++ 4.5.2,我主要使用 -std=c++Ox 进行编译)
在这种情况下,实用程序中的符号.a 未导出(即“objdump -t a.out | grep 符号”为空)。
如果我使用“ar x”提取 .a 中的 .o 文件并仅使用 .o 进行链接,则符号将被导出并由插件找到(如果您想知道,这些符号是用 dlopen 加载的)。
我尝试过使用 -Wl,-export-dynamic 但没有成功。
正如前面提到的,我确实有一个解决方法,但我仍然希望了解我缺少什么。提前致谢 !
Why is -rdynamic not exporting the symbols in .a files but is exporting the symbols in .o files ?
I have an app and a plug-in in a .so file. The main app is linked using a series of object files and one static library, like this:
CXXFLAGS = $(CXXFLAGS_COMMON) -rdynamic
STATICLIBS = ../Utilities/Utilities.a
...
all:
$(CXX) $(CXXFLAGS) -o $(SAMPLE) main.o $(STATICLIBS) $(SHAREDLIBS) $(INCLUDES)
(CXX is g++ 4.5.2 on Ubunut, I use mainly -std=c++Ox for compilation)
In this case, the symbols in Utilities.a are not exported (i.e. "objdump -t a.out | grep symbol" is empty).
If I use "ar x" to extract the .o files in the .a and link using only the .o's, then the symbols are exported and found by the plug-ins (that are loaded with dlopen in case you wondered).
I have tried using -Wl,-export-dynamic but without success.
I do have a workaround, as mentionned, but I'd still wish to understand what I am missing. Thanks in advance !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
通常,链接器仅包含静态档案(
.a
文件)中被引用的部分。要强制链接器包含
.a
文件的全部内容,您可以使用--whole-archive
链接器选项(因此-Wl,-- gcc 命令行上的 Whole-archive
)。请注意,
-Wl,--whole-archive
在命令行上是位置敏感的 - 它仅影响命令行上紧随其后的.a
文件。如果您不希望完全包含更多静态存档,则可以随后使用 -Wl,--no-whole-archive 关闭其效果。因此,例如,使用您的命令:
Normally, the linker only includes those parts of static archives (
.a
files) which are referenced.To force the linker to include the entire contents of a
.a
file, you can use the--whole-archive
linker option (so-Wl,--whole-archive
on the gcc command line).Note that
-Wl,--whole-archive
is position-sensitive on the command line — it only affects.a
files following it on the command line. Its effect may be subsequently turned off using-Wl,--no-whole-archive
, if there are further static archives which you don't want to be completely included.So, for instance, with your command:
.o
文件是 ELF 可重定位的,.so
文件是 ELF 共享对象。而.a
文件是当前存档。当链接器通过gcc
中的-rdynamic
传递--export-dynamic
标志时,它正在寻找动态目标文件。我认为链接器不关心查看存档并提取符号。
A
.o
file is ELF relocatable, and, a.so
file is ELF shared object. Whereas a.a
file is current archive. When the linker is passed the--export-dynamic
flag through the-rdynamic
fromgcc
, it is looking for a dynamic object file.I think the linker does not care to look into the archive and extract the symbols.