c++visual-c++dlllinker

DLL and compiler settings


Many articles and posts warn about compiler settings that can cause incompatibilities when linking and using DLLs.

If you follow best practice when writing your DLL in C++ and export your functions using extern “C” and only using POD data types or sticking to interface (pure virtual) classes, what are the compiler settings that can cause problems?

How do you know which compiler settings will cause problems? Will different compilers have different settings? Is there a list of Visual C++ compiler settings available?


Solution

  • This is not a definitive list, but things I remembered:

    1. Linking to different runtimes like, for example, linking to the debug Visual C++ runtime in your main executable and linking to the release Visual C++ runtime in the DLL you are importing can also cause problems. (Code Generation -> Runtime Library option)

    2. Linking to different versions of the C++ runtime (i.e. VC90 and VC100) is also not advisable. (General tab -> Platform Toolset option)

    3. Be aware of the calling conventions the DLL uses (C/C++ Advanced tab -> Calling Convention option)

    4. When passing structures between the two modules make sure both modules have same packing (C/C++ -> Code Generation -> Struct Member Alignment option)

    5. Not sure, but sounds reasonable that they should have the same exception handling model (C/C++ -> Code Generation -> Enable C++ exceptions option)

    6. Same floating point model if you're passing floats to and from (C/C++ -> Code Generation -> Floating Point Model option)

    7. Both modules are the same architecture, obviously (Intel x86, AMD x64, Itanium, ARM, etc.)

    For the second part of the question, here is comprehensive documentation on all the compiler options.