views:

172

answers:

4

I was wondering what the consequences are for compiling a class A with one compiler that doesn't allow multiple inheritance, and compiling a class B that does support it (and class B derived from class A).

I don't really understand the linking process...would it be possible to use both together? What disadvantages exist for using separate compilers in this situation, with vtables? Would it be impossible for code using class B to function properly?

Thanks.

+10  A: 

As a general rule, don't ever compile parts of your C++ program with different compilers.

Different compilers may use, and often do, different mangling schemas for the symbol mangling stage, so it's very unlikely that the linking between separately compiled stuff will work.

See doc about mangling name_mangling

Arkaitz Jimenez
In addition, vtable implementation, exception handling, etc (the whole C++ ABI) might be different.
sellibitze
Thanks for the heads-up. I will never do this.
Jasie
It's possible to do this if you use shared objects/DLLs. Then there is a common linking convention so that different compilers can work together. If you use a DLL in your application from an external vendor, you don't know what compiler they used. You would have to break up your application into "component" shared objects/DLLs.
David G
@David: This is just wrong. The only way to make binaries compatible is to use the "C" ABI (As this is well defined). This means no sharing of C++ structures methods/functions or classes. The only reason you can share DLL's is that VS compiler (cl) is so ubiquitous.
Martin York
+3  A: 

Object layout (vtable pointer location, vtable format, sub-object placement, etc.) is not guaranteed to be the same between the compilers.

Nikolai N Fetissov
+2  A: 

It's not just classes that won't be able to talk to one another. Bare functions declared in a header but compiled only by one of the compilers will be invisible to the other compiler because of name mangling.

Also, any static classes/members of classes compiled by the compiler that does NOT compile main() will not initialize correctly because that compiler's runtime will not be executed. Even things like 64bit long long arithmetic (on 32bit platforms) might not be linked correctly because of the conflicting runtime libraries.

jmucchiello
+2  A: 

As an addendum to Arkaitz's post above, you may find other issues that could stop code working together from compilation units built with different compilers:

  1. data size issues (eg one compiler uses 32 bit ints, the other 64 bit)
  2. data alignment issues
  3. issues with heap memory

Basically anywhere that the C++/C standards isn't very specific about things leaves scope for differences between compilers, and hence scope for problems mixing them

pxb