I need to set a correct runtime library in CMakeLists.txt.
The canonical way of doing this is to use the CMAKE_MSVC_RUNTIME_LIBRARY
variable, which sets the default value of the MSVC_RUNTIME_LIBRARY
target property (which allows for per-target control).
As an example, to use the static runtime library, you can write the following in your CMakeLists.txt (or better, from a toolchain or preset):
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
The $<CONFIG:Debug>
generator expression returns 1
at generation time if the active configuration is "Debug". Then this will reduce to $<1:Debug>
which reduces to Debug
. On other configurations, it will reduce to $<0:Debug>
, which is then the empty string. You can implement your own custom logic using these generator expressions. The $<IF:cond,then,else>
genex will be useful here.
See the documentation for CMAKE_MSVC_RUNTIME_LIBRARY
: https://cmake.org/cmake/help/latest/variable/CMAKE_MSVC_RUNTIME_LIBRARY.html
How CMAKE_BUILD_TYPE=RelWithDebInfo maps to Visual Studio configurations?
Setting CMAKE_BUILD_TYPE
with the Visual Studio generator does absolutely nothing because it is a multi-config generator. It will create one configuration for each name in the CMAKE_CONFIGURATION_TYPES
list variable. There are a host of _<CONFIG>
suffixed variables that adjust configuration-specific settings like flags and target name postfixes.
The full list may be found in the documentation, here (search for <CONFIG>
): https://cmake.org/cmake/help/latest/manual/cmake-variables.7.html