# CMake 错误：目标 "mylib" 具有 INTERFACE_LIBRARY 类型，但 target_link_libraries 使用了非 INTERFACE 关键字。

- **ID:** `cmake/target-link-libraries-interface-mismatch`
- **领域:** cmake
- **类别:** type_error
- **验证级别:** ai_generated
- **修复率:** 90%

## 根因

INTERFACE 库目标只能使用 INTERFACE 关键字在 target_link_libraries 中链接，但使用了 PUBLIC 或 PRIVATE。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| CMake 3.12 | active | — | — |
| CMake 3.18 | active | — | — |
| CMake 3.27 | active | — | — |

## 解决方案

1. ```
   将 target_link_libraries(mylib PUBLIC some_dep) 替换为 target_link_libraries(mylib INTERFACE some_dep)。示例：target_link_libraries(mylib INTERFACE some_dep)。
   ```
2. ```
   如果目标应有源文件，将其类型更改为 STATIC 或 SHARED：add_library(mylib STATIC source.cpp)。然后根据需要使用 PUBLIC/PRIVATE。
   ```

## 无效尝试

- **** — INTERFACE libraries are meant for header-only or pure-interface targets; changing type alters build semantics and may require source files. (85% 失败率)
- **** — The dependency is required for consuming targets to find headers or propagate compile definitions. (95% 失败率)
