cmake config_error ai_generated true

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-config-version-mismatch

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

版本兼容性

版本状态引入弃用备注
cmake 3.20.0 active
cmake 3.21.0 active
cmake 3.22.0 active

根因分析

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

English

find_package() found a package configuration file, but its version does not satisfy the version requirements specified in the call.

generic

官方文档

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

解决方案

  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)

无效尝试

常见但无效的做法:

  1. Delete the package config file and let CMake search again 80% 失败

    CMake will find the same file again if it's the only one; deleting it may break other projects that depend on it.

  2. Modify MyPackageConfigVersion.cmake to report a higher version 95% 失败

    Tampering with version files causes silent runtime errors due to API incompatibility; dangerous and unsupported.