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
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| CMake 3.10 | active | — | — | — |
| CMake 3.16 | active | — | — | — |
| CMake 3.24 | active | — | — | — |
Root Cause
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.
generic中文
使用 EXACT 或版本范围的 find_package() 找到了不满足所需版本约束的包配置文件,通常是由于安装了多个版本或版本指定不正确。
Official Documentation
https://cmake.org/cmake/help/latest/command/find_package.html#version-selectionWorkarounds
-
90% success Install the required version of the package, e.g., `apt-get install mypackage=2.0.0` or build from source, then clean the CMake cache with `rm -rf CMakeCache.txt CMakeFiles/`.
Install the required version of the package, e.g., `apt-get install mypackage=2.0.0` or build from source, then clean the CMake cache with `rm -rf CMakeCache.txt CMakeFiles/`.
-
85% success Use `find_package(MyPackage 2.0.0 EXACT)` only if the exact version is required, or remove EXACT and allow compatible versions if the API is backward-compatible.
Use `find_package(MyPackage 2.0.0 EXACT)` only if the exact version is required, or remove EXACT and allow compatible versions if the API is backward-compatible.
-
80% success Set `set(MyPackage_DIR "/path/to/correct/version")` before find_package() to force CMake to search the specific directory containing the desired version.
Set `set(MyPackage_DIR "/path/to/correct/version")` before find_package() to force CMake to search the specific directory containing the desired version.
中文步骤
安装所需版本的包,例如 `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 搜索包含所需版本的特定目录。
Dead Ends
Common approaches that don't work:
-
Omitting version argument in find_package()
80% fail
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% fail
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% fail
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.