cmake build_error ai_generated true

CMake Error: Cannot find source file: /path/to/src/module.cpp. Tried extensions .c .C .c++ .cc .cpp .cxx .cu .m .M .mm .h .hh .h++ .hm .hpp .hxx .in .txx .f .f90 .F .F90 .s .S .asm

ID: cmake/source-file-not-found-alternate-extensions

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

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.20 active
3.22 active
3.24 active
3.26 active
3.28 active

Root Cause

The source file listed in add_executable() or add_library() does not exist on disk. Common causes: file was moved, renamed, deleted, or the path is relative to a wrong directory.

generic

中文

add_executable() 或 add_library() 中列出的源文件在磁盘上不存在。常见原因:文件被移动、重命名、删除,或路径相对于错误的目录。

Official Documentation

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

Workarounds

  1. 90% success Verify the file exists at the exact path. Use absolute paths or correct relative paths from CMAKE_CURRENT_SOURCE_DIR. Example: set(MY_SRC ${CMAKE_CURRENT_SOURCE_DIR}/src/module.cpp) add_executable(myapp ${MY_SRC})
    Verify the file exists at the exact path. Use absolute paths or correct relative paths from CMAKE_CURRENT_SOURCE_DIR. Example:
      set(MY_SRC ${CMAKE_CURRENT_SOURCE_DIR}/src/module.cpp)
      add_executable(myapp ${MY_SRC})
  2. 85% success If the file was moved, update the CMakeLists.txt to point to the new location. Run 'cmake --build . --clean-first' after fixing.
    If the file was moved, update the CMakeLists.txt to point to the new location. Run 'cmake --build . --clean-first' after fixing.
  3. 80% success If using generated files, ensure they are generated before the target is defined. Use add_custom_command with OUTPUT and add the output to the target's sources.
    If using generated files, ensure they are generated before the target is defined. Use add_custom_command with OUTPUT and add the output to the target's sources.

中文步骤

  1. 验证文件是否存在于确切路径。使用绝对路径或从 CMAKE_CURRENT_SOURCE_DIR 开始的正确相对路径。示例:
      set(MY_SRC ${CMAKE_CURRENT_SOURCE_DIR}/src/module.cpp)
      add_executable(myapp ${MY_SRC})
  2. 如果文件已被移动,更新 CMakeLists.txt 指向新位置。修复后运行 'cmake --build . --clean-first'。
  3. 如果使用生成的文件,确保在定义目标之前生成它们。使用带 OUTPUT 的 add_custom_command,并将输出添加到目标的源文件中。

Dead Ends

Common approaches that don't work:

  1. 60% fail

    The stale CMake cache may still reference the old path, causing the same error. A full reconfigure is needed.

  2. 90% fail

    The error is about a missing source file, not about target existence. Adding to another target doesn't create the missing file.

  3. 75% fail

    If the file doesn't exist on disk, no extension change will help. The file must be present and the path must match exactly.