且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Linux上的RTLD_LOCAL和dynamic_cast

更新时间:2023-11-10 11:42:46

好吧,我们最终要做的就是解决该问题.

Ok, what we finally did is kind of worked around the problem.

我们在要dynamic_cast()的类中添加了两个静态函数:

We added to the classes that we want to dynamic_cast() two static functions:

static MyClass* doNew();
static MyClass* doDynCast(MyBase*);

这些是在cpp文件中实现的,该文件将newdynamic_cast()type_info对象保留在同一库中,从而使dynamic_cast()可以解决此问题.

These were implemented in the cpp file which kept the new, the dynamic_cast() and the type_info object in the same lib and thus making the dynamic_cast() work around the problem.

对于我们的特定情况,此解决方案就足够了,但是如果有人有更通用的解决方案,它将受到欢迎.

This solution was enough for our specific case but if anyone has a more general solution it will be welcomed.

我们发现的另一种选择是将类的所有实现都放入cpp文件中,这使得typeinfo符号仅出现在一个库中,而所有其他库仅引用它.这将导致成功dynamic_cast().

Another option that we found is to put all the implementation of the class in the cpp file which make the typeinfo symbol be present only in one library and all other libraries reference it only. This results in a successful dynamic_cast().