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 .f90 .F .F90
ID: cmake/target-sources-file-not-found
88%Fix Rate
85%Confidence
1Evidence
2023-06-20First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| CMake 3.10 | active | — | — | — |
| CMake 3.16 | active | — | — | — |
| CMake 3.22 | active | — | — | — |
| CMake 3.27 | active | — | — | — |
Root Cause
The source file specified in target_sources() or add_executable() does not exist on disk at the given path.
generic中文
在 target_sources() 或 add_executable() 中指定的源文件在给定路径上不存在。
Official Documentation
https://cmake.org/cmake/help/latest/command/target_sources.htmlWorkarounds
-
90% success Verify the file exists at the exact path specified. Use ${CMAKE_CURRENT_SOURCE_DIR} for relative paths to avoid ambiguity.
Verify the file exists at the exact path specified. Use ${CMAKE_CURRENT_SOURCE_DIR} for relative paths to avoid ambiguity. -
85% success If the file was moved or renamed, update the CMakeLists.txt to match the new location or name.
If the file was moved or renamed, update the CMakeLists.txt to match the new location or name.
-
70% success Use file(GLOB ...) to automatically collect source files, but note this is discouraged for production builds due to missing dependency tracking.
Use file(GLOB ...) to automatically collect source files, but note this is discouraged for production builds due to missing dependency tracking.
中文步骤
Verify the file exists at the exact path specified. Use ${CMAKE_CURRENT_SOURCE_DIR} for relative paths to avoid ambiguity.If the file was moved or renamed, update the CMakeLists.txt to match the new location or name.
Use file(GLOB ...) to automatically collect source files, but note this is discouraged for production builds due to missing dependency tracking.
Dead Ends
Common approaches that don't work:
-
Adding the file with a different extension like .cpp vs .cc or .cxx in the CMakeLists.txt
90% fail
CMake does not automatically rename files; the file must physically exist with the exact name listed. Changing the extension only works if the actual file matches.
-
Removing the target and re-adding without the missing file, assuming it's optional
75% fail
If the file is required for compilation, removing it will cause linker errors later. This only works if the file is genuinely unused.
-
Using an absolute path that includes the build directory instead of source directory
85% fail
CMake expects source files relative to the source tree, not build tree. Using build directory paths causes confusion and does not fix the missing file.