# CMake 错误：INTERFACE_LIBRARY 目标只能具有 INTERFACE 属性。目标 "mylib" 的 SOURCES 属性是不允许的。

- **ID:** `cmake/interface-library-sources-ignored`
- **领域:** cmake
- **类别:** config_error
- **验证级别:** ai_generated
- **修复率:** 92%

## 根因

尝试在 INTERFACE 库目标上设置源文件，而根据定义，INTERFACE 库不能有编译源文件。

## 版本兼容性

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

## 解决方案

1. ```
   Remove SOURCES from the INTERFACE library. If you need compiled sources, use add_library(mylib STATIC ...) or add_library(mylib SHARED ...) instead.
   ```
2. ```
   If you need both interface properties and compiled sources, create two targets: an INTERFACE library for headers and a STATIC/SHARED library for sources, then link them.
   ```

## 无效尝试

- **Adding SOURCES property with set_property(TARGET mylib PROPERTY SOURCES ...) to work around the error** — INTERFACE libraries are header-only or pure-interface; they have no compiled sources, so setting SOURCES violates the target type. (95% 失败率)
- **Changing the target type to STATIC or SHARED but keeping the same interface logic** — Changing target type changes the build semantics; INTERFACE libraries are intended for header-only or transitive dependencies, not compiled code. (70% 失败率)
- **Deleting the target entirely and re-adding it with sources as a different target** — This loses the interface propagation properties and may break dependency chains. (80% 失败率)
