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...]
ccache <compiler> [ARG...]
ccache -s [OPTION...]
ccache -C [OPTION...]

PARAMETERS

-s
    Show current cache statistics.

--show-stats
    Show current cache statistics.

-z
    Zero current cache statistics.

--zero-stats
    Zero current cache statistics.

-C
    Clear the entire cache.

--clear
    Clear the entire cache.

-M SIZE
    Set the maximum cache size (e.g., -M 10G or -M 500M).

--max-size=SIZE
    Set the maximum cache size.

-F
    Flush any pending writes to disk.

--flush
    Flush any pending writes to disk.

-x
    Show the current effective configuration.

--show-config
    Show the current effective configuration.

--version
    Display ccache version information.

--help
    Display a help message.

DESCRIPTION

ccache is a compiler cache. It acts as a wrapper around your C/C++/Objective-C compiler, significantly speeding up recompilation times. When you compile code, ccache intercepts the compiler call. It then calculates a hash of the source code, compiler options, preprocessor output, and other relevant environment variables. If a matching compilation has been performed before and is found in the cache, ccache simply returns the cached object file, avoiding the actual compiler invocation. If no match is found, ccache executes the real compiler, stores the resulting object file in its cache, and then passes it back to the build system. This process is transparent to the build system, making it easy to integrate. While it doesn't speed up the very first full build, it provides substantial benefits during iterative development cycles, saving developers valuable time.

CAVEATS

While ccache is highly effective, it has some limitations. It does not speed up the initial full build of a project, as there are no cached results yet. It can consume significant disk space, so it's important to monitor and manage the cache size using the -M option. In rare cases, if environmental variables or compiler options are changed in a subtle way not tracked by ccache's hashing algorithm, it might serve a stale cache entry, though this is uncommon with default settings. Proper integration often involves prepending ccache's directory to your PATH, which needs to be correctly set up.

PATH INTEGRATION

The most common way to use ccache is to create symbolic links (e.g., gcc, g++, cc) to the ccache executable in a directory, and then prepend this directory to your system's PATH environment variable. When a build system invokes gcc, it will find the ccache symlink first, which then executes ccache, which in turn calls the actual compiler. This makes ccache completely transparent to the build process.

CACHE DIRECTORY AND CONFIGURATION

By default, ccache stores its cache files in ~/.ccache. This location can be customized using the CCACHE_DIR environment variable. ccache also offers extensive configuration options via environment variables (e.g., CCACHE_MAXSIZE, CCACHE_COMPILERCHECK) and a configuration file (usually ~/.config/ccache/ccache.conf). These options allow fine-tuning of caching behavior, such as hash extensions, compiler check methods, and debug logging.

HISTORY

ccache was initially developed by Andrew Tridgell, known for his work on rsync and Samba. It was first released around 2004 and quickly gained popularity in the open-source community for its ability to significantly reduce build times in large projects. It has since been widely adopted by projects such as the Linux kernel, WebKit, and LLVM, becoming a staple tool for developers. The project continues to be actively maintained and improved upon, ensuring its compatibility with modern compilers and build systems.

SEE ALSO

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

Copied to clipboard