CMake 错误:找不到请求精确版本 2.0.0 的包 "MyPackage" 的配置文件。找到版本 2.1.0,但需要精确匹配。
CMake Error: Could not find a configuration file for package "MyPackage" that requests exact version 2.0.0. Found version 2.1.0, but exact match required.
ID: cmake/find-package-version-exact-mismatch
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 3.18 | active | — | — | — |
| 3.22 | active | — | — | — |
| 3.25 | active | — | — | — |
| 3.28 | active | — | — | — |
根因分析
find_package 使用了 EXACT 版本要求,但已安装的包具有不同的版本(即使只有次要/补丁版本不同)。
English
find_package is called with EXACT version requirement, but the installed package has a different version (even if minor/patch differ).
官方文档
https://cmake.org/cmake/help/latest/command/find_package.html#version-selection解决方案
-
更新 find_package 调用以接受已安装的版本:find_package(MyPackage 2.1.0 EXACT) 或如果不是严格必要,则移除 EXACT。
-
安装所需的精确版本包:在 Ubuntu 上,使用 apt-get install mypackage=2.0.0 或从源码构建正确的标签版本。
-
如果允许,使用版本范围:find_package(MyPackage 2.0.0...2.1.0 EXACT) 无效;改为使用不带 EXACT 的 find_package(MyPackage 2.0.0) 并添加自定义版本检查。
无效尝试
常见但无效的做法:
-
Manually editing the package's version file to change the version number
90% 失败
Editing the installed package file is a hack that may break other projects depending on the correct version. It is also overwritten on package updates.
-
Using find_package without EXACT but then adding a version check via if()
70% 失败
The project may require EXACT for a reason (e.g., ABI compatibility). Removing EXACT can lead to subtle runtime failures.
-
Installing an older version of the package manually from source
85% 失败
This can cause conflicts with other system packages that depend on the newer version, leading to dependency hell.