Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Linking release built library with my debug build

Tags:

c++

I've downloaded a 3rd party library, and built the .lib file in 'release' mode. After adding the lib to my project, if i run my project in release mode, it's fine. But if i run my project in debug mode, i get an error:

_iterator_debug_level value '0' doesn't match value '2;

I could rebuild the library in debug mode, but I don't think I'll need to be debugging the library itself? And I've downloaded prebuilt 3rd party libraries before which only come with a release build (i assume?) that link fine whether my project is in debug or release. I'm wondering how that is done.

like image 493
terryhau Avatar asked Apr 24 '11 17:04

terryhau


2 Answers

If you want to distribute a release library that others can use in either release or debug mode, you need to do two things:

  • Build a DLL, so that you get your own copy of the C runtime library
  • Not share CRT resources, such as the heap, across the library boundary. The biggest thing for C code is that dynamically allocated memory has to be deallocated on the same side of the boundary. For C++ code, you can use the std namespace inside your DLL, but not pass those objects across the boundary.

That's what the pre-built third-party libraries have most likely done. You can do the same thing with your library only if the external interface doesn't share CRT objects. Or you can build separate release and debug versions as static libraries.

like image 96
Ben Voigt Avatar answered Sep 19 '22 17:09

Ben Voigt


Looks like your debug binary and the library you downloaded use incompatible iterator debug modes. Iterator debugging is usually controlled by macros. Depending on macro values the sizes of interators and many other objects can change. You are lucky that your program emitted useful error message instead of simply crushing.

Check the library documentation and make sure that your project uses the same iterator debug mode. You may also try recompiling the library in release mode. If that doesn't help, you would have to recompile the library in debug mode, even if you don't intend to debug the library itself.

like image 23
pic11 Avatar answered Sep 23 '22 17:09

pic11