GCC -rdynamic不能与静态库一起工作 [英] GCC -rdynamic not working with static libraries

查看:661
本文介绍了GCC -rdynamic不能与静态库一起工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么-rdynamic不在.a文件中导出符号,而是在.o文件中导出符号?



我有一个应用程序和一个插件一个.so文件。主应用程序使用一系列目标文件和一个静态库链接,如下所示:

  CXXFLAGS = $(CXXFLAGS_COMMON) - rdynamic 
STATICLIBS = ../Utilities/Utilities.a
...

全部:
$(CXX)$(CXXFLAGS)-o $(样本) main.o $(STATICLIBS)$(SHAREDLIBS)$(INCLUDES)



(CXX是g ++ 4.5。 2在Ubunut上,我主要使用-std = c ++ Ox进行编译)

在这种情况下,Utilities.a中的符号不​​会被导出(即objdump - t a.out | grep symbol为空)。



如果我使用ar x来提取.a中的.o文件并仅使用链接。 o,那么这些符号会被插件导出并找到(如果您想知道的话,这些插件会加载dlopen)。



我试过使用-Wl,-export - 动态但没有成功。



我有一个解决方法,正如提到的,但我仍然想明白我缺少的东西。通常情况下,链接器只包含静态存档的那些部分( .a ) / code> files)。



强制链接器包含 .a 文件,您可以使用 - 整个存档链接器选项(所以 -Wl, - 整个存档 -Wl, - whole-archive 是位置指定的,在命令行上敏感 - 它只会影响命令行中的 .a 文件。如果还有更多的静态档案不需要完全包含,那么它的效果可能会随后被 -Wl, - no-whole-archive 关掉。



所以,比如用你的命令:

  $(CXX )$(CXXFLAGS)-o $(SAMPLE)main.o -Wl, -  whole-archive $(STATICLIBS)-Wl, -  no-whole-archive $(SHAREDLIBS)$(INCLUDES)


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 !

解决方案

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:

$(CXX) $(CXXFLAGS) -o $(SAMPLE) main.o -Wl,--whole-archive $(STATICLIBS) -Wl,--no-whole-archive $(SHAREDLIBS) $(INCLUDES)

这篇关于GCC -rdynamic不能与静态库一起工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆