cmake build_error ai_generated true

CMake Error: Cannot find source file: /path/to/src/main.cpp. Tried extensions .c .C .c++ .cc .cpp .cxx .cu .m .M .mm .h .hh .h++ .hm .hpp .hxx .in .txx .f .F .for .f77 .f90 .f95 .f03 .f08.

ID: cmake/target-sources-glob-missing

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

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
CMake 3.10 active
CMake 3.18 active
CMake 3.25 active

Root Cause

A source file listed in target_sources() or add_executable() does not exist at the specified path, often due to a typo, missing file, or incorrect use of generator expressions that resolve to an empty string.

generic

中文

target_sources() 或 add_executable() 中列出的源文件在指定路径不存在,通常是由于拼写错误、文件丢失或生成器表达式解析为空字符串。

Official Documentation

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

Workarounds

  1. 95% success Correct the file path in the CMakeLists.txt, e.g., change `target_sources(mylib PRIVATE src/main.cpp)` to the actual path: `target_sources(mylib PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src/main.cpp)`.
    Correct the file path in the CMakeLists.txt, e.g., change `target_sources(mylib PRIVATE src/main.cpp)` to the actual path: `target_sources(mylib PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src/main.cpp)`.
  2. 85% success If the file is generated by a custom command, ensure the generation target runs before the compile step by adding `add_dependencies(mylib generator_target)`.
    If the file is generated by a custom command, ensure the generation target runs before the compile step by adding `add_dependencies(mylib generator_target)`.
  3. 80% success Use `configure_file()` to generate the missing source from a template if it is meant to be created at configure time.
    Use `configure_file()` to generate the missing source from a template if it is meant to be created at configure time.

中文步骤

  1. 在 CMakeLists.txt 中更正文件路径,例如将 `target_sources(mylib PRIVATE src/main.cpp)` 改为实际路径:`target_sources(mylib PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src/main.cpp)`。
  2. 如果文件由自定义命令生成,通过添加 `add_dependencies(mylib generator_target)` 确保生成目标在编译步骤之前运行。
  3. 如果文件本应在配置时创建,使用 `configure_file()` 从模板生成缺失的源文件。

Dead Ends

Common approaches that don't work:

  1. Creating an empty source file at the missing path 85% fail

    Adding the missing file as an empty file may bypass the error but will cause compilation failures due to missing code.

  2. Commenting out the target_sources() call 75% fail

    Removing the source file entry from target_sources() may allow the build to proceed but will exclude the file from compilation, leading to undefined symbols.

  3. Replacing explicit source list with file(GLOB) 70% fail

    Using file(GLOB) to automatically collect sources may mask the issue and lead to unpredictable builds if files are added or removed.