Fix CMake "Generator Does Not Match" and Stale-Cache Errors When Switching Ninja and Make
A build tree remembers how it was first configured. Ask it for a different generator and it refuses; move it to a new path and it refuses differently — and in both cases the refusal names the file responsible: CMakeCache.txt. This guide builds one small program, configures it once with Ninja, and reproduces both refusals verbatim. It then reads the cache entries the first configure pinned — generator, source directory, compiler — and fixes each error the clean way: one build directory per generator, or a cache removal that lets the tree start over.
Written November 19, 2026 · Tested with CMake 3.28.3 · 15 min read
- The program, and the first configure
- Error one: the generator does not match
- What the cache pins
- Error two: the cache directory is different
What this guide is
Two configure errors, one cause: a build tree is not a neutral folder that each configure rewrites from scratch — the first configure writes a cache into it, and every later configure has to agree with what that cache recorded. The examples here use CMake 3.28.3 and GCC 13.3.0, with Ninja 1.13.2 and GNU Make 4.3 as the two generators being switched between. The first error appears when the generator changes inside an existing tree. The second appears when the tree itself moves — copied to a new folder, checked out at a different path, or carried over from another machine. Both are fixed by giving CMake a tree whose cache matches the request, or no cache at all. Neither is fixed by editing the cache, and the last section shows what an edited cache actually leaves behind.
This guide is about the two errors, not about which generator is faster. Switching generators to compare them is a legitimate reason to hit the first error; the fix below — one build directory per generator — is also what makes that comparison repeatable.
The program, and the first configure
The program is one source file:
#include <iostream>
int main() { std::cout << "hello\n"; }
Its build file is the smallest one that builds it:
cmake_minimum_required(VERSION 3.28)
project(hello CXX)
add_executable(hello main.cpp)
Configure it once, with Ninja, into a build directory called build:
$ cmake -S . -B build -G Ninja
That configure succeeds, and it does more than write a build.ninja file. It creates build/CMakeCache.txt and a build/CMakeFiles directory holding the run's state — and the cache records choices that later configures are not free to contradict. The next two sections each contradict one of them.
Error one: the generator does not match
Keep the same build directory and ask for the other generator:
$ cmake -S . -B build -G "Unix Makefiles"
CMake Error: Error: generator : Unix Makefiles
Does not match the generator used previously: Ninja
Either remove the CMakeCache.txt file and CMakeFiles directory or choose a different binary directory.
Configure stops before generating anything. The error is symmetric — a tree first configured with Unix Makefiles refuses Ninja with the same text and the two generator names swapped:
$ cmake -S . -B build-make -G Ninja
CMake Error: Error: generator : Ninja
Does not match the generator used previously: Unix Makefiles
Either remove the CMakeCache.txt file and CMakeFiles directory or choose a different binary directory.
The reason is structural, not a quirk of these two generators. A generator is responsible for writing the input files for a native build system, and exactly one generator must be selected for a build tree to determine what native build system is to be used. A tree configured for Ninja holds build.ninja and Ninja's rule files; a tree configured for Make holds a Makefile. CMake will not rewrite one into the other in place, because the cache and the generated files were all produced under the first choice. So the generator is chosen once per build tree — on the first configure, with the -G option — and the cache defends that choice on every configure after it.
The error's last line is also the whole remedy list, and it has exactly two entries: remove the cache file and the CMakeFiles directory, or choose a different binary directory. The two fixes below are those two entries, taken one at a time.
What the cache pins
Open the cache the first configure wrote and read the entries that matter here:
$ grep -E "CMAKE_GENERATOR:|CMAKE_HOME_DIRECTORY:|CMAKE_CACHEFILE_DIR:" build/CMakeCache.txt
CMAKE_CACHEFILE_DIR:INTERNAL=/home/hatch/workspace/slot3-verify/hello/build
CMAKE_GENERATOR:INTERNAL=Ninja
CMAKE_HOME_DIRECTORY:INTERNAL=/home/hatch/workspace/slot3-verify/hello
Three facts about the tree are pinned there. CMAKE_GENERATOR records the generator in use — the value the mismatch error compares the new -G request against. CMAKE_HOME_DIRECTORY records the path to the top of the source tree; it is an internal cache entry used to locate the source directory when loading a CMakeCache.txt from a build tree, and it has the same value as CMAKE_SOURCE_DIR. CMAKE_CACHEFILE_DIR records the build directory itself, as an absolute path. The compiler is pinned in the same file, in its own entry: CMAKE_CXX_COMPILER:FILEPATH=/usr/bin/c++.
Two consequences follow from those entries being absolute and permanent. First, the generator cannot change while the cache stands — that is error one. Second, the cache is only valid at the exact pair of paths it was created with: move either the source tree or the build tree and the recorded paths stop matching the real ones — that is error two. The cache is also why deleting generated files alone does not reset a tree: removing build.ninja or a Makefile leaves CMakeCache.txt in place, and the next configure still reads the pinned generator from it.
Error two: the cache directory is different
Copy the whole project — sources and the configured build tree together — to a new folder, and configure the copied tree:
$ cp -a hello hello-moved
$ cd hello-moved
$ cmake -S . -B build -G Ninja
CMake Error: The current CMakeCache.txt directory /home/hatch/workspace/slot3-verify/hello-moved/build/CMakeCache.txt is different than the directory /home/hatch/workspace/slot3-verify/hello/build where CMakeCache.txt was created. This may result in binaries being created in the wrong place. If you are not sure, reedit the CMakeCache.txt
CMake Error: The source "/home/hatch/workspace/slot3-verify/hello-moved/CMakeLists.txt" does not match the source "/home/hatch/workspace/slot3-verify/hello/CMakeLists.txt" used to generate cache. Re-run cmake with a different source directory.
Note that the generator did not change this time — the request is Ninja, the cache says Ninja, and configure still fails. What changed is location. The first line compares CMAKE_CACHEFILE_DIR against where the cache file now sits, and they differ by one path component: the project folder was renamed by the copy. The second line compares the source directory the same way: the copied CMakeLists.txt is not the path recorded in CMAKE_HOME_DIRECTORY when the cache was created. Renaming the project folder, moving a whole project out of a container or archive, and opening a tree that was configured on another machine at a different path all produce this pair of lines, because all of them break the same recorded paths. Moving only the build tree, with the source folder left where it was, produces the first line alone — the recorded source path still matches, so only the cache-directory check fires.
There is no re-pointing step that repairs this. The recorded paths are spread through the cache and the generated files, not held in one place — which is why the practical fix is the same pair as before: a fresh build directory at the new location, or a cache removal in the moved tree. A copied tree that arrives with a build folder inside it is best treated as sources plus a stale cache: keep the sources, discard the cache.
Fix one: a fresh build directory per generator
The first remedy the error names is a different binary directory. Give each generator its own tree, and neither configure ever contradicts a cache:
$ cmake -S . -B build-ninja -G Ninja
$ cmake --build build-ninja
[1/2] Building CXX object CMakeFiles/hello.dir/main.cpp.o
[2/2] Linking CXX executable hello
$ ./build-ninja/hello
hello
$ cmake -S . -B build-make -G "Unix Makefiles"
$ cmake --build build-make
[ 50%] Building CXX object CMakeFiles/hello.dir/main.cpp.o
[100%] Linking CXX executable hello
[100%] Built target hello
$ ./build-make/hello
hello
Both trees configure, build, and run, side by side, from the same sources. This is the fix to reach for as the default layout, not just as an error remedy: a project that is routinely built both ways — or built one way while a second configuration is kept for comparison — should name its build directories after the choice they pin, build-ninja and build-make here, rather than reusing one generic build for whichever generator was wanted last. The -S and -B options are what make this cheap: -S names the path to the root directory of the project, -B names the directory CMake uses as the root of the build tree, and CMake creates the build directory if it does not already exist. Nothing in the source tree changes between the two.
The same fix covers the moved-tree error: at the new location, configure into a new build directory and the stale copied cache is never read. The copied build folder can then be deleted outright — it holds nothing the new tree needs.
Fix two: remove the cache and the CMakeFiles directory
The second remedy keeps the directory and removes what pins it. In the original build tree — still holding the Ninja cache from the first configure — delete exactly the two things the error names:
$ rm -rf build/CMakeCache.txt build/CMakeFiles
$ cmake -S . -B build -G "Unix Makefiles"
$ cmake --build build
[ 50%] Building CXX object CMakeFiles/hello.dir/main.cpp.o
[100%] Linking CXX executable hello
[100%] Built target hello
$ ./build/hello
hello
The tree now configures as Unix Makefiles, builds, and runs. Removing those two paths is a full reset of the tree's recorded choices: with no cache to load, the configure runs as a first configure, takes the generator from -G, re-detects the compiler, and writes a fresh cache. That is also the cost of this fix — every cache entry set on earlier configures, including any -D options the build relied on, is gone with the file, and has to be supplied again on the new configure line. A build directory whose configuration was assembled over several -D passes is a candidate for fix one instead: leave it intact and start the second generator in a new directory.
Remove both paths, not just the cache file. CMakeFiles holds state from the earlier configure runs alongside the cache, and the error names the pair together because they are removed together; a tree with a fresh cache and a stale CMakeFiles is a half-reset. The --fresh form in the next section removes exactly this pair, which is the confirmation that the pair is the unit.
The shortcut: --fresh performs the same removal
Since version 3.24, the cmake command has a --fresh option that performs a fresh configuration of the build tree: it removes any existing CMakeCache.txt file and the associated CMakeFiles/ directory, and recreates them from scratch. Applied to the same tree, it switches the generator in one line:
$ cmake --fresh -S . -B build -G Ninja
$ cmake --build build
[1/2] Building CXX object CMakeFiles/hello.dir/main.cpp.o
[2/2] Linking CXX executable hello
$ ./build/hello
hello
--fresh is fix two with the removal done by CMake instead of by hand — same reset, same consequence for -D entries recorded in the old cache, which are discarded along with it. It is the convenient form when the tree is at hand and the configure line carries everything the new configuration needs. It does not help with the moved-tree error in the way it first appears to: it can reset the copied tree in place, and that works, but a copied tree is still better served by deleting its build folder and configuring fresh at the new location, so no other stale generated file from the old path survives alongside the new cache.
Why hand-editing the cache is not a fix
The tempting third option is to open CMakeCache.txt and change the one line that disagrees — set CMAKE_GENERATOR:INTERNAL=Ninja to the other generator's name, and configure again. It deserves a direct answer, because it appears to work. With that one line edited to Unix Makefiles, a configure with -G "Unix Makefiles" no longer raises the mismatch error and completes. Then look at what the tree holds:
$ ls build
CMakeCache.txt
CMakeFiles
Makefile
build.ninja
cmake_install.cmake
hello
Both generators' build files are now in one tree: the new Makefile next to the stale build.ninja the edited cache never removed. Exactly one generator is supposed to be selected for a build tree; this tree claims both, and which one a given build command picks up depends on which file it reads first. The rest of the recorded state was produced under the old generator as well — the cache is not one line, and the generated files, the CMakeFiles state, and the remaining cache entries were all written for the first configuration. The documentation states the boundary from the other side: the value of CMAKE_GENERATOR should never be modified by project code, because the generator is an input to the configure, chosen with -G — not a setting a build tree carries and revises.
An edited cache converts a loud, self-explaining error into a quiet, mixed tree. The two real fixes cost one command each, and each leaves a tree whose cache and CMakeFiles were written by one configure under the new generator, so the recorded state agrees with the generator that now owns the tree. A stale build.ninja or Makefile from the old generator can still sit on disk after a cache reset — inert, because nothing reads it once the cache points at the new generator — while the edited tree's recorded state was written under the old generator and only relabelled. When a tree's history is unknown — an archive, a copy, a folder found on a shared machine — the same reasoning applies in stronger form: do not read its cache to decide whether to trust it. Give it a fresh build directory, or remove the cache and CMakeFiles, and let a first configure write a cache that describes the tree as it now is.
Which fix for which cause
| What configure prints or does | Cause | Fix |
|---|---|---|
| Generator does not match the generator used previously | The cache pins the first generator | Fresh directory, or remove the cache |
| The current CMakeCache.txt directory is different than the directory where it was created | Tree copied, moved, or renamed; paths no longer match | Fresh directory, or remove the cache in place |
| The source does not match the source used to generate cache | The source tree moved too; the old path is recorded | Same as above |
| Configure succeeds after the generator line was edited by hand | The check was bypassed; both generators' files remain | Remove the edited cache; reconfigure |
The build works, but options set with -D on earlier configures are gone |
A cache reset dropped the held options | Re-supply the -D entries |
Checklist
- Before reconfiguring an existing tree with a new generator, expect the mismatch error: the generator is chosen on a tree's first configure and pinned in its cache.
- Before copying or archiving a project, leave the build tree out. A copied cache fails at the new path, and the sources are the only part that travels.
- Before trusting a build tree of unknown history, check its cache's
CMAKE_GENERATOR,CMAKE_HOME_DIRECTORY, andCMAKE_CACHEFILE_DIRentries against where the tree actually is and what you are about to ask for. - Before removing a cache, note the
-Dentries the build relies on — the removal discards them, and the new configure needs them on its command line. - Before reaching for a text editor on
CMakeCache.txt, stop: the fixes are a different build directory, a cache removal, or--fresh— an edited cache leaves a mixed tree. - Before switching generators back and forth for comparison, create one build directory per generator and keep both; switching then costs a
-Bchange, not a reset.
Related guides on this site
- Fix CMake "Could NOT Find a Package" — and When FetchContent Is the Right Fallback — the sibling configure failure: a package that is installed, but outside every prefix the search covers.
- Fix CMake "Cannot Find Source File" and "No SOURCES Given to Target" — the other cache-adjacent configure failure: a listed source name and a file on disk that have stopped agreeing.
- C++20 Modules with CMake and Ninja — a full project configured and built with Ninja end to end, in a tree that keeps the one generator it was created with.
- CMake Presets, vcpkg, and Modules: Decisions — the configuration-layer choices around generators and build trees, once the errors themselves are fixed.
Sources
- CMake, cmake(1) —
-Gspecifies a build system generator,-Sand-Bspecify the source and build trees, and--fresh(new in version 3.24) performs a fresh configuration by removing any existingCMakeCache.txtfile and associatedCMakeFiles/directory and recreating them from scratch. - CMake, CMAKE_GENERATOR — the generator used to build the project, selected via the
cmake -Goption, and a value that should never be modified by project code. - CMake, CMAKE_HOME_DIRECTORY — the path to the top of the source tree, held as an internal cache entry used to locate the source directory when loading a
CMakeCache.txtfrom a build tree. - CMake, cmake-generators(7) — a generator is responsible for writing the input files for a native build system, and exactly one generator must be selected for a build tree to determine what native build system is to be used.
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.