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.

3 Answers
3

How RTTI information is stored is an implementation details 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 there 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 seprate 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.

@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 WangSep 12 '12 at 0:07

@Shao-ChuanWang: Potentially. It all depends on your compiler. To be safe all objects must be compiled with exactly the same flags.
–
Loki AstariSep 12 '12 at 0:30

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...!-)

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.