cmake build_error ai_generated true

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 .F .for .f77 .f90 .f95 .f03 .f08。

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

其他格式: JSON · Markdown 中文 · English
95%修复率
90%置信度
1证据数
2023-06-12首次发现

版本兼容性

版本状态引入弃用备注
CMake 3.10 active
CMake 3.18 active
CMake 3.25 active

根因分析

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

English

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

官方文档

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

解决方案

  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()` 从模板生成缺失的源文件。

无效尝试

常见但无效的做法:

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

    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% 失败

    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% 失败

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