Fix CMake "Cannot Find Source File" and "No SOURCES Given to Target"
A rename is the smallest change a project can make, and it produces two configure errors that look unrelated: one says a source file cannot be found, the other says the target was given no sources at all — even though the target lists two. This guide builds a three-file program whose main source has been renamed from main.cpp to app_main.cpp while CMakeLists.txt still asks for the old name, reads both errors as one failure, and traces the path CMake actually looked for. It then builds the second, separate route to the No SOURCES error: a source list collected by file(GLOB) with a pattern that matches nothing, so the list expands to empty and the target really is given no sources.
Written November 19, 2026 · Tested with CMake 3.28.3 · 9 min read
- The file was renamed, and configure fails twice
- How CMake turns src/main.cpp into a path
- Fix one: list the file that exists
- The second route: a list that expands to nothing
What this guide is
Two configure errors share one message in this family: a name in CMakeLists.txt and a file on disk have stopped agreeing. The examples here use CMake 3.28.3 and GCC 13.3.0. One small program threads through the first half: a header named greet.h and a source file named greet.cpp under src, plus a main source that was renamed from main.cpp to app_main.cpp. The rename is the kind an editor refactor or a git mv does in one second. CMakeLists.txt was not updated with it, and that one stale name is the whole problem — although the output makes it look like two.
The file was renamed, and configure fails twice
The header declares one function:
// src/greet.h
#pragma once
void greet();
The source file defines it:
// src/greet.cpp
#include "greet.h"
#include <iostream>
void greet() { std::cout << "hello from greet\n"; }
The renamed main source calls it:
// src/app_main.cpp
#include "greet.h"
int main() {
greet();
return 0;
}
The build file still lists the name the main source had before the rename:
cmake_minimum_required(VERSION 3.28)
project(rename_demo CXX)
add_executable(app src/main.cpp src/greet.cpp)
The folder on disk and the list in the build file now differ by exactly one name:
$ ls src
app_main.cpp
greet.cpp
greet.h
Configure the project and CMake reports two errors for the one stale name:
$ cmake -S . -B build
CMake Error at CMakeLists.txt:3 (add_executable):
Cannot find source file:
src/main.cpp
CMake Error at CMakeLists.txt:3 (add_executable):
No SOURCES given to target: app
CMake Generate step failed. Build files cannot be regenerated correctly.
Read the pair in order. The first error names the file CMake could not find, spelled exactly as it was listed. The second error claims the target was given no sources, which reads as a contradiction: the add_executable call plainly lists two. Both errors cite the same line, and the sections below show that the second one is not a separate defect. It is what the generate step reports for a target whose listed sources could not be assembled — and it disappears with the first error when the name is fixed, with no other change.
How CMake turns src/main.cpp into a path
add_executable builds a target from the source files listed in the call, and a relative source path in that list is not searched for. It is resolved: relative source file paths are interpreted as being relative to the current source directory, the directory whose CMakeLists.txt is being processed, which CMake stores in the variable CMAKE_CURRENT_SOURCE_DIR. So src/main.cpp in the top-level CMakeLists.txt means one exact file — <project>/src/main.cpp — and no other folder is consulted. A file sitting in the project under a different name, or the right name in a different folder, is equally absent as far as that path is concerned.
The resolution can be printed instead of guessed. Adding one message line above the add_executable call shows the full path CMake is about to look for:
message(STATUS "looking for: ${CMAKE_CURRENT_SOURCE_DIR}/src/main.cpp")
-- looking for: /path/to/rename_demo/src/main.cpp
Set that path next to the ls src output above and the failure is fully stated: CMake looked for src/main.cpp under the project root, the disk holds src/app_main.cpp, and the two strings differ. Nothing about the compiler, the generator, or the build directory is involved, which is why deleting the build directory and reconfiguring from scratch reproduces the identical pair of errors — the stale name is read again from the same CMakeLists.txt.
The same resolution rule is what makes the moved-file variant of this error identical in shape. If the file keeps its name but moves — main.cpp now lives in src, while CMakeLists.txt still lists plain main.cpp — the resolved path is <project>/main.cpp, the file is not there, and the same two errors appear with the listed spelling in the first one. In both variants the first error is a string comparison the reader can do by hand: the path in the error against the paths in the folder.
Fix one: list the file that exists
The fix is to make the list name the file that is actually on disk:
cmake_minimum_required(VERSION 3.28)
project(rename_demo CXX)
add_executable(app src/app_main.cpp src/greet.cpp)
Reconfigure in a fresh build directory and both errors are gone:
$ cmake -S . -B build
$ cmake --build build
[ 33%] Building CXX object CMakeFiles/app.dir/src/app_main.cpp.o
[ 66%] Building CXX object CMakeFiles/app.dir/src/greet.cpp.o
[100%] Linking CXX executable app
[100%] Built target app
$ ./build/app
hello from greet
Nothing else changed between the failing run and this one — same sources, same compiler, one word different in the build file. That is the evidence that the No SOURCES error was a knock-on: a target with a genuinely empty source list cannot be fixed by renaming an entry, and this one was. When a configure shows both errors together, fix the file the first error names and reconfigure before spending any time on the second.
There is one alternative fix, and it is the worse default. Renaming the file back to main.cpp also makes the strings agree, and is the right move only when the rename itself was the mistake. When the rename was deliberate — a clearer name, a file moved to match a new layout — the build file is the stale side, and editing it keeps the rename and the history that came with it.
The second route: a list that expands to nothing
The No SOURCES error also occurs on its own, with no Cannot find error beside it, and then it means exactly what it says. A second small project collects its sources with file(GLOB) instead of listing them:
cmake_minimum_required(VERSION 3.28)
project(glob_demo CXX)
file(GLOB APP_SOURCES CONFIGURE_DEPENDS "src/*.cc")
message(STATUS "APP_SOURCES=[${APP_SOURCES}]")
add_executable(app ${APP_SOURCES})
The sources in src are the same kind as before — main.cpp, greet.cpp, greet.h — all with the .cpp extension. The glob pattern asks for src/*.cc, a different extension that no file in the folder has. Configure prints the collected list before the error, and the list is empty:
$ cmake -S . -B build
-- APP_SOURCES=[]
CMake Error at CMakeLists.txt:5 (add_executable):
No SOURCES given to target: app
CMake Generate step failed. Build files cannot be regenerated correctly.
The mechanism is plain variable expansion. A variable reference is replaced by the value of the variable, or by the empty string if the variable is not set — and a glob that matched nothing sets its variable to an empty list, which expands the same way. After expansion, the call CMake executes is add_executable(app) with no sources at all. Since CMake 3.11 the sources may be omitted from add_executable when they are added later with target_sources, so an empty call is not rejected on sight; it is the generate step, reached with no sources from either place, that reports the target as given none.
This is why the printed list matters more than the error. The error names the target and stops; the message line shows the expansion that produced it. Any source list that travels through a variable can land here — a glob pattern with the wrong extension or the wrong folder, a variable name typed differently at the set and at the use (so the use expands an unset variable to the empty string), or a list built inside a condition that did not run. Each produces the identical single error, and each is found the same way: print the list at the point of the add_executable call and read what is actually in it.
Fix two: make the pattern match the files
Correct the pattern to the extension the files have:
file(GLOB APP_SOURCES CONFIGURE_DEPENDS "src/*.cpp")
The printed list now names both source files, configure completes, and the program builds and runs:
$ cmake -S . -B build
-- APP_SOURCES=[/path/to/glob_demo/src/greet.cpp;/path/to/glob_demo/src/main.cpp]
$ cmake --build build
[ 33%] Building CXX object CMakeFiles/app.dir/src/greet.cpp.o
[ 66%] Building CXX object CMakeFiles/app.dir/src/main.cpp.o
[100%] Linking CXX executable app
[100%] Built target app
$ ./build/app
hello from greet
Listing the sources explicitly instead of globbing them is the other fix, and it removes this failure mode rather than repairing it: a listed name that stops matching a file produces the Cannot find error, which names the file, instead of an empty expansion, which names nothing. Globbing stays reasonable for folders whose contents change often, but it trades a loud error for a quiet one, and the message print of the collected list is the compensation — kept in the file, it turns the next empty expansion into a one-line read.
Which error for which cause
| What configure prints | Cause | Fix |
|---|---|---|
| Cannot find source file, followed by No SOURCES given to target for the same target | A listed name resolves to a path that does not exist — the file was renamed or moved and the list was not updated | Fix the listed name (or path) to the file that exists; the No SOURCES error clears with it |
| Cannot find source file naming a header | Same cause, for a listed header — headers in the source list are checked for existence the same way | Fix the listed name or path for the header |
| No SOURCES given to target alone, with a source list built in a variable | The variable expanded to empty at the add_executable call — a glob that matched nothing, a mistyped variable name, or a list built under a condition that did not run |
Print the list at the call, find why it is empty, and fix the pattern, the name, or the condition |
| No SOURCES given to target alone, with sources meant to arrive later | The target was created with no sources and no target_sources call ever added any |
Add the sources, in the add_executable call or in a target_sources call before generation |
Checklist
- Before changing anything, compare the path in the Cannot find error, character for character, with the files in the folder. The error prints the listed spelling, not the file that exists.
- Before deleting the build directory, remember that this failure survives a clean reconfigure: the stale name is in CMakeLists.txt, not in the cache.
- Before treating the two errors as two bugs, fix the file the first error names and reconfigure. The No SOURCES error that rides with a Cannot find error clears with it.
- Before debugging a lone No SOURCES error, print the source list at the
add_executablecall with amessageline. An empty print is the diagnosis. - Before trusting a glob, check the pattern against the folder: the extension and the directory in the pattern must both match the files as they are, not as they were planned.
- Before renaming or moving a source file, search the project's CMakeLists.txt files for the old name. The rename is finished only when the lists agree with the disk.
Related guides on this site
- Fix "Cannot Find -l..." — How ld Searches for Libraries, and Where the Path Is Set — the link-time sibling of this error: a file that exists, and a search that never visits its folder.
- C++20 Modules with CMake and Ninja — a full CMake project built end to end, for projects that grow past a single
add_executablelist. - CMake Presets, vcpkg, and Modules: Decisions — the configuration-layer choices around a working CMake build, once the build itself configures.
- C++20 Header-Only Modules with CMake Install — install rules for a CMake target, the step after a target that configures and builds cleanly.
Sources
- CMake, add_executable — a target is built from the source files listed in the command invocation, and the sources may be omitted only when they are added later using
target_sources(). - CMake, add_library — the same listed-sources rule for library targets, with the same omission provision for
target_sources(). - CMake, target_sources — relative source file paths are interpreted as being relative to the current source directory, that is,
CMAKE_CURRENT_SOURCE_DIR. - CMake, CMAKE_CURRENT_SOURCE_DIR — the variable holds the full path to the source directory currently being processed.
- CMake, cmake-language — a variable reference is replaced by the value of the variable, or by the empty string if the variable is not set, which is how an empty list leaves an
add_executablecall with no sources.
Keep Exploring
Seven tips and seven puzzles so far, each one a short runnable example with the fix explained.
Paste an AddressSanitizer, UBSan, or ThreadSanitizer report and get the bug, the file and line, and the fix \u2014 in plain language.
C++20 and C++23 features, migration paths, and measured trade-offs.
The full C++ hub \u2014 language basics, the standard library, coroutines, and measured comparisons.
Paste a [Service] section and get a plain-language verdict on every sandboxing setting it finds.