tags:

views:

351

answers:

3

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.

+1  A: 

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
A: 

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
Different flags may cause the padding in structures to be different thus causing objects to incompatible across libraries.
Martin York
+3  A: 

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.

Martin York
Excellent points that no others thought of here, specifically the issues regarding padding depending on the compiler settings.
Elemental