CMake Error: Policy CMP0091 is not set: MSVC runtime library flags are selected by the CMAKE_MSVC_RUNTIME_LIBRARY variable.
ID: cmake/missing-override-policy-visibility-inline
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| CMake 3.15 | active | — | — | — |
| CMake 3.16 | active | — | — | — |
| CMake 3.20 | active | — | — | — |
| Visual Studio 2019 | active | — | — | — |
| Visual Studio 2022 | active | — | — | — |
Root Cause
CMake 3.15+ introduces CMP0091 to control MSVC runtime library selection via CMAKE_MSVC_RUNTIME_LIBRARY, but the policy is not explicitly set, causing silent fallback to older behavior that may mismatch with project expectations.
generic中文
CMake 3.15+ 引入了 CMP0091 策略,通过 CMAKE_MSVC_RUNTIME_LIBRARY 变量控制 MSVC 运行时库的选择,但未显式设置该策略,导致静默回退到旧行为,可能与项目预期不匹配。
Official Documentation
https://cmake.org/cmake/help/latest/policy/CMP0091.htmlWorkarounds
-
90% success Add `cmake_policy(SET CMP0091 NEW)` before any project() call in the top-level CMakeLists.txt, then set CMAKE_MSVC_RUNTIME_LIBRARY to the desired value (e.g., MultiThreadedDLL).
Add `cmake_policy(SET CMP0091 NEW)` before any project() call in the top-level CMakeLists.txt, then set CMAKE_MSVC_RUNTIME_LIBRARY to the desired value (e.g., MultiThreadedDLL).
-
85% success Use `set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreadedDLL")` and `cmake_policy(SET CMP0091 NEW)` inside a toolchain file for cross-platform builds.
Use `set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreadedDLL")` and `cmake_policy(SET CMP0091 NEW)` inside a toolchain file for cross-platform builds.
-
75% success If using CMake 3.14 or older, upgrade to 3.15+ and adopt the new policy; otherwise, avoid using CMAKE_MSVC_RUNTIME_LIBRARY and rely on manual flag management.
If using CMake 3.14 or older, upgrade to 3.15+ and adopt the new policy; otherwise, avoid using CMAKE_MSVC_RUNTIME_LIBRARY and rely on manual flag management.
中文步骤
在顶层 CMakeLists.txt 中的任何 project() 调用之前添加 `cmake_policy(SET CMP0091 NEW)`,然后将 CMAKE_MSVC_RUNTIME_LIBRARY 设置为所需值(例如 MultiThreadedDLL)。
在工具链文件中使用 `set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreadedDLL")` 和 `cmake_policy(SET CMP0091 NEW)` 进行跨平台构建。
如果使用 CMake 3.14 或更早版本,升级到 3.15+ 并采用新策略;否则,避免使用 CMAKE_MSVC_RUNTIME_LIBRARY,依赖手动标志管理。
Dead Ends
Common approaches that don't work:
-
Setting CMAKE_MSVC_RUNTIME_LIBRARY directly without cmake_policy(SET CMP0091 NEW)
70% fail
Manually setting CMAKE_MSVC_RUNTIME_LIBRARY without setting the policy may still produce the warning or error, as the policy governs whether the variable is honored.
-
Hardcoding MSVC runtime flags in CMAKE_CXX_FLAGS
85% fail
Adding /MD or /MT flags manually via CMAKE_CXX_FLAGS can conflict with CMake's runtime library selection, causing linker errors or undefined behavior.
-
Suppressing the warning with cmake_policy(SET CMP0091 OLD)
60% fail
Ignoring the warning may allow the build to proceed, but the resulting binaries may link against mismatched runtime libraries, leading to crashes.