# CMake 错误：install(EXPORT "MyTargets") 包含目标 "mylib"，但该目标不在导出集中。

- **ID:** `cmake/export-import-target-not-found`
- **领域:** cmake
- **类别:** install_error
- **验证级别:** ai_generated
- **修复率:** 92%

## 根因

install(EXPORT) 命令引用的目标之前没有使用相同的 EXPORT 名称通过 install(TARGETS) 注册。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| CMake 3.10 | active | — | — |
| CMake 3.15 | active | — | — |
| CMake 3.22 | active | — | — |

## 解决方案

1. ```
   确保目标使用相同的 EXPORT 名称安装：install(TARGETS mylib EXPORT MyTargets DESTINATION lib)。然后 install(EXPORT MyTargets DESTINATION lib/cmake)。示例：install(TARGETS mylib EXPORT MyTargets DESTINATION lib) 和 install(EXPORT MyTargets DESTINATION lib/cmake/MyProject)。
   ```
2. ```
   如果目标不应被导出，将其从 install(EXPORT) 调用中移除，或为其创建单独的导出集。
   ```

## 无效尝试

- **** — CMake uses the EXPORT name to group targets; mismatched names cause the target to be omitted from the set. (90% 失败率)
- **** — Without the export, other projects cannot use find_package to locate the installed targets. (95% 失败率)
