Question

What could prevent me from linking with a third-party .lib built with Visual Studio 2008 in a program that I compile with Visual Studio 2005? Thanks

Was it helpful?

Solution

Update: This applies to DLLs only, which was the original question. With static libraries, all hope is lost.

I'll try to summarize a few facts:

  • The ABI itself is compatible, so any code that uses the same definitions for all data types and function signatures is fine, but
  • System internal structure definitions and system include files may have changed, except
  • Parts of the ABI are defined to be either fixed or backwards compatible.

Thus it is safe to call functions and methods, passing data types that are entirely (that is, including their members) defined

  • in the header files matching the DLL, or
  • in an IDL file (because these are meant to never change once published), or
  • in another header file, including system headers, if the definition is guaranteed to be fixed

For pointer members, the rule is relaxed if the pointer is never dereferenced.

Now the tricky part:

  • The internal data structures of the system allocator are not part of the unchanging set. As a pointer to allocated memory is also a pointer to the allocation structure immediately preceeding it, the never-dereference rule applies. Thus:
    • it is not safe to deallocate memory that was passed in, with the exception of CoTaskMemAlloc()/CoTaskMemFree().
    • a virtual destructor can only be called if the object was created inside the DLL (it is the new-expression that counts here, not whether the constructor is exported)
    • a nonvirtual destructor can only be called if the object was created outside the DLL.
  • The STL is not part of the unchanging set, so any struct containing STL types is unsafe.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top