Usage of "CMAKE_MSVC_RUNTIME_LIBRARY" prior to project() or not?

Hi,

In CMake, some options (like CMAKE_OSX_ARCHITECTURES), should be set before the first “project()” command, which is pretty clear.

But, I need a somehow similar option, CMAKE_MSVC_RUNTIME_LIBRARY, and I want to be 100% sure that I understand the documentation correctly.

The documentation states that:
This variable has effect only when policy CMP0091 is set to NEW prior to the first project() or enable_language() command that enables a language using a compiler targeting the MSVC ABI.

And then about the policy:
This policy was introduced in CMake version 3.15. It may be set by cmake_policy() or cmake_minimum_required(). If it is not set, CMake does not warn, and uses OLD behavior.

So my question is: is the following code correct, or should I do something else before using “project()” command?

cmake_minimum_required(VERSION 3.15)
project(test C CXX)
set(CMAKE_MSVC_RUNTIME_LIBRARY “MultiThreaded$<$CONFIG:Debug:Debug>”)

Thank you

I think only the policy needs to be set before compiler detection (so that the configuration-based flags can avoid mentioning any of the runtime selection flags). The variable/property can be set at any point after that.

Thank you for your answer, I understood this way, also.

But after this, seems that if set “cmake_minimum_required”, the policy is not needed anymore, and I am not sure about this. Also in “Professional CMake” it states something similar.

Yes, saying that your minimum version is 3.X opts into the NEW behavior of all policies introduced as of 3.X.