Setting the MSVC runtime in CMake

Solution 1:

It seems all the while I was working on this, I forgot to remove the bad CMake configuration I'm trying to replace.

Further down the build script, I had left this little bugger:

set(CMAKE_CXX_FLAGS_DEBUG
  "/DWIN32 /D_WINDOWS /EHsc /WX /wd4355 /wd4251 /wd4250 /wd4996"
  CACHE STRING "Debug compiler flags" FORCE
)

Basically, I was overriding the results of by configure_msvc_runtime() macro with build flags that did not set the MSVC runtime.

Solution 2:

This functionality will be improved with the release of cmake-3.15.

  • CMAKE_MSVC_RUNTIME_LIBRARY
  • CMP0091

It should be a matter of setting CMAKE_MSVC_RUNTIME_LIBRARY, for example (from docs) to set "multi-threaded statically-linked runtime library with or without debug information depending on the configuration":

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

Solution 3:

I took your code and generalized it to work for every existing configuration and not just for Debug/Release/RelWithDebInfo/MinSizeRel.

Also I made it to work with gcc too - check it out here