CMP0091 cmake config_error ai_generated true

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

Also available as: JSON · Markdown · 中文
90%Fix Rate
85%Confidence
1Evidence
2023-08-15First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
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.html

Workarounds

  1. 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).
  2. 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.
  3. 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.

中文步骤

  1. 在顶层 CMakeLists.txt 中的任何 project() 调用之前添加 `cmake_policy(SET CMP0091 NEW)`,然后将 CMAKE_MSVC_RUNTIME_LIBRARY 设置为所需值(例如 MultiThreadedDLL)。
  2. 在工具链文件中使用 `set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreadedDLL")` 和 `cmake_policy(SET CMP0091 NEW)` 进行跨平台构建。
  3. 如果使用 CMake 3.14 或更早版本,升级到 3.15+ 并采用新策略;否则,避免使用 CMAKE_MSVC_RUNTIME_LIBRARY,依赖手动标志管理。

Dead Ends

Common approaches that don't work:

  1. 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.

  2. 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.

  3. 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.