Dynamic_cast failed to hide character

I have many static libraries. One is static_lib_a.a. I am creating a dynamic library dynamic_lib.so to combine them.

In static_lib_a.a, it uses xerces 3.1.1 to parse xml. Below is a snippet of code in static_lib_a.a

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

The type pNode is xerces :: DOMNode. It is assigned to the xerces :: DOMElement object. This line of code will decrease.

To hide all the static_lib_a.a characters in dynamic_lib.so, I use -fvisibility = hidden to create this static library. I found that if you add - fvisibility = hidden , pElementNode will return a NULL pointer at runtime.

The gcc compiler version is 3.4.4.

Does anyone have similar problems before?

+3
source share
3 answers

The root of your problem is described in the gcc wiki under "C ++ Exception Problems". Make sure you follow the "undefined reference" link and read sections of virtual tables and types of info.

All this applies to your case, because the classes xerces::DOMNodeand xerces::DOMElementdo not contain impure, non-line virtual functions (in fact, these classes are completely contained in the headers). This means that a virtual table for any class is emitted in every object file that includes its header.

typeinfo , dynamic_cast, , , , .

, typeinfo xerces::DOMNode xerces::DOMElement static_lib_a.a . wiki, , dynamic_lib.so, dynamic_cast .

+3

- , . , - , , , , , , .

Windows, , DLL, , , , "" ", , .

, , 4. ,

#if defined(__GNUC__) && __GNUC__ >= 4

, , . , :

__attribute__((visibility("default")))

, , #define, - , SO_EXPORT, :

#define SO_EXPORT __attribute__((visibility("default")))

:

class SO_EXPORT MyAccessInterface;

:

SO_EXPORT int doSomething( parameters );

( , "", " " ). , , .

#define SO_HIDDEN __attribute__((visibility("hidden")))

class SO_HIDDEN MyClassImpl;

+1

dynamic_castrequires the typeinfo node for the derived class to point to the typeinfo node for the base class, which runs with dynamic movement. If the symbol for the info node type is not displayed, then the module containing the node for the derived class will have its own copy, and dynamic_cast then assumes that these classes belong to separate trees and forbids listing.

You need to add an attribute declaring default visibility to define the base class.

0
source

Source: https://habr.com/ru/post/1794678/


All Articles