# CMake 错误：找不到源文件：/path/to/src/main.cpp。已尝试扩展名 .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`
- **领域:** cmake
- **类别:** build_error
- **验证级别:** ai_generated
- **修复率:** 88%

## 根因

在 target_sources() 或 add_executable() 中指定的源文件在给定路径上不存在。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| CMake 3.10 | active | — | — |
| CMake 3.16 | active | — | — |
| CMake 3.22 | active | — | — |
| CMake 3.27 | active | — | — |

## 解决方案

1. ```
   Verify the file exists at the exact path specified. Use ${CMAKE_CURRENT_SOURCE_DIR} for relative paths to avoid ambiguity.
   ```
2. ```
   If the file was moved or renamed, update the CMakeLists.txt to match the new location or name.
   ```
3. ```
   Use file(GLOB ...) to automatically collect source files, but note this is discouraged for production builds due to missing dependency tracking.
   ```

## 无效尝试

- **Adding the file with a different extension like .cpp vs .cc or .cxx in the CMakeLists.txt** — 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. (90% 失败率)
- **Removing the target and re-adding without the missing file, assuming it's optional** — If the file is required for compilation, removing it will cause linker errors later. This only works if the file is genuinely unused. (75% 失败率)
- **Using an absolute path that includes the build directory instead of source directory** — 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. (85% 失败率)
