Nayana Adassuriya
Nayana Adassuriya

Reputation: 24766

How name mangling works with DLLs and LIBs compiled with different compilers

I'm pretty sure I don't have clear understanding about C++ Name Mangling. But as I know there is no stranded for C++ Name Mangling. So my question is

Think I compiled DLL or LIB using one compiler. They try to use above DLL or LIB in a program compiles using different compiler.

So how second compiler identify the functions, class, data types etc.. inside that LIB or DLL? (I guess above two compiler having different Name Mangling)

from wiki

There isn't a standard scheme by which even trivial C++ identifiers are mangled, and consequently different compiler vendors (or even different versions of the same compiler, or the same compiler on different platforms) mangle public symbols in radically different (and thus totally incompatible) ways.

Upvotes: 0

Views: 197

Answers (1)

Jerry Coffin
Jerry Coffin

Reputation: 490663

Generally speaking, you have two choices:

  1. Export C++ "stuff" (classes, etc.) as-is, realizing they will work only with that same compiler (or one engineered specifically to be compatible, such as Intel's with code from Microsoft's). Making the latter work generally also means using the same standard library code (which Intel's does).
  2. Export only C-style functions using a specified, neutral calling convention such as stdcall.

Note that name mangling is generally the least of your concerns with something like this. Getting the names to match would actually be pretty easy in most cases. The real problem is that different compilers produce code using different calling conventions. If those don't match (precisely), the best you can hope for is for the resulting program to crash quickly and cleanly. Differences in name mangling simply catch the problem early, and keep differences in calling convention from destroying your data.

Upvotes: 2

Related Questions