Toolkit: ccache
ccache is a compiler cache. It sits in front of the compiler, hashes everything that affects a compilation (the preprocessed source or the source plus its headers, the compiler, the flags), and if it has seen that exact input before, copies the stored object file instead of compiling. Clean rebuilds, branch switches and CI runs that recompile unchanged code become much faster.
CMake integrates it with one variable per language: CMAKE_<LANG>_COMPILER_LAUNCHER. CMake then prefixes every compile command with the launcher, without changing the compiler CMake detected.
find_program(CCACHE ccache)
if(CCACHE)
set(CMAKE_C_COMPILER_LAUNCHER ${CCACHE})
set(CMAKE_CXX_COMPILER_LAUNCHER ${CCACHE})
endif()Better still, keep it out of CMakeLists.txt entirely and pass it per machine: -DCMAKE_CXX_COMPILER_LAUNCHER=ccache, a user preset, or the environment variable CMAKE_CXX_COMPILER_LAUNCHER=ccache (3.17+), which applies to every project you configure.
A clean rebuild with and without the cache
This session builds a small project, deletes the build directory, and builds again. The second build compiles nothing: every object comes from the cache.
mkdir -p cached/src && cd cached
cat > CMakeLists.txt <<'CM'
cmake_minimum_required(VERSION 3.25)
project(Cached LANGUAGES CXX)
add_executable(app src/main.cpp src/a.cpp src/b.cpp src/c.cpp)
CM
for f in a b c; do echo "#include <map>
#include <string>
int $f() { std::map<std::string, int> m{{\"$f\", 1}}; return m.size(); }" > src/$f.cpp; done
echo 'int a(); int b(); int c(); int main() { return a() + b() + c() - 3; }' > src/main.cpp
ccache --zero-stats > /dev/null
cmake -S . -B build -G Ninja --log-level=NOTICE -DCMAKE_CXX_COMPILER_LAUNCHER=ccache > /dev/null
cmake --build build -v | grep -o 'ccache /usr/bin/c++ .*main.cpp.o -c' | sed 's/ -MD.*-o / … -o /'
echo "--- first build"
ccache --print-stats | grep -E '^(direct_cache_hit|preprocessed_cache_hit|cache_miss)\s'
rm -rf build
cmake -S . -B build -G Ninja --log-level=NOTICE -DCMAKE_CXX_COMPILER_LAUNCHER=ccache > /dev/null
cmake --build build > /dev/null && ./build/app && echo "app runs"
echo "--- after a clean rebuild"
ccache --print-stats | grep -E '^(direct_cache_hit|preprocessed_cache_hit|cache_miss)\s'ccache /usr/bin/c++ -fno-diagnostics-color … -o CMakeFiles/app.dir/src/main.cpp.o -c
--- first build
cache_miss 4
direct_cache_hit 0
preprocessed_cache_hit 0
app runs
--- after a clean rebuild
cache_miss 4
direct_cache_hit 4
preprocessed_cache_hit 0
- The verbose compile line starts with
ccache /usr/bin/c++: the launcher wraps the real compiler. - The first build had 4 misses (nothing cached yet). The counters add up, so after
rm -rf buildthe misses stay at 4 and the rebuild added 4 direct hits: ccache matched the sources and headers by hash without even running the preprocessor, and no compiler ran at all. --print-statsgives machine-readable counters;ccache -sis the human summary,ccache -z(or--zero-stats) resets the counters.
Making cache hits likely
ccache only hits when the inputs are identical, and absolute paths are part of the input. Two machines (or two checkouts on one machine) building from different directories miss each other's entries, unless you tell ccache where the paths should be relative to:
| Setting | Why |
|---|---|
CCACHE_BASEDIR=$PWD (or base_dir in ccache.conf) |
rewrites absolute paths under it to relative ones, so different checkout locations share entries |
CCACHE_DIR |
where the cache lives; in CI, point it at a directory you save and restore between runs |
CCACHE_MAXSIZE |
cache size limit (default 5 GB) |
CCACHE_SLOPPINESS |
trade exactness for hits (e.g. time_macros if you use __DATE__) |
CCACHE_COMPILERCHECK=content |
hash the compiler binary, not its mtime (useful when compilers are reinstalled in CI) |
In GitHub Actions, the cache directory is typically saved with actions/cache (or the hendrikmuhs/ccache-action), keyed on the OS, compiler and branch.
What ccache doesn't do
- It doesn't speed up linking (each link is new) or the first build.
- Precompiled headers, C++20 modules and some flags (
-ftime-trace, coverage in some versions) reduce or prevent caching; checkccache -sfor "uncacheable" counts. - On Windows with MSVC, ccache works with the Ninja generator (
CMAKE_CXX_COMPILER_LAUNCHER), not the Visual Studio generator. sccache (Mozilla) is an alternative with cloud storage backends (S3, GCS, Redis); both are compared in Build Performance.
Test yourself
Which variable makes CMake run every C++ compile through ccache?
CMAKE_CXX_COMPILER=ccacheCMAKE_CXX_COMPILER_LAUNCHER=ccacheCMAKE_CXX_FLAGS=--ccacheCCACHE_ENABLE=ON
After rm -rf build and a rebuild, why were all compiles cache hits?
- Ninja remembered them
- The sources, headers, flags and compiler were identical, so ccache returned the stored objects
- CMake skipped compiling
- The objects were in /tmp
Two developers build the same commit from different checkout paths and get no shared cache hits. What helps?
- CCACHE_MAXSIZE
- CCACHE_BASEDIR set to the checkout root
- Disabling direct mode
- Using Makefiles