隐藏符号时,dynamic_cast失败 [英] dynamic_cast failed when hiding symbol

查看:160
本文介绍了隐藏符号时,dynamic_cast失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很多静态库.一个是static_lib_a.a.我创建了一个动态库dynamic_lib.so来将它们放在一起.

I have many static libraries. One is static_lib_a.a. I create a dynamic library, dynamic_lib.so to put them together.

在static_lib_a.a中,它使用xerces 3.1.1解析xml.以下是 static_lib_a.a

In static_lib_a.a, it uses xerces 3.1.1 to parse xml. The following is the code snippet in static_lib_a.a

xerces::DOMElement *pElementNode = dynamic_cast<xerces::DOMElement *>(pNode);

pNode的类型是xerces :: DOMNode.它被分配给xerces :: DOMElement对象.这行代码将进行向下转换.

The type of pNode is xerces::DOMNode. It is assigned to an object of xerces::DOMElement. This line of code will do downcasting.

为了在dynamic_lib.so中隐藏static_lib_a.a的所有符号,我使用 -fvisibility = hidden 来构建此静态库.我发现如果添加- fvisibility = hidden ,pElementNode将在运行时返回NULL指针.

In order to hide all symbols of static_lib_a.a in dynamic_lib.so, I use -fvisibility=hidden to build this static library. I found if I add -fvisibility=hidden, pElementNode will return a NULL pointer in runtime.

gcc编译器的版本为3.4.4.

Version of gcc compiler is 3.4.4.

有人以前有类似的问题吗?

Does anyone have similar issues before?

推荐答案

gcc wiki 下标题为"C ++异常的问题"的部分.确保您点击此处的模糊链接"链接,并阅读了有关虚拟表和typeinfo的部分.

The root of your problem is described on the gcc wiki under the section entitled "Problems with C++ exceptions". Make sure you follow the "vague linkage" link there and read the sections on virtual tables and typeinfo.

这一切都适用于您的情况,因为类xerces::DOMNodexerces::DOMElement不包含任何非纯的,非内联的虚函数(实际上,这些类完全包含在头文件中).这意味着任何一个类的虚拟表都会在每个包含其标题的目标文件中发出.

This all applies to your case because the classes xerces::DOMNode and xerces::DOMElement contain no non-pure, non-inline virtual functions (in fact these classes are contained entirely in headers). This means that the virtual table for either class is emitted in every object file that includes its header.

dynamic_cast正常工作所必需的任何一个类的typeinfo符号,都在与虚拟表相同的对象中发出,即在包含其标题的每个对象文件中发出.

The typeinfo symbols for either class, which are required for dynamic_cast to work properly, are emitted in the same object as the virtual table i.e. in every object file that includes its header.

当您用隐藏的可见性标记您的库时,来自static_lib_a.a的对象中xerces::DOMNodexerces::DOMElement的所有typeinfo符号都被标记为隐藏.正如Wiki页面所指出的那样,这可确保链接器随后将其标记为在dynamic_lib.so中隐藏,并且您的dynamic_cast将失败.

When you marked your library with hidden visibility all the typeinfo symbols for xerces::DOMNode and xerces::DOMElement in objects from static_lib_a.a were marked hidden. As the wiki page points out, this ensures that the linker will then mark it as hidden in dynamic_lib.so and your dynamic_cast will fail.

这篇关于隐藏符号时,dynamic_cast失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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