0
votes

I added a new configuration DebugWithReleaseCRT to our CMake scripts, which is based on pretty standard Debug config but links with Release CRT (/MD instead of /MDd) and defines _ITERATOR_DEBUG_LEVEL=0. I checked generated project file settings and all looks good:

C/C++ -> Code Generation -> RuntimeLibrary = Multi-Threaded DLL (/MD)

However when I build it I get errors like:

3>3rd-party.lib(3rd-party.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MD_DynamicRelease' doesn't match value 'MDd_DynamicDebug' in My.obj

which I read as:

your My.cpp file is compiled with debug CRT (MDd_DynamicDebug) while you're trying to link with library 3rd-party.lib that is built with release CRT (MD_DynamicRelease)

I also checked this particular file (My.cpp) settings but nothing fancy there, it inherited /MD flag from the parent project.

Why my project is linked with Debug CRT despite of specified /MD flag?

1
Try cleaning the cache, the object files an rebuild again. Also note that 'MD_DynamicRelease' doesn't match value 'MDd_DynamicDebug' message is telling that you are invoking or using a wrong CMAKE_BUILD_TYPEJoel
@Joel: it's not cache problem or rebuild, and CMAKE_BUILD_TYPE is not relevant in VS caseAndriy Tylychko
Is it possible both /MD and /MDd are used on the command line? (Check the "Command Line" in the project settings). Maybe /MDd is overriding /MD, and the IDE isn't displaying it correctly in the project settings.MuertoExcobito

1 Answers

0
votes

The problem is related with VS precompiler definition _DEBUG

The compiler defines _DEBUG when you specify the /MTd or /MDd option. These options specify debug versions of the C run-time library.

As my DebugWithReleaseCRT config was based on Debug, it copied _DEBUG definition too. Turns out that if _DEBUG is defined it overrules /MD flag (MD_DynamicRelease) and VS still links with debug CRT (hey Visual Studio team, that was a surprise!).

The problem was solved by removing _DEBUG from DebugWithReleaseCRT. Actually it was solved by replacing _DEBUG by NDEBUG because some 3rd-party dependencies required exactly one of them to be defined. I'm still not sure it's a clean way to configure DebugWithReleaseCRT, I don't feel particularly confident about defining NDEBUG in kind of debug config.