9

If I have multiple linked C++ statically linked libraries in C++, is it possible for them to share (pass to and from functions) class objects if they have been compiled with differing values of enabled/disabled run time type information (RTTI)?

--edit: Thanks for the responses, the specific things I was worried about was 1. Does enabling RTTI change the behaviour of sizeof for static (non polymorphic types)?

and, 2. If I create a class in an RTTI enabled library and pass it to another non RTTI enabled library, do virtual methods work properly. (and vice versa)

and lastly 3. If I create a class in an RTTI enabled library, I expect to be able to use dynamic_cast with it, if I pass that object to a non-RTTI enabled library, can I still use it on that object. ... I would assume not, and it seems like a bad idea anyway... I'm just curious.

Akusete
  • 10,704
  • 7
  • 57
  • 73

3 Answers3

7

How RTTI information is stored is an implementation detail and thus not portable across different compilers.

Also most compilers do not even guarantee that objects compiled with different flags will use the same ABI for their methods. This is most prominently shown with release and debug libraries but other flags can cause differences as well.

Not only may the ABI for functions/methods change but flags can affect the padding used by the compiler between elements in structures thus even objects without virtual methods may be incompatible when compiled with different flags.

When using most IDS you can see the effects. Debug/Release binaries are built into separate directories and only linked against the same kind of binary (also any user defined build will be built into a separate unique directory as a difference in flags may cause incompatibilities). If you change certain flags on a build then the whole project is usually forced to re-build.

Alex Reinking
  • 16,724
  • 5
  • 52
  • 86
Martin York
  • 257,169
  • 86
  • 333
  • 562
  • Excellent points that no others thought of here, specifically the issues regarding padding depending on the compiler settings. – Elemental Sep 29 '09 at 16:00
  • @Loki Astari: Does it imply if I use an object from a .so or .dll (compiled with RTTI disabled), I would not be able to use dynamic_cast nor typeid for that object. Or worse, the ABI was not correct so the programs are incompatible (maybe crash, etc). – Shao-Chuan Wang Sep 12 '12 at 00:07
  • @Shao-ChuanWang: Potentially. It all depends on your compiler. To be safe all objects must be compiled with exactly the same flags. – Martin York Sep 12 '12 at 00:30
1

That depends on what specific C++ compiler you're talking about -- I have no really recent cross-platforms experience with C++ (my C++ work in recent years has been almost exclusively with C++ on Linux), but a few years ago I'd have bet that gcc would have let you get away with quite a bit of such miscegenation, Visual C++ "no way", other compilers somewhat in the middle...!-)

Alex Martelli
  • 854,459
  • 170
  • 1,222
  • 1,395
-1

As long as the classes shared are not polymorphic (i.e, they don't contain virtual functions), this will not be a problem. But you won't be able to use dynamic_cast, typeid and exceptions with RTTI disabled.

Vijay Mathew
  • 26,737
  • 4
  • 62
  • 93
  • 1
    Different flags may cause the padding in structures to be different thus causing objects to incompatible across libraries. – Martin York Sep 29 '09 at 10:48