cmake config_error ai_generated true

CMake Error: add_executable cannot create target "myapp" because no source files were specified.

ID: cmake/add-executable-no-sources

Also available as: JSON · Markdown · 中文
95%Fix Rate
90%Confidence
1Evidence
2023-03-22First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.10 active
3.16 active
3.22 active
3.26 active

Root Cause

The add_executable command was called without any source file arguments, either due to a missing variable expansion or an empty list.

generic

中文

add_executable 命令在未指定任何源文件参数的情况下被调用,可能是由于变量展开缺失或列表为空。

Official Documentation

https://cmake.org/cmake/help/latest/command/add_executable.html

Workarounds

  1. 95% success Check the variable that holds source files: message(STATUS "SOURCES=${SOURCES}") before add_executable. Ensure it is not empty. For example, if SOURCES is set via file(GLOB ...), verify the glob pattern matches files.
    Check the variable that holds source files: message(STATUS "SOURCES=${SOURCES}") before add_executable. Ensure it is not empty. For example, if SOURCES is set via file(GLOB ...), verify the glob pattern matches files.
  2. 90% success Explicitly list source files in add_executable: add_executable(myapp main.cpp utils.cpp). Avoid using empty variables.
    Explicitly list source files in add_executable: add_executable(myapp main.cpp utils.cpp). Avoid using empty variables.
  3. 85% success If using a macro or function, ensure arguments are passed correctly: function(my_add_executable name) add_executable(${name} ${ARGN}) endfunction(). Call with at least one source.
    If using a macro or function, ensure arguments are passed correctly: function(my_add_executable name) add_executable(${name} ${ARGN}) endfunction(). Call with at least one source.

中文步骤

  1. 检查保存源文件的变量:在 add_executable 之前添加 message(STATUS "SOURCES=${SOURCES}")。确保它不为空。例如,如果 SOURCES 是通过 file(GLOB ...) 设置的,请验证 glob 模式与文件匹配。
  2. 在 add_executable 中显式列出源文件:add_executable(myapp main.cpp utils.cpp)。避免使用空变量。
  3. 如果使用宏或函数,请确保正确传递参数:function(my_add_executable name) add_executable(${name} ${ARGN}) endfunction()。调用时至少提供一个源文件。

Dead Ends

Common approaches that don't work:

  1. Adding a dummy source file like 'main.cpp' without checking if it exists 80% fail

    If the file does not exist, CMake will later fail with 'Cannot find source file'. The root cause (empty variable) remains unaddressed.

  2. Removing the add_executable call entirely and relying on add_library 90% fail

    The project needs an executable target; removing it breaks the build logic, and the error shifts to missing target.

  3. Setting CMAKE_MINIMUM_REQUIRED to a lower version 95% fail

    The error is not version-dependent; it is a syntax/argument error that is caught at parse time regardless of CMake version.