c++ - Exported typeinfo for pure virtual class in a shared library - Stack Overflow

I created a test case here:Using clang I want to compile an executable and a lib. The lib contains comp

I created a test case here:

Using clang I want to compile an executable and a lib. The lib contains complex hierarchy of classes. Most of them virtual.

I want to be able to cast a to a virtual class.

However this seems impossible as typeinfo of virtual class is a weak exported symbol and dynamic_cast in the executable fails.

The mentioned repository creates "inside_cast" that delegates the cast to the lib. However this is quite cumbersome.

Is there a way how to use dynamic_cast in the executable to cast to virtual class?

I created a test case here:

https://github/KapitanPL/inside_cast_example

Using clang I want to compile an executable and a lib. The lib contains complex hierarchy of classes. Most of them virtual.

I want to be able to cast a to a virtual class.

However this seems impossible as typeinfo of virtual class is a weak exported symbol and dynamic_cast in the executable fails.

The mentioned repository creates "inside_cast" that delegates the cast to the lib. However this is quite cumbersome.

Is there a way how to use dynamic_cast in the executable to cast to virtual class?

Share Improve this question asked Mar 10 at 13:36 KapitanKapitan 938 bronze badges 8
  • 2 We require all code to be in the question as text. Would be nice to reduce it to something that doesn't depend on Qt too. – HolyBlackCat Commented Mar 10 at 13:43
  • You should do that within the dll (or static part of the lib), it is the only code that knows which implementation of the abstract base it can be. But to be honest... you're question is not completely clear. – Pepijn Kramer Commented Mar 10 at 13:43
  • I'm not entirely sure about this, but try using __attribute__((visibility("default"))) always (not only when building the library). – HolyBlackCat Commented Mar 10 at 13:43
  • What I am trying to say is your dll should only export an abstract baseclass. And you can create a function that will return a std::unique_ptr to that abstract baseclass. Then later if the dll gets the interface back it may try to cast to an implementation class (to get access to more private stuff), but that is rarely ever needed. So can you explain a bit better what problem you want to solve... I got an XY problem feeling here. – Pepijn Kramer Commented Mar 10 at 13:45
  • There is no such thing as a "virtual class", much less a "pure virtual class". Since you haven't shown any code, it's difficult to figure out what you're asking. – Pete Becker Commented Mar 10 at 13:58
 |  Show 3 more comments

1 Answer 1

Reset to default 2

If the class can be generated from the header file then each library will create its own virtual table and RTTI data, instead move one virtual function to the .cpp file so no other library can create this Table, and the linker will have to grab it from the original library.

// .hpp file

class DLLEXPORT B: virtual public A {
public:
   virtual ~B(); 
   virtual QString getName() = 0;
};

// .cpp file
B::~B() = default;

yes, this prevents inlining, and every use has to go back to the original library. we just prevented every other library from creating its own copy.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744844015a4596725.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信