p00ya00
p00ya00

Reputation: 796

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?

Upvotes: 3

Views: 2143

Answers (3)

Loki Astari
Loki Astari

Reputation: 264331

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

Upvotes: 2

Sean Middleditch
Sean Middleditch

Reputation: 2605

To answer part of your question, GCC/Linux does not have this problem. At least, not as often. libstdc++ and glibc are the standard C++/C libraries on GNU systems, and the authors of those libraries go to efforts to avoid breaking compatibility. glibc is pretty much always backward compatible, but libstdc++ has broken ABI several times in the past and probably will again in the future.

It is very difficult to write stable ABIs in C++ compared to C, because the automatic features in C++ take away some of the control you need to maintain an ABI. Especially once you get into templates and inline functions, where some of the code gets embedded in your application rather than staying contained in the shared library. That means that the object's structure can't ever change without requiring a recompilation of the application.

In practice, it isn't a huge deal on Windows. It would be fantastic if Microsoft just made the MSI installer know how to grab Microsoft-provided DLLs from Windows Update when an app is installed that needs them, but simply adding the redistributable to an InnoSetup-generated installer works well enough.

Upvotes: 0

Steve Wellens
Steve Wellens

Reputation: 20620

If properly built, DLLs should be programming-language and version neutral. You can link to DLLs built with VB, C, C++, etc.

You can use dependency walker to examine the exported functions in the dll.

Upvotes: 0

Related Questions