CMake 错误:找到的包版本为 1.2.3,但所需版本为 2.0.0(位于 /usr/lib/cmake/MyPackage/MyPackageConfig.cmake)。
CMake Error: Found package version 1.2.3 but required version is 2.0.0 (found /usr/lib/cmake/MyPackage/MyPackageConfig.cmake).
ID: cmake/find-package-version-inequality
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| CMake 3.10 | active | — | — | — |
| CMake 3.16 | active | — | — | — |
| CMake 3.24 | active | — | — | — |
根因分析
使用 EXACT 或版本范围的 find_package() 找到了不满足所需版本约束的包配置文件,通常是由于安装了多个版本或版本指定不正确。
English
find_package() with EXACT or version range finds a package configuration file whose version does not satisfy the required version constraint, often due to multiple installed versions or incorrect version specification.
官方文档
https://cmake.org/cmake/help/latest/command/find_package.html#version-selection解决方案
-
安装所需版本的包,例如 `apt-get install mypackage=2.0.0` 或从源码构建,然后使用 `rm -rf CMakeCache.txt CMakeFiles/` 清理 CMake 缓存。
-
仅在需要精确版本时使用 `find_package(MyPackage 2.0.0 EXACT)`,或者如果 API 向后兼容,则移除 EXACT 以允许兼容版本。
-
在 find_package() 之前设置 `set(MyPackage_DIR "/path/to/correct/version")`,强制 CMake 搜索包含所需版本的特定目录。
无效尝试
常见但无效的做法:
-
Omitting version argument in find_package()
80% 失败
Removing the version requirement from find_package() may allow the build to proceed but could cause runtime incompatibilities with the older library.
-
Modifying the package's Version.cmake file to report a fake version
95% 失败
Manually editing the package's version file to report a higher version may bypass the error but corrupts the package's metadata.
-
Changing CMAKE_FIND_PACKAGE_SORT_DIRECTION
70% 失败
Setting CMAKE_FIND_PACKAGE_SORT_DIRECTION to DEC may change the order of found packages but does not solve version mismatch if only one version is installed.