views:

1051

answers:

2

Is it correct to link a static library (.lib) compiled with VS 2005 with a program which is compiled with VS 2008? Both library and my program are written in C++. This program is run on Windows Mobile 6 Professional emulator.

This seems to work, there are no linking errors. However the program crashes during startup because strange things happen inside the linked library. E.g. lib can return a vector of characters with size of big negative number.

There are no such problems when the program is compiled with VS 2005.

What is even more strange the problem is only when using release configuration for the build. When compiling using debug configuration the problem doesn't occur.

+2  A: 

It's not incorrect to link to an older library in the way you describe, but it doesn't surprise me you're seeing some odd behavior. Couple sanity checks:

  • Are both files using the same versions of the same run-time libraries?
  • Is your .EXE application "seeing" the same header files that the .LIB was built against? Make sure that the _WIN32_WINNT (etc.) macros are declared properly.

And when you say .LIB, do you mean a true static library (mylib.lib) or an import library for a DLL (mylib.lib -> mylib.dll)?

And what are the compile/link settings for your VS2008 executable project?

James D
+10  A: 

VS2005 and VS2008 use different STL implementations. When the VS2005 code returns a vector, the object has memory layout different from what VS2008 expects. That should be the reason for the broken values you see in the returned date.

As a rule of thumb, you should always compile all C++ modules of a project with the same compiler and all settings/#defines equal.

One particular #define that causes similar behaviour is the SECURE_SCL #define of VS2008. Two modules compiled with different settings will create exactly your problems, because #defining SECURE_SCL introduces more member variables to various C++ library classes.

Timbo