How to fix CMake Error: Cannot find source file: *.cpp
Problem:
You are trying to build your CMake-based project using a CMakeLists.txt
like
cmake_minimum_required(VERSION 3.10)
project(my_project)
add_executable(my_project *.cpp)
Solution
You can’t use the glob pattern *.cpp
directly in add_executable()
. You need to use file(GLOB ...)
in order to set a variable, which you can then use in add_executable()
:
cmake_minimum_required(VERSION 3.10)
project(my_project)
file(GLOB SRC_FILES *.cpp)
add_executable(my_project ${SRC_FILES})