cmake config_error ai_generated true

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

CMake Error: INTERFACE_LIBRARY targets may only have INTERFACE properties. The SOURCES property of target "mylib" is not allowed.

ID: cmake/interface-library-sources-ignored

其他格式: JSON · Markdown 中文 · English
92%修复率
88%置信度
1证据数
2023-03-15首次发现

版本兼容性

版本状态引入弃用备注
CMake 3.10 active — — —
CMake 3.16 active — — —
CMake 3.22 active — — —
CMake 3.27 active — — —

根因分析

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

English

Attempting to set source files on an INTERFACE library target, which by definition cannot have compile sources.

generic

官方文档

https://cmake.org/cmake/help/latest/manual/cmake-buildsystem.7.html#interface-libraries

解决方案

  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.

无效尝试

常见但无效的做法:

  1. Adding SOURCES property with set_property(TARGET mylib PROPERTY SOURCES ...) to work around the error 95% 失败

    INTERFACE libraries are header-only or pure-interface; they have no compiled sources, so setting SOURCES violates the target type.

  2. Changing the target type to STATIC or SHARED but keeping the same interface logic 70% 失败

    Changing target type changes the build semantics; INTERFACE libraries are intended for header-only or transitive dependencies, not compiled code.

  3. Deleting the target entirely and re-adding it with sources as a different target 80% 失败

    This loses the interface propagation properties and may break dependency chains.