CMake 错误:未设置策略 CMP0074:find_package 使用 <PackageName>_ROOT 变量。运行 "cmake --help-policy CMP0074" 查看策略详情。
CMake Error: Policy CMP0074 is not set: find_package uses <PackageName>_ROOT variables. Run "cmake --help-policy CMP0074" for policy details.
ID: cmake/policy-cmp0074-not-set-find-package
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| cmake 3.12 | active | — | — | — |
| cmake 3.15 | active | — | — | — |
| cmake 3.20 | active | — | — | — |
| cmake 3.25 | active | — | — | — |
根因分析
CMake 3.12+ 引入了新策略 CMP0074,用于控制 find_package 是否遵循 <PackageName>_ROOT 环境变量。如果未显式设置该策略,CMake 会报错以强制开发者决定。
English
CMake 3.12+ has a new policy CMP0074 that controls whether find_package honors <PackageName>_ROOT environment variables. If the policy is not set explicitly, CMake issues an error to force the developer to decide.
官方文档
https://cmake.org/cmake/help/latest/policy/CMP0074.html解决方案
-
在任何 find_package 调用之前添加:cmake_policy(SET CMP0074 NEW) 以启用新行为。
-
添加 cmake_policy(SET CMP0074 OLD) 以使用旧行为(忽略 <PackageName>_ROOT)。
-
设置 cmake_minimum_required(VERSION 3.11) 以完全避免该策略,但这可能不适用于现代项目。
无效尝试
常见但无效的做法:
-
70% 失败
CMAKE_POLICY_DEFAULT_CMP0074 is a variable, not a policy command. Must use cmake_policy(SET CMP0074 NEW) explicitly.
-
95% 失败
Without policy set, CMake will still error out. The policy must be set before find_package.
-
50% 失败
Policy CMP0074 exists in 3.12+; using an older version may suppress the error but also lose new features. Not a proper fix.