# CMake 错误：找到的包版本为 1.2.3，但所需版本为 2.0.0（位于 /usr/lib/cmake/MyPackage/MyPackageConfig.cmake）。

- **ID:** `cmake/find-package-version-inequality`
- **领域:** cmake
- **类别:** config_error
- **验证级别:** ai_generated
- **修复率:** 92%

## 根因

使用 EXACT 或版本范围的 find_package() 找到了不满足所需版本约束的包配置文件，通常是由于安装了多个版本或版本指定不正确。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| CMake 3.10 | active | — | — |
| CMake 3.16 | active | — | — |
| CMake 3.24 | active | — | — |

## 解决方案

1. ```
   安装所需版本的包，例如 `apt-get install mypackage=2.0.0` 或从源码构建，然后使用 `rm -rf CMakeCache.txt CMakeFiles/` 清理 CMake 缓存。
   ```
2. ```
   仅在需要精确版本时使用 `find_package(MyPackage 2.0.0 EXACT)`，或者如果 API 向后兼容，则移除 EXACT 以允许兼容版本。
   ```
3. ```
   在 find_package() 之前设置 `set(MyPackage_DIR "/path/to/correct/version")`，强制 CMake 搜索包含所需版本的特定目录。
   ```

## 无效尝试

- **Omitting version argument in find_package()** — Removing the version requirement from find_package() may allow the build to proceed but could cause runtime incompatibilities with the older library. (80% 失败率)
- **Modifying the package's Version.cmake file to report a fake version** — Manually editing the package's version file to report a higher version may bypass the error but corrupts the package's metadata. (95% 失败率)
- **Changing CMAKE_FIND_PACKAGE_SORT_DIRECTION** — 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. (70% 失败率)
