Name

ccache - a fast C/C++ compiler cache

Synopsis

ccache [ccache options]
ccache [KEY=VALUE …​] compiler [compiler options]
compiler [compiler options]

The first form takes options described in Command line options below. The second form invokes the compiler, optionally using configuration options as KEY=VALUE arguments. In the third form, ccache is masquerading as the compiler as described in Run modes.

Description

Ccache is a compiler cache. It speeds up recompilation by caching the result of previous compilations and detecting when the same compilation is being done again.

Ccache has been carefully written to always produce exactly the same compiler output that you would get without the cache. The only way you should be able to tell that you are using ccache is the speed. Currently known exceptions to this goal are listed under Caveats. If you discover an undocumented case where ccache changes the output of your compiler, please let us know.

Run modes

There are two different ways to use ccache to cache a compilation:

  1. Prefix your compilation command with ccache. This method is most convenient if you just want to try out ccache or wish to use it for some specific projects. Example:

    ccache gcc -c example.c
  2. Let ccache masquerade as the compiler. This method is most useful when you wish to use ccache for all your compilations. To do this on a Debian system, it’s easiest to just prepend /usr/lib/ccache to your PATH. /usr/lib/ccache contains symlinks for all compilers currently installed as Debian packages.

    Alternatively, you can create any symlinks you like yourself like this:

    cp /usr/bin/ccache /usr/local/bin/
    ln -s ccache /usr/local/bin/gcc
    ln -s ccache /usr/local/bin/g++

    On platforms that don’t support symbolic links you can simply copy ccache to the compiler name instead for a similar effect:

    cp /usr/bin/ccache /usr/local/bin/gcc
    cp /usr/bin/ccache /usr/local/bin/g++

    And so forth. This will work as long as the directory with symbolic links or ccache copies comes before the directory with the compiler (typically /usr/bin) in PATH.

    The technique of letting ccache masquerade as the compiler works well, but currently doesn’t interact well with other tools that do the same thing. See Using ccache with other compiler wrappers.

Command line options

These command line options only apply when you invoke ccache as “ccache”. When ccache masquerades as a compiler (as described in the previous section), the normal compiler options apply and you should refer to the compiler’s documentation.

Common options

-c, --cleanup

Clean up the cache by removing not recently used cached files until the specified file number and cache size limits are not exceeded. This also recalculates the cache file count and size totals. Normally, there is no need to initiate cleanup manually as ccache keeps the cache below the specified limits at runtime and keeps statistics up to date on each compilation. Forcing a cleanup is mostly useful if you have modified the cache contents manually or believe that the cache size statistics may be inaccurate.

-C, --clear

Clear the entire cache, removing all cached files, but keeping the configuration file.

--config-path PATH

Let the command line options operate on configuration file PATH instead of the default. Using this option has the same effect as setting (overriding) the environment variable CCACHE_CONFIGPATH temporarily.

-d, --dir PATH

Let the command line options operate on cache directory PATH instead of the default. For example, to show statistics for a cache directory at /shared/ccache you can run ccache -d /shared/ccache -s. Using this option has the same effect as setting the environment variable CCACHE_DIR temporarily.

--evict-namespace NAMESPACE

Remove files created in the given namespace from the cache.

--evict-older-than AGE

Remove files used less recently than AGE from the cache. AGE should be an unsigned integer with a d (days) or s (seconds) suffix. If combined with --evict-namespace, only remove files within that namespace.

-h, --help

Print a summary of command line options.

-F NUM, --max-files NUM

Set the maximum number of files allowed in the cache to NUM. Use 0 for no limit. The value is stored in a configuration file in the cache directory and applies to all future compilations.

-M SIZE, --max-size SIZE

Set the maximum size of the files stored in the cache. SIZE should be a number followed by an optional suffix: kB, MB, GB, TB (decimal), KiB, MiB, GiB or TiB (binary). The default suffix is GiB. Use 0 for no limit. The value is stored in a configuration file in the cache directory and applies to all future compilations.

-X LEVEL, --recompress LEVEL

Recompress the cache to level LEVEL using the Zstandard algorithm. The level can be an integer, with the same semantics as the compression_level configuration option, or the special value uncompressed for no compression. See [Cache compression] for more information. This can potentially take a long time since all files in the cache need to be visited. Only files that are currently compressed with a different level than LEVEL will be recompressed.

--recompress-threads THREADS

Use up to THREADS threads when recompressing the cache. The default is to use one thread per CPU.

-o KEY=VALUE, --set-config KEY=VALUE

Set configuration option KEY to VALUE in the configuration file. See Configuration for more information.

-x, --show-compression

Print cache compression statistics. See Cache compression for more information. This can potentially take a long time since all files in the cache need to be visited.

-p, --show-config

Print current configuration options and from where they originate (environment variable, configuration file or compile-time default) in human-readable format.

--show-log-stats

Print statistics counters from the stats log in human-readable format. See stats_log. Use -v/--verbose once or twice for more details.

-s, --show-stats

Print a summary of configuration and statistics counters in human-readable format. Use -v/--verbose once or twice for more details.

-v, --verbose

Increase verbosity. The option can be given multiple times.

-V, --version

Print version and copyright information.

-z, --zero-stats

Zero the cache statistics (but not the configuration options).

Options for remote file-based storage

--trim-dir PATH

Remove not recently used files from directory PATH until it is at most the size specified by --trim-max-size.

Don’t use this option to trim the local cache. To trim the local cache directory to a certain size, use CCACHE_MAXSIZE=SIZE ccache -c.
--trim-max-size SIZE

Specify the maximum size for --trim-dir. SIZE should be a number followed by an optional suffix: kB, MB, GB, TB (decimal), KiB, MiB, GiB or TiB (binary). The default suffix is GiB. Use 0 for no limit.

--trim-method METHOD

Specify the method to trim a directory with --trim-dir. Possible values are:

atime

LRU (least recently used) using the file access timestamp. This is the default.

mtime

LRU (least recently used) using the file modification timestamp.

--trim-recompress LEVEL

Recompress to level LEVEL using the Zstandard algorithm when using --trim-dir. The level can be an integer, with the same semantics as the compression_level configuration option, or the special value uncompressed for no compression. See [Cache compression] for more information. This can potentially take a long time since all files in the cache need to be visited. Only files that are currently compressed with a different level than LEVEL will be recompressed.

--trim-recompress-threads THREADS

Recompress using up to THREADS threads with --trim-recompress. The default is to use one thread per CPU.

Options for scripting or debugging

--checksum-file PATH

Print the checksum (128 bit XXH3) of the file at PATH (- for standard input).

--extract-result PATH

Extract data stored in the result file at PATH (- for standard input). The data will be written to ccache-result.* files in to the current working directory. This option is only useful when debugging ccache and its behavior.

--format FORMAT

Specify format for --print-log-stats and --print-stats. Possible values are:

tab

Tab separated. This is the default.

json

JSON formatted.

-k KEY, --get-config KEY

Print the value of configuration option KEY. See Configuration for more information.

--hash-file PATH

Print the hash (160 bit BLAKE3) of the file at PATH (- for standard input). This is only useful when debugging ccache and its behavior.

--inspect PATH

Print the content of a result or manifest file at PATH (- for standard input) to standard output in human-readable format. File content embedded in a result file will however not be printed; use --extract-result to extract the file content. This option is only useful when debugging ccache and its behavior.

--print-log-stats

Print statistics counters from the stats log in machine-parsable (tab-separated or JSON) format. See stats_log and --format.

--print-stats

Print statistics counter IDs and corresponding values in machine-parsable (tab-separated or JSON) format. See --format.

--print-version

Print version and don’t do anything else.

Extra options

When run as a compiler, ccache usually just takes the same command line options as the compiler you are using. The only exception to this is the option --ccache-skip. That option can be used to tell ccache to avoid interpreting the next option in any way and to pass it along to the compiler as-is.

--ccache-skip currently only tells ccache not to interpret the next option as a special compiler option — the option will still be included in the direct mode hash.

The reason this can be important is that ccache does need to parse the command line and determine what is an input filename and what is a compiler option, as it needs the input filename to determine the name of the resulting object file (among other things). The heuristic ccache uses when parsing the command line is that any argument that exists as a file is treated as an input file name. By using --ccache-skip you can force an option to not be treated as an input file name and instead be passed along to the compiler as a command line option.

Another case where --ccache-skip can be useful is if ccache interprets an option specially but shouldn’t, since the option has another meaning for your compiler than what ccache thinks.

See also ignore_options.

Configuration

Ccache’s default behavior can be overridden by options in configuration files, which in turn can be overridden by environment variables with names starting with CCACHE_. Ccache normally reads configuration from two files: first a system-level configuration file and secondly a cache-specific configuration file. The priorities of configuration options are as follows (where 1 is highest):

  1. Command line settings in KEY=VALUE form. Example:

    ccache debug=true compiler_check="%compiler% --version" gcc -c example.c
  2. Environment variables.

  3. The cache-specific configuration file (see below).

  4. The system (read-only) configuration file <sysconfdir>/ccache.conf (typically /etc/ccache.conf or /usr/local/etc/ccache.conf).

  5. Compile-time defaults.

As a special case, if the environment variable CCACHE_CONFIGPATH is set it specifies the configuration file, and the system configuration file won’t be read.

Location of the configuration file

The location of the cache-specific configuration file is determined like this on non-Windows systems:

  1. If CCACHE_CONFIGPATH is set, use that path.

  2. Otherwise, if the environment variable CCACHE_DIR is set then use $CCACHE_DIR/ccache.conf.

  3. Otherwise, if cache_dir is set in the system configuration file then use <cache_dir>/ccache.conf.

  4. Otherwise, if there is a legacy $HOME/.ccache directory then use $HOME/.ccache/ccache.conf.

  5. Otherwise, if XDG_CONFIG_HOME is set then use $XDG_CONFIG_HOME/ccache/ccache.conf.

  6. Otherwise, use $HOME/Library/Preferences/ccache/ccache.conf (macOS) or $HOME/.config/ccache/ccache.conf (other systems).

On Windows, this is the method used to find the configuration file:

  1. If CCACHE_CONFIGPATH is set, use that path.

  2. Otherwise, if the environment variable CCACHE_DIR is set then use %CCACHE_DIR%/ccache.conf.

  3. Otherwise, if cache_dir is set in the system configuration file then use <cache_dir>\ccache.conf. The system-wide configuration on Windows is %ALLUSERSPROFILE%\ccache\ccache.conf by default. The ALLUSERSPROFILE environment variable is usually C:\ProgramData.

  4. Otherwise, if there is a legacy %USERPROFILE%\.ccache directory then use %USERPROFILE%\.ccache\ccache.conf.

  5. Otherwise, use %LOCALAPPDATA%\ccache\ccache.conf if it exists.

  6. Otherwise, use %APPDATA%\ccache\ccache.conf.

See also the cache_dir configuration option for how the cache directory location is determined.

Configuration value syntax

All configuration values support expansion of environment variables. The syntax is similar to POSIX shell syntax: $VAR or ${VAR}. Both variants will expand to the value of the environment variable VAR.

Two consecutive dollar signs ($$) will expand to a single dollar sign ($).

Configuration file syntax

Configuration files are in a simple “key = value” format, one option per line. Lines starting with a hash sign are comments. Blank lines are ignored, as is whitespace surrounding keys and values. Example:

# Set maximum cache size to 10 GB:
max_size = 10G

Boolean values

Some configuration options are boolean values (i.e. truth values). In a configuration file, such values must be set to the string true or false. For the corresponding environment variables, the semantics are a bit different:

  • A set environment variable means “true” (even if set to the empty string).

  • The following case-insensitive negative values are considered an error (instead of surprising the user): 0, false, disable and no.

  • An unset environment variable means “false”.

Each boolean environment variable also has a negated form starting with CCACHE_NO. For example, CCACHE_COMPRESS can be set to force compression and CCACHE_NOCOMPRESS can be set to force no compression.

Configuration options

Below is a list of available configuration options. The corresponding environment variable name is indicated in parentheses after each configuration option key.

absolute_paths_in_stderr (CCACHE_ABSSTDERR)

This option specifies whether ccache should rewrite relative paths in the compiler’s textual output (standard error and standard output) to absolute paths. This can be useful if you use base_dir with a build system (e.g. CMake with the "Unix Makefiles" generator) that executes the compiler in a different working directory, which makes relative paths in compiler errors or warnings incorrect. The default is false.

base_dir (CCACHE_BASEDIR)

This option should be an absolute path to a directory. If set, ccache will rewrite absolute paths into paths relative to the current working directory, but only absolute paths that begin with base_dir. Cache results can then be shared for compilations in different directories even if the project uses absolute paths in the compiler command line. See also the discussion under Compiling in different directories. If set to the empty string (which is the default), no rewriting is done.

A typical path to use as base_dir is your home directory or another directory that is a parent of your project directories. Don’t use / as the base directory since that will make ccache also rewrite paths to system header files, which typically is counterproductive.

For example, say that Alice’s current working directory is /home/alice/project1/build and that she compiles like this:

ccache gcc -I/usr/include/example -I/home/alice/project2/include -c /home/alice/project1/src/example.c

Here is what ccache will actually execute for different base_dir values:

# Current working directory: /home/alice/project1/build

# With base_dir = /:
gcc -I../../../../usr/include/example -I../../project2/include -c ../src/example.c

# With base_dir = /home or /home/alice:
gcc -I/usr/include/example -I../../project2/include -c ../src/example.c

# With base_dir = /home/alice/project1 or /home/alice/project1/src:
gcc -I/usr/include/example -I/home/alice/project2/include -c ../src/example.c

If Bob has put project1 and project2 in /home/bob/stuff and both users have set base_dir to /home or /home/$USER, then Bob will get a cache hit (if they share ccache directory) since the actual command line will be identical to Alice’s command line:

# Current working directory: /home/bob/stuff/project1/build

# With base_dir = /home or /home/bob:
gcc -I/usr/include/example -I../../project2/include -c ../src/example.c

Without base_dir there will be a cache miss since the absolute paths will differ. With base_dir set to / there will be a cache miss since the relative path to /usr/include/example will be different. With base_dir set to /home/bob/stuff/project1 there will a cache miss since the path to project2 will be a different absolute path.

Rewriting absolute paths to relative is kind of a brittle hack. It works OK in many cases, but there might be cases where things break. One known issue is that absolute paths are not reproduced in dependency files, which can mess up dependency detection in tools like Make and Ninja. If possible, use relative paths in the first place instead of using base_dir.
cache_dir (CCACHE_DIR)

This option specifies where ccache will keep its cached compiler outputs.

On non-Windows systems, the default is $HOME/.ccache if such a directory exists, otherwise $XDG_CACHE_HOME/ccache if XDG_CACHE_HOME is set, otherwise $HOME/Library/Caches/ccache (macOS) or $HOME/.cache/ccache (other systems).

On Windows, the default is %USERPROFILE%\.ccache if such a directory exists, otherwise %LOCALAPPDATA%\ccache.

Previous ccache versions defaulted to storing the cache in %APPDATA%\ccache on Windows. This can result in large network file transfers of the cache in domain environments and similar problems. Please check this directory for cache directories and either delete them or the whole directory, or move them to the %LOCALAPPDATA%\ccache directory.
compiler (CCACHE_COMPILER or (deprecated) CCACHE_CC)

This option can be used to force the name of the compiler to use. If set to the empty string (which is the default), ccache works it out from the command line.

compiler_check (CCACHE_COMPILERCHECK)

By default, ccache includes the modification time (“mtime”) and size of the compiler in the hash to ensure that results retrieved from the cache are accurate. If compiler plugins are used, these plugins will also be added to the hash. This option can be used to select another strategy. Possible values are:

content

Hash the content of the compiler binary. This makes ccache very slightly slower compared to mtime, but makes it cope better with compiler upgrades during a build bootstrapping process.

mtime

Hash the compiler’s mtime and size, which is fast. This is the default.

none

Don’t hash anything. This may be good for situations where you can safely use the cached results even though the compiler’s mtime or size has changed (e.g. if the compiler is built as part of your build system and the compiler’s source has not changed, or if the compiler only has changes that don’t affect code generation). You should only use none if you know what you are doing.

string:value

Hash value. This can for instance be a compiler revision number or another string that the build system gene