# CMake 错误：CPack 组件 "runtime" 的版本为 1.0.0，但组件 "headers" 的版本为 2.0.0。所有组件必须具有相同的版本。

- **ID:** `cmake/cpack-component-version-mismatch`
- **领域:** cmake
- **类别:** install_error
- **验证级别:** ai_generated
- **修复率:** 85%

## 根因

当使用带有组件的 CPack 时，每个组件的版本必须与项目版本匹配；当跨组件不一致地设置 CPACK_COMPONENT_<compName>_VERSION 时会发生不匹配。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| CMake 3.10 | active | — | — |
| CMake 3.18 | active | — | — |
| CPack 3.20 | active | — | — |

## 解决方案

1. ```
   将所有组件版本设置为相同值，例如 `set(CPACK_COMPONENT_RUNTIME_VERSION "2.0.0")` 和 `set(CPACK_COMPONENT_HEADERS_VERSION "2.0.0")` 以匹配项目版本。
   ```
2. ```
   移除组件的显式版本设置，通过不设置任何 CPACK_COMPONENT_*_VERSION 变量来依赖项目版本。
   ```
3. ```
   使用 `set(CPACK_COMPONENTS_ALL_IN_ONE_PACKAGE ON)` 将所有组件合并到一个包中，绕过版本检查。
   ```

## 无效尝试

- **Deleting all CPACK_COMPONENT_*_VERSION variables** — Removing version specifications from all components may default to the project version, but CPack may still fail if any component has an explicit version set elsewhere. (70% 失败率)
- **Setting only CPACK_PACKAGE_VERSION without adjusting component versions** — Setting a global CPACK_PACKAGE_VERSION may not override component-specific versions, leading to persistent mismatches. (80% 失败率)
- **Disabling component grouping in CPack** — Using CPack without components (GROUPING IGNORE) may suppress the error but creates a monolithic package that defeats the purpose of component-based installation. (75% 失败率)
