cmake config_error ai_generated true

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

Also available as: JSON · Markdown · 中文
92%Fix Rate
88%Confidence
1Evidence
2023-03-15First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
CMake 3.10 active — — —
CMake 3.16 active — — —
CMake 3.22 active — — —
CMake 3.27 active — — —

Root Cause

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

generic

中文

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

Official Documentation

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

Workarounds

  1. 90% success Remove SOURCES from the INTERFACE library. If you need compiled sources, use add_library(mylib STATIC ...) or add_library(mylib SHARED ...) instead.
    Remove SOURCES from the INTERFACE library. If you need compiled sources, use add_library(mylib STATIC ...) or add_library(mylib SHARED ...) instead.
  2. 85% success 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.
    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. 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.

Dead Ends

Common approaches that don't work:

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

    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% fail

    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% fail

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