如何修复 CMake Error: Cannot find source file: *.cpp

问题:

你正在尝试使用类似这样的 CMakeLists.txt 构建基于 CMake 的项目

CMakeLists.example.txt
cmake_minimum_required(VERSION 3.10)
project(my_project)

add_executable(my_project *.cpp)

解决方案

你不能在 add_executable() 中直接使用 glob 模式 *.cpp。你需要使用 file(GLOB ...) 来设置变量,然后可以在 add_executable() 中使用:

cmake_fix_example.txt
cmake_minimum_required(VERSION 3.10)
project(my_project)

file(GLOB SRC_FILES *.cpp)
add_executable(my_project ${SRC_FILES})

Check out similar posts by category: CMake