How to Use CCache with CMake? How to Use CCache with CMake? unix unix

How to Use CCache with CMake?


As of CMAKE 3.4 you can do:

-DCMAKE_CXX_COMPILER_LAUNCHER=ccache


It is now possible to specify ccache as a launcher for compile commands and link commands (since cmake 2.8.0). That works for Makefile and Ninja generator. To do this, just set the following properties :

find_program(CCACHE_FOUND ccache)if(CCACHE_FOUND)    set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE ccache)    set_property(GLOBAL PROPERTY RULE_LAUNCH_LINK ccache) # Less useful to do it for linking, see edit2endif(CCACHE_FOUND)

It is also possible to set these properties only for specific directories or targets.

For Ninja, this is possible since version 3.4.For XCode, Craig Scott gives a workaround in his answer.

Edit : Thanks to uprego and Lekensteyn's comment, I edited the answer to check if ccache is available before using it as launcher and for which generators is it possible to use a compile launcher.

Edit2: @Emilio Cobos recommended to avoid doing that for the linking part as ccache doesn't improve linking speed and can mess with other types of cache like sccache


I personally have /usr/lib/ccache in my $PATH. This directory contains loads of symlinks for every possible name the compiler could be called from (like gcc and gcc-4.3), all pointing to ccache.

And I didn't even create the symlinks. That directory comes pre-filled when I install ccache on Debian.