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

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

## 根因

find_package() 找到了一个包配置文件，但其版本不满足调用中指定的版本要求。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| cmake 3.20.0 | active | — | — |
| cmake 3.21.0 | active | — | — |
| cmake 3.22.0 | active | — | — |

## 解决方案

1. ```
   安装满足要求的较新版本的包。例如，在 Ubuntu 上：

sudo apt-get install mypackage=2.0.0

或从源码构建：

git clone https://github.com/example/mypackage.git -b v2.0.0
cd mypackage && mkdir build && cd build
cmake .. && sudo make install
   ```
2. ```
   如果 API 向后兼容，可以在 find_package 中指定较低的版本要求：

find_package(MyPackage 1.2.3 REQUIRED)

或使用 EXACT 固定到特定版本：

find_package(MyPackage 2.0.0 EXACT REQUIRED)
   ```

## 无效尝试

- **Delete the package config file and let CMake search again** — CMake will find the same file again if it's the only one; deleting it may break other projects that depend on it. (80% 失败率)
- **Modify MyPackageConfigVersion.cmake to report a higher version** — Tampering with version files causes silent runtime errors due to API incompatibility; dangerous and unsupported. (95% 失败率)
