Search code examples
c++shared-librariesbinary-compatibility

C++ Dynamic Library Compiling/Linking


I know that if I link my c++ program to a dynamic library (DLL) that was built with a different version of Visual Studio, it won't work because of the binary compatibility issue. (I have experienced this with Boost library and VS 2005 and 2008)

But my question is: is this true for all versions of MSVS? Does this apply to static libraries(LIB) as well? Is this an issue with GCC & Linux as well? and finally how about linking in VS to a DLL built with MinGW?

By the way aside from cross-platform or cross-compiler, why can't two version of the same compiler(VS) be compatibile?


Solution

  • Hi. I know that if I link my c++ program to a dynamic library (DLL) that was built with a different version of Visual Studio, it won't work because of the binary compatibility issue. (I have experienced this with Boost library and VS 2005 and 2008)

    I do not remember ever seeing MS changing the ABI, so technically different versions of the compiler will produce the same output (given the same flags (see below)).

    Therefore I don't think this is not incompatibilities in Dev Studio but changes in Boost.
    Different versions of boost are not backwards compatible (in binary, source they are backward compatible).

    But my question is: is this true for all versions of MSVS?

    I don't believe there is a problem. Now if you use different flags you can make the object files incompatible. This is why debug/release binaries are built into separate directories and linked against different versions of the standard run-time.

    Does this apply to static libraries(LIB) as well?

    You must link against the correct static library. But once the static library is in your code it is stuck there all resolved names will not be re-resolved at a later date.

    Is this an issue with GCC & Linux as well?

    Yes. GCC has broken backwards compatability in the ABI a couple of times (several on purpose (some by mistake)). This is not a real issue as on Linux code is usually distributed as source and you compile it on your platform and it will work.

    and finally how about linking in VS to a DLL built with MinGW?

    Sorry I don't know.

    By the way aside from cross-platform or cross-compiler, why can't two version of the same compiler(VS) be compatibile?

    Well fully optimized code objects may be compressed more thus alignment is different. Other compiler flags may affect the way code is generated that is incompatible with other binary objects (changing the way functions are called (all parameters on the stack or some parameters in registers)). Technically only objects compiled with exactly the same flags should be linked together (technically it is a bit looser than that as a lot of flags don't affect the binary compatibility).

    Note some libraries are released with multiple versions of the same library that are compiled in different ways. You usually distinguish the library by the extension on the end. At my last job we used the following convention.

    libASR.dll   // A Sincgle threaded Relase  version lib
    libASD.dll   // A Single  threaded Debug   version
    libAMR.dll   // A Multi   threaded Release version
    libAMD.dll   // A Multi   threaded Debug   version