LinuxCommandLibrary

ccache

Cache compiler output to speed recompilation

TLDR

Show current cache statistics

$ ccache [[-s|--show-stats]]
copy

Clear all cache
$ ccache [[-C|--clear]]
copy

Reset statistics (but not cache itself)
$ ccache [[-z|--zero-stats]]
copy

Compile C code and cache compiled output (to use ccache on all gcc invocations, see the note above)
$ ccache gcc [path/to/file.c]
copy

SYNOPSIS

ccache [OPTION]… [COMPILER [COMPILER-OPTION]…]

PARAMETERS

-c, --cleanup
    Clean up cache by removing old entries to fit max size limit.


-C, --clear
    Clear the entire cache.


-d, --show-compression
    Print compression statistics.


-h, --help
    Display help and exit.


-M, --max-size=SIZE
    Set maximum cache size (e.g., 10G, 1T).


-m, --max-files=NUM
    Set maximum number of files in cache.


-F, --max-extra-files=NUM
    Set max extra (non-primary) files.


-o VAR=VALUE, --set-config VAR=VALUE
    Set configuration option (e.g., compression=true).


-s, --show-stats
    Show cache statistics.


-S, --show-summary
    Show summary statistics only.


-v, --verbose
    Increase verbosity.


-V, --version
    Print version and exit.


DESCRIPTION

ccache is a compiler cache designed to speed up recompilation of C and C++ code by storing and reusing the results of previous compilations.

It acts as a frontend wrapper for compilers like GCC, Clang, and others. When invoked as ccache gcc source.c, it checks if an identical compilation (based on input file hash, compiler flags, etc.) exists in the cache. If yes, it reuses the object file or executable, bypassing the actual compilation. Cache misses trigger normal compilation, with results stored for future use.

Key benefits include dramatically reduced build times during development, especially for large projects with iterative changes. It supports cache statistics, size limits, compression (zstd, zlib), and configuration via files or environment variables. Primary cache directory is ~/.ccache by default.

ccache is transparent to build systems like Make or CMake; just prefix compiler invocations. It handles dependencies intelligently but may require cache invalidation for major changes like header modifications.

CAVEATS

Cache hits require identical compiler, flags, and input; mismatches cause misses.
Does not handle linking; large caches consume disk space.
May interfere with debug info or optimizations if not configured.
Not suitable for final release builds or cross-compilation without tweaks.

CONFIGURATION

Edit ~/.ccache/ccache.conf for options like max_size=10G, compression=true, hash_dir=false.
Overrides via environment: CCACHE_MAXSIZE=5G.

USAGE EXAMPLE

export CC=ccache\ gcc
export CXX=ccache\ g++
make clean all
Use ccache -s to check stats.

HISTORY

Developed by Daniel Jacobsson in 2002 as a fast compiler cache.
Version 3.x by him until 2011; rewritten as 4.x by Joel Rosdahl starting 2019 with major improvements in speed, compression, and S3 support.
Widely used in open-source projects like Linux kernel builds.

SEE ALSO

gcc(1), clang(1), make(1), distcc(1)

Copied to clipboard