LNK2038 Linker Error in Visual C++

Problem

I got this error at the linking stage of compiling a Visual C++ project:

Error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '0' doesn't match value '2' in main.obj

This project was being linked with other libraries and the error referred to the _ITERATOR_DEBUG_LEVEL was defined with different values in my application and the external library.

Solution

The difference in the _ITERATOR_DEBUG_LEVEL typically happens if a Release version of a library is linked against a Debug version of another library or application. Make the two have the same build type and this error should go away.

If you cannot do that, then set the _ITERATOR_DEBUG_LEVEL in your project to match the value used in the other library.

Tried with: Visual Studio 2010 and Windows 7 x64