cmake config_error ai_generated true

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

其他格式: JSON · Markdown 中文 · English
85%修复率
87%置信度
1证据数
2023-09-15首次发现

版本兼容性

版本状态引入弃用备注
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).

generic

官方文档

https://cmake.org/cmake/help/latest/command/find_package.html#version-selection

解决方案

  1. 更新 find_package 调用以接受已安装的版本:find_package(MyPackage 2.1.0 EXACT) 或如果不是严格必要,则移除 EXACT。
  2. 安装所需的精确版本包:在 Ubuntu 上,使用 apt-get install mypackage=2.0.0 或从源码构建正确的标签版本。
  3. 如果允许,使用版本范围:find_package(MyPackage 2.0.0...2.1.0 EXACT) 无效;改为使用不带 EXACT 的 find_package(MyPackage 2.0.0) 并添加自定义版本检查。

无效尝试

常见但无效的做法:

  1. 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.

  2. 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.

  3. 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.