# Troubles overriding MT/MD compilation flags

**URL:** https://discourse.cmake.org/t/troubles-overriding-mt-md-compilation-flags/9248
**Category:** Usage
**Tags:** os:windows
**Created:** [October 23, 2023, 11:10am UTC](https://discourse.cmake.org/t/troubles-overriding-mt-md-compilation-flags/9248 "2023-10-23T11:10:06Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Retro52](https://discourse.cmake.org/user_avatar/discourse.cmake.org/retro52/32/3937_2.png) [@Retro52](https://discourse.cmake.org/u/Retro52)
#### Post date: [October 23, 2023, 11:10am UTC](https://discourse.cmake.org/t/troubles-overriding-mt-md-compilation-flags/9248/1 "2023-10-23T11:10:06Z")

</div>

Hi!  
I was working on transferring the project from using msbuild/make to cmake, and I encountered a problem overriding the -MD/-MDd compilation options

The FAQ | Wiki suggests using either manual replace or cmake flags override:  
[https://gitlab.kitware.com/cmake/community/-/wikis/FAQ#how-can-i-build-my-msvc-application-with-a-static-runtime](https://gitlab.kitware.com/cmake/community/-/wikis/FAQ#how-can-i-build-my-msvc-application-with-a-static-runtime)

However, at least for my configuration (CMake version 3.24.2, Ninja generator bundled with CLion 2022.3.2 version 1.10.2) none of it worked.

I have the following C/CXX overrides:

```auto
if(MSVC)
    set(CMAKE_C_FLAGS_DEBUG_INIT "/D_DEBUG /MTd /Zi /Ob0 /Od /RTC1")
    set(CMAKE_C_FLAGS_MINSIZEREL_INIT "/MT /O1 /Ob1 /D NDEBUG")
    set(CMAKE_C_FLAGS_RELEASE_INIT "/MT /O2 /Ob2 /D NDEBUG")
    set(CMAKE_C_FLAGS_RELWITHDEBINFO_INIT "/MT /Zi /O2 /Ob1 /D NDEBUG")
endif()

if(MSVC)
    set(CMAKE_CXX_FLAGS_DEBUG_INIT "/D_DEBUG /MTd /Zi /Ob0 /Od /RTC1")
    set(CMAKE_CXX_FLAGS_MINSIZEREL_INIT "/MT /O1 /Ob1 /D NDEBUG")
    set(CMAKE_CXX_FLAGS_RELEASE_INIT "/MT /O2 /Ob2 /D NDEBUG")
    set(CMAKE_CXX_FLAGS_RELWITHDEBINFO_INIT "/MT /Zi /O2 /Ob1 /D NDEBUG")
endif()

```

These are set using:

```auto
# It is important to setup these flags before declaring client project
set(CMAKE_USER_MAKE_RULES_OVERRIDE "${CMAKE_SOURCE_DIR}/cmake/Setup/CFlagsOverride.cmake")
set(CMAKE_USER_MAKE_RULES_OVERRIDE_CXX "${CMAKE_SOURCE_DIR}/cmake/Setup/CXXFlagsOverride.cmake")

```

If I use my default generator - Visual Studio 16 2019 - it seems to work just fine  
But for some reason, Ninja seems to add -MDd manually, resulting in unexpected behavior:

Below is an example of such an override, from CMake generation logs:

```auto
			"compileCommandFragments" : 
			[
				{
					"fragment" : "/DWIN32 /D_WINDOWS /GR /EHsc /D_DEBUG /MTd /Zi /Ob0 /Od /RTC1 -std:c++14 -MDd"
				},
...

```

I am pretty sure I do not override this anywhere else, as well as I am sure that none of my subprojects does, so it seems to me like a sort of generator bug or something.

---

<div class="post-metadata">

### Author: ![Retro52](https://discourse.cmake.org/user_avatar/discourse.cmake.org/retro52/32/3937_2.png) [@Retro52](https://discourse.cmake.org/u/Retro52)
#### Post date: [October 23, 2023, 12:21pm UTC](https://discourse.cmake.org/t/troubles-overriding-mt-md-compilation-flags/9248/2 "2023-10-23T12:21:02Z")

</div>

For anyone who will meet the same problem: it seems that CMake implicitly adds CMAKE\_MSVC\_RUNTIME\_LIBRARY as a compilation option parameter, and, by default, it seems to be evaluated as -MDd (The default value of the variable seems to be an empty string, tho I’m pretty sure there is some sort of naughty check, that adds the default value (-MD/-MDd) instead).  
So, the best option here is to manually specify CMAKE\_MSVC\_RUNTIME\_LIBRARY to something like this:

```auto
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")

```
