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
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:
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:
# 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:
"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.