CCache is a fantastic tool for speeding up compile and recompilation times.
It is best supported on Linux and MacOS using either GCC or Clang, but has good support for several other platforms and compilers as well. For instance, I used it to compile wxWidgets on Windows using MinGW-w64 GCC without issue.
The best way to use CCache in a CMake build is to use it as a
compiler launcher
by setting the CMAKE_C_COMPILER_LAUNCHER
and CMAKE_CXX_COMPILER_LAUNCHER
environment variables
to use ccache
.
First, set the environment variables for the current shell session.
Windows (PowerShell):
# PowerShell
$env:CMAKE_C_COMPILER_LAUNCHER='ccache'
$env:CMAKE_CXX_COMPILER_LAUNCHER='ccache'
Linux/MacOS (bash):
CMAKE_C_COMPILER_LAUNCHER='ccache'
CMAKE_CXX_COMPILER_LAUNCHER='ccache'
Then run the initial CMake configuration. Upon first configuration, CMake will read the values
of CMAKE_C_COMPILER_LAUNCHER
and CMAKE_CPP_COMPILER_LAUNCHER
and use those to launch the respective
C and C++ compilers.
# Example
cmake -B build/ -DCMAKE_BUILD_TYPE=Release
Then build the project as usual.
If running a subsequent CMake configuration in a project where ccache wasn't used as the compiler launcher, you'll need to set the CMake cache variables directly instead:
cmake -B build/ -DCMAKE_BUILD_TYPE=Release -DCMAKE_C_COMPILER_LAUNCHER=ccache -DCMAKE_CXX_COMPILER_LAUNCHER=ccache
Then build the project as usual.