[CMake] Force MSVC runtime for debug builds

clinton at elemtech.com clinton at elemtech.com
Tue Nov 8 10:21:02 EST 2016


----- On Nov 7, 2016, at 1:37 AM, Stephan Menzel <stephan.menzel at gmail.com> wrote: 

> Hello everyone,
> I'm looking for a way to force Debug configurations in generated MSVC solutions
> to use the Release runtime instead of the default "Debug". e.g. /MD rather than
> /MDd.

> My use case is an ever recurring problem of creating libraries that are linked
> in plug-in fashion against Release only applications. I want for my Debug
> configuration to have debug info and no optimization but still use the release
> runtime so I can link them.

> I tried replacing /MDd with /MD in CMAKE_CXX_FLAGS_DEBUG. This yielded objects
> built with /MD alright but failed to link into an executable with stuff like
> that:

> error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MD_DynamicRelease'
> doesn't match value 'MDd_DynamicDebug' in Test.obj

> From what I gather all the obj are actually compiled for Release runtime without
> optimization and Debug info just like I need it. But then the main obj is built
> and linked in Debug mode, causing the executable to not be created.

> Is there a way to solve this? Like a generic CMake executable flag or something
> that I can use to force the other runtime?

> Please note that using RelWithDebInfo and turning off optimization is not
> exactly what I need as I still want that mode much the way it is.

If you are going to use /MD instead of /MDd, then you probably also need to remove the _DEBUG preprocessor flag. 
IIRC, I've seen cases where defining _DEBUG includes symbols only defined by the debug runtimes. 

Clint 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/cmake/attachments/20161108/25822521/attachment.html>


More information about the CMake mailing list