# CMake错误：目标'mylib'是INTERFACE_LIBRARY类型，但target_link_libraries使用了非INTERFACE关键字

- **ID:** `cmake/target-links-interface-libraries-with-non-interface-keyword`
- **领域:** cmake
- **类别:** type_error
- **验证级别:** ai_generated
- **修复率:** 95%

## 根因

定义为INTERFACE（仅头文件）库的目标必须在target_link_libraries中仅使用INTERFACE关键字，但使用了PRIVATE或PUBLIC。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| CMake 3.20 | active | — | — |
| CMake 3.22 | active | — | — |
| CMake 3.28 | active | — | — |

## 解决方案

1. ```
   Replace PUBLIC with INTERFACE in target_link_libraries for the INTERFACE library
   ```
2. ```
   Convert the INTERFACE library to a STATIC library with a dummy source file if link dependencies are needed
   ```
3. ```
   Use target_link_options instead of target_link_libraries for compiler-specific flags
   ```

## 无效尝试

- **Change the target type to STATIC or SHARED** — Changing target type may break the build if the library is meant to be header-only. (50% 失败率)
- **Use PUBLIC keyword but add dummy source files** — PUBLIC is still not allowed for INTERFACE libraries; only INTERFACE keyword is valid. (90% 失败率)
- **Remove all target_link_libraries calls** — This may lose necessary link dependencies for consumers of the header-only library. (30% 失败率)
