JoseleMG
JoseleMG

Reputation: 302

DLL compatibility for different compilers

Try to don't judge me really hard for this question.

Will I have problems if I try to use a DLL (written in C++) in a Visual Studio project that was compiled with the MinGW toolchain (g++)?

If the answer is yes, could someone explain me why?

Upvotes: 2

Views: 2013

Answers (3)

Niall
Niall

Reputation: 30605

As far as code execution goes, if it targets Windows, then it should run.

For interoperability; it depends on the API exposed from the dll.

  • If it is a "C" style API then you should be fine.
  • If it is a C++ API with classes and STL use, then you may run into trouble using different compilers (even different settings in the compilers). C++ doesn't have a standard ABI.

As noted in the comments and probably worth touching on here; the "C" style API is more than just the function calls, compatibility includes (but not limited) to things such as;

  • Memory allocation; general rule is "deallocation must be done by the same code as the allocation" - i.e. in the dll such that the matching allocation/deallocation is used (e.g. new and delete, malloc and free, CustomAlloc and CustomDealloc)
  • Data alignment and packing, not all compilers have the same defaults
  • Calling conventions, again, not all compilers have the same defaults, when in doubt, be explicit on what that convention should be (e.g. __stdcall)

As for the C++ ABI, in addition to all of the above, incompatibilities include;

  • Name mangling
  • STL class and container implementations
  • Separate and different implementations of new and delete, with custom memory pooling etc.
  • Exception mechanisms, e.g. generation, and stack unwinding

These are not comprehensive lists, but they go some way to motivating the general advice that when possible, use the same toolchains and same compiler/linker settings.

Upvotes: 4

Non-maskable Interrupt
Non-maskable Interrupt

Reputation: 3911

The problem is not about DLL. It's about how you describe such DLL to your compiler (ie. header files and exports).

Those descriptions affect how your code expect on data layout (e.g. different compiler may have different default alignment and padding), or even different code path (e.g. resolve to different inline functions and macro, such as in STL case).

Then each compiler may have different name mangling scheme, you may need a bit hack to glue the codes.

With very special care on the above issues you should be fine using a DLL from different compiler.

Upvotes: 0

Chlorek
Chlorek

Reputation: 158

No, because it is all one standard. And in fact your C++ application compiled with gcc calls system functions from OS dll files. So no matter if you call them manually or automatically, it is all the same.

Upvotes: -3

Related Questions