cmake config_error ai_generated true

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

ID: cmake/interface-library-sources-property

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

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
CMake 3.10+ active
CMake 3.20+ active
CMake 3.28+ active

Root Cause

An INTERFACE library target was mistakenly given source files via set_target_properties or add_library with source arguments, which is not allowed because INTERFACE libraries are header-only and have no build artifacts.

generic

中文

INTERFACE 库目标被错误地通过 set_target_properties 或 add_library 赋予源文件参数,这是不允许的,因为 INTERFACE 库是仅头文件的,没有构建产物。

Official Documentation

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

Workarounds

  1. 85% success Remove source files from add_library call and use target_sources with INTERFACE scope if needed, or convert to a STATIC library if sources are required.
    Remove source files from add_library call and use target_sources with INTERFACE scope if needed, or convert to a STATIC library if sources are required.
  2. 75% success Define the target as an OBJECT library if you need to collect object files, then link with other targets.
    Define the target as an OBJECT library if you need to collect object files, then link with other targets.
  3. 90% success Use target_include_directories with INTERFACE scope to propagate headers without sources.
    Use target_include_directories with INTERFACE scope to propagate headers without sources.

中文步骤

  1. Remove source files from add_library call and use target_sources with INTERFACE scope if needed, or convert to a STATIC library if sources are required.
  2. Define the target as an OBJECT library if you need to collect object files, then link with other targets.
  3. Use target_include_directories with INTERFACE scope to propagate headers without sources.

Dead Ends

Common approaches that don't work:

  1. Adding SOURCES property via set_property with INTERFACE scope 95% fail

    The SOURCES property is inherently non-INTERFACE; even setting it as INTERFACE fails because CMake rejects it for INTERFACE libraries.

  2. Changing library type to STATIC and linking sources 70% fail

    This changes the library's purpose and may break header-only design, causing unnecessary compilation and link errors.

  3. Ignoring the error and using add_library with sources anyway 100% fail

    CMake will abort the configuration; the build cannot proceed without fixing the target definition.