Force MSVC runtime for debug builds

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Force MSVC runtime for debug builds

Stephan Menzel
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.

Cheers,
Stephan



--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
http://public.kitware.com/mailman/listinfo/cmake
Reply | Threaded
Open this post in threaded view
|

Re: Force MSVC runtime for debug builds

Clinton Stimpson


----- On Nov 7, 2016, at 1:37 AM, Stephan Menzel <[hidden email]> 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

--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
http://public.kitware.com/mailman/listinfo/cmake
Reply | Threaded
Open this post in threaded view
|

Re: Force MSVC runtime for debug builds

Michael Jackson
I would agree. I don't know how many subtle runtime bugs I have had to
figure out because our project mixed debug and release runtimes on MSVC.
DON'T do it unless you REALLY have to. But even then I would never
actually deploy that into production.

YMMV
--
Mike Jackson  [[hidden email]]


[hidden email] wrote:

>
>
> ----- On Nov 7, 2016, at 1:37 AM, Stephan Menzel
> <[hidden email]> 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
>
--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
http://public.kitware.com/mailman/listinfo/cmake
Reply | Threaded
Open this post in threaded view
|

Re: Force MSVC runtime for debug builds

Stephan Menzel
In reply to this post by Clinton Stimpson

On Tue, Nov 8, 2016 at 4:21 PM, <[hidden email]> wrote:
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

What a surprise. Removing _DEBUG did the trick. I was under the impression _DEBUG only controls Microsoft STL impl's debug iterator stuff but apparently it also makes the resulting exe link with /MD rather than /MDd. Guess that was wrong.

So yes, problem solved.

Thank you!

Stephan

--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
http://public.kitware.com/mailman/listinfo/cmake
Reply | Threaded
Open this post in threaded view
|

Re: Force MSVC runtime for debug builds

Kim Kryger
I ran into this today. I tried to use /MD to link against a custom md library, but because _DEBUG was defined *by CMake* I couldn't use it out of the box.

Why is CMake defining _DEBUG for MSVC projects anyways? _DEBUG is automatically defined when using /MDd, and I don't believe it should be defining it itself. I've had to remove it for multiple projects because it's making assumptions as to what I want defined.


Default for CMAKE_CXX_FLAGS_DEBUG_INIT
/D_DEBUG /MDd /Zi /Ob0 /Od /RTC1

I don't think that first define should be there. 


Reference: 
https://github.com/Kitware/CMake/blob/5d29506811c5b75ae48e12de6c317f6440874215/Modules/Platform/Windows-MSVC.cmake#L294


On Fri, Nov 11, 2016 at 9:42 AM, Stephan Menzel <[hidden email]> wrote:

On Tue, Nov 8, 2016 at 4:21 PM, <[hidden email]> wrote:
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

What a surprise. Removing _DEBUG did the trick. I was under the impression _DEBUG only controls Microsoft STL impl's debug iterator stuff but apparently it also makes the resulting exe link with /MD rather than /MDd. Guess that was wrong.

So yes, problem solved.

Thank you!

Stephan

--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
http://public.kitware.com/mailman/listinfo/cmake


--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
http://public.kitware.com/mailman/listinfo/cmake
Reply | Threaded
Open this post in threaded view
|

Re: Force MSVC runtime for debug builds

Mueller-Roemer, Johannes Sebastian

I agree that CMake should not be defining _DEBUG explicitly, as it is used by Microsoft’s headers to match the chosen runtime.

Maybe this warrants a bug report on gitlab.

 

From: CMake [mailto:[hidden email]] On Behalf Of Kim Kryger
Sent: Monday, November 14, 2016 22:05
To: Stephan Menzel <[hidden email]>
Cc: cmake <[hidden email]>
Subject: Re: [CMake] Force MSVC runtime for debug builds

 

I ran into this today. I tried to use /MD to link against a custom md library, but because _DEBUG was defined *by CMake* I couldn't use it out of the box.

 

Why is CMake defining _DEBUG for MSVC projects anyways? _DEBUG is automatically defined when using /MDd, and I don't believe it should be defining it itself. I've had to remove it for multiple projects because it's making assumptions as to what I want defined.

 

 

Default for CMAKE_CXX_FLAGS_DEBUG_INIT

/D_DEBUG /MDd /Zi /Ob0 /Od /RTC1

 

I don't think that first define should be there. 

 

 

Reference: 

https://github.com/Kitware/CMake/blob/5d29506811c5b75ae48e12de6c317f6440874215/Modules/Platform/Windows-MSVC.cmake#L294

 

 

On Fri, Nov 11, 2016 at 9:42 AM, Stephan Menzel <[hidden email]> wrote:

 

On Tue, Nov 8, 2016 at 4:21 PM, <[hidden email]> wrote:

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

 

What a surprise. Removing _DEBUG did the trick. I was under the impression _DEBUG only controls Microsoft STL impl's debug iterator stuff but apparently it also makes the resulting exe link with /MD rather than /MDd. Guess that was wrong.

 

So yes, problem solved.

 

Thank you!

 

Stephan


--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
http://public.kitware.com/mailman/listinfo/cmake

 


--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
http://public.kitware.com/mailman/listinfo/cmake
Reply | Threaded
Open this post in threaded view
|

Re: Force MSVC runtime for debug builds

Kim Kryger

On Tue, Nov 15, 2016 at 3:47 AM, Mueller-Roemer, Johannes Sebastian <[hidden email]> wrote:

I agree that CMake should not be defining _DEBUG explicitly, as it is used by Microsoft’s headers to match the chosen runtime.

Maybe this warrants a bug report on gitlab.

 

From: CMake [mailto:[hidden email]] On Behalf Of Kim Kryger
Sent: Monday, November 14, 2016 22:05
To: Stephan Menzel <[hidden email]>
Cc: cmake <[hidden email]>
Subject: Re: [CMake] Force MSVC runtime for debug builds

 

I ran into this today. I tried to use /MD to link against a custom md library, but because _DEBUG was defined *by CMake* I couldn't use it out of the box.

 

Why is CMake defining _DEBUG for MSVC projects anyways? _DEBUG is automatically defined when using /MDd, and I don't believe it should be defining it itself. I've had to remove it for multiple projects because it's making assumptions as to what I want defined.

 

 

Default for CMAKE_CXX_FLAGS_DEBUG_INIT

/D_DEBUG /MDd /Zi /Ob0 /Od /RTC1

 

I don't think that first define should be there. 

 

 

Reference: 

https://github.com/Kitware/CMake/blob/5d29506811c5b75ae48e12de6c317f6440874215/Modules/Platform/Windows-MSVC.cmake#L294

 

 

On Fri, Nov 11, 2016 at 9:42 AM, Stephan Menzel <[hidden email]> wrote:

 

On Tue, Nov 8, 2016 at 4:21 PM, <[hidden email]> wrote:

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

 

What a surprise. Removing _DEBUG did the trick. I was under the impression _DEBUG only controls Microsoft STL impl's debug iterator stuff but apparently it also makes the resulting exe link with /MD rather than /MDd. Guess that was wrong.

 

So yes, problem solved.

 

Thank you!

 

Stephan


--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
http://public.kitware.com/mailman/listinfo/cmake

 



--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
http://public.kitware.com/mailman/listinfo/cmake