I'm using CMake on windows with the Windows SDK and NMake Makefiles.
By default it compiles with the /MD compiler switch.
How can I change it to compile with the /MT switch instead?
You can modify the CMAKE_<LANG>_FLAGS_<CONFIG> variables:
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MT")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MTd")
If your CMake flags already contain /MD, you can ensure that the above commands are executed after the point at which /MD is inserted (the later addition of /MT overrides the conflicting existing option), or you can set the flags from scratch:
set(CMAKE_CXX_FLAGS_RELEASE "/MT")
set(CMAKE_CXX_FLAGS_DEBUG "/MTd")
Or alternatively, you could replace the existing /MD and /MDd values with /MT and /MTd respectively by doing something like:
set(CompilerFlags
CMAKE_CXX_FLAGS
CMAKE_CXX_FLAGS_DEBUG
CMAKE_CXX_FLAGS_RELEASE
CMAKE_C_FLAGS
CMAKE_C_FLAGS_DEBUG
CMAKE_C_FLAGS_RELEASE
)
foreach(CompilerFlag ${CompilerFlags})
string(REPLACE "/MD" "/MT" ${CompilerFlag} "${${CompilerFlag}}")
endforeach()
string(REPLACE...) command - it's fixed now.
cmake -D CMAKE_CXX_FLAGS="/MT" ..?
Commented
Dec 3, 2020 at 0:13
CMake finally added proper support for this in version 3.15 with the MSVC_RUNTIME_LIBRARY target property:
cmake_minimum_required(VERSION 3.15)
cmake_policy(SET CMP0091 NEW)
project(my_project)
add_executable(foo foo.c)
set_property(TARGET foo PROPERTY
MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
You can also specify a global default by setting the CMAKE_MSVC_RUNTIME_LIBRARY variable instead.
cmake_policy(SET CMP0091 NEW) on 3.16.3, is it really required?
Commented
Jan 28, 2020 at 3:41
cmake_minimum_required is new enough, you don't need to request the new behavior explicitly. I mostly included the policy in the answer so that people are aware that a policy exists for this feature and look it up if they care about the compatibility implications.
Commented
Jan 28, 2020 at 6:53
It seems that for Visual Studio 15 2017 and CMake 3.12 the way to replace /MD by /MT is by adding this snippet to the CMakeLists.txt file:
if(MSVC)
add_compile_options(
$<$<CONFIG:>:/MT> #---------|
$<$<CONFIG:Debug>:/MTd> #---|-- Statically link the runtime libraries
$<$<CONFIG:Release>:/MT> #--|
)
endif()
I found this solution in the official CMake repository: https://gitlab.kitware.com/cmake/cmake/issues/18390
Edit: As commented by @bernardo-ramos, this block should be added before the add_executable or add_library.
add_executable or add_library
Commented
Dec 19, 2020 at 2:31
I have to use set( ... CACHE ... FORCE) to overwrite MSVC's default cache.
If I do not use this method, MSVC still outputs /MD options.
set(CompilerFlags
CMAKE_CXX_FLAGS
CMAKE_CXX_FLAGS_DEBUG
CMAKE_CXX_FLAGS_RELEASE
CMAKE_CXX_FLAGS_MINSIZEREL
CMAKE_CXX_FLAGS_RELWITHDEBINFO
CMAKE_C_FLAGS
CMAKE_C_FLAGS_DEBUG
CMAKE_C_FLAGS_RELEASE
CMAKE_C_FLAGS_MINSIZEREL
CMAKE_C_FLAGS_RELWITHDEBINFO
)
foreach(CompilerFlag ${CompilerFlags})
string(REPLACE "/MD" "/MT" ${CompilerFlag} "${${CompilerFlag}}")
set(${CompilerFlag} "${${CompilerFlag}}" CACHE STRING "msvc compiler flags" FORCE)
message("MSVC flags: ${CompilerFlag}:${${CompilerFlag}}")
endforeach()
Since CMake 3.15 MSVC_RUNTIME_LIBRARY property does that
See documentation https://cmake.org/cmake/help/latest/prop_tgt/MSVC_RUNTIME_LIBRARY.html
Here is example to set target property accordingly
set_target_properties(${this_target} PROPERTIES
CMAKE_MSVC_RUNTIME_LIBRARY MultiThreaded$<$<CONFIG:Debug>:DebugDLL>
)
This snippet is compatible with multi-config generators (MSbuild, Ninja Multi-Config, Xcode).
It sets non-Debug configuration to use static multi-threaded runtimes and MultiThreadedDebugDLL for Debug configuration.
MultiThreadedDebugDLL builds faster, for more interactive development experience during debugging.
Here is an example https://github.com/ohhmm/openmind/blob/44f0dd7cb619c6f33565e9d07250bfc6581bd32a/cmake/bins.cmake#L123
Check out ucm_set_runtime - this macro will replace the flags for static or dynamic runtime - to see the effects, use ucm_print_flags (also checkout this Stack Overflow question).