ccache: Cache C/C++ Compilations
ccache caches the result of a compilation so an identical compile later is served from cache instead of rerun.
ccache is the standard compiler cache for GCC and Clang. The whole value in CI comes from persisting its cache directory between runs so unchanged translation units never recompile.
What it does
ccache wraps the compiler: it hashes the preprocessed source, compiler, and flags, and on a cache hit copies the stored object file instead of invoking the compiler. You hook it in by prefixing the compiler (ccache gcc), via masquerade symlinks on PATH, or through build-system launcher variables.
Common usage
# prefix the compiler directly
export CC="ccache gcc" CXX="ccache g++"
# CMake compiler launcher
cmake -B build -DCMAKE_C_COMPILER_LAUNCHER=ccache \
-DCMAKE_CXX_COMPILER_LAUNCHER=ccache
# masquerade: put ccache symlinks early on PATH
export PATH="/usr/lib/ccache:$PATH"Options
| Mechanism | What it does |
|---|---|
| ccache gcc / ccache g++ | Prefix the compiler invocation |
| CMAKE_<LANG>_COMPILER_LAUNCHER | CMake hook to run ccache before the compiler |
| /usr/lib/ccache on PATH | Masquerade symlinks intercept gcc/g++/cc/c++ |
| CCACHE_DIR | Where the cache lives (default ~/.cache/ccache) |
| ccache <compiler> --version | Confirm ccache is intercepting the compiler |
In CI
The cache directory (CCACHE_DIR) is the thing to persist across runs; set it to a stable path and save/restore it with your CI cache. With Ninja or Meson the launcher approach is the cleanest because it survives reconfigures. Verify hits with ccache -s after the build.
Common errors in CI
Zero cache hits despite a restored cache usually means a changing compiler path, an absolute __FILE__ path, or a changing build directory in the hash; set CCACHE_BASEDIR and a stable compiler. "ccache: error: Failed to create temporary file" means CCACHE_DIR or its tmp is not writable; point it at a writable path. If ccache seems ignored, the launcher/PATH wiring did not take effect.