0

I use Visual Studio 2017. I have a solution which has a different options for Debug and Release configurations, for example a runtime library. I want to create a CMakeLists.txt which can be used with Visual Studio generator, for example:

cmake -G "Visual Studio 15 2017" ..
cmake --build . --config RelWithDebInfo --parallel 8

I need to set a correct runtime library in CMakeLists.txt. Should I use settings from the Release configuration in Visual Studio ? How CMAKE_BUILD_TYPE=RelWithDebInfo maps to Visual Studio configurations ?

Irbis
  • 1,432
  • 1
  • 13
  • 39

1 Answers1

0

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

Alex Reinking
  • 16,724
  • 5
  • 52
  • 86