How to Use CCache with CMake?
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.
CMake + ccache: RULE_LAUNCH_COMPILE or CMAKE_LANG_COMPILER_LAUNCHER
The first one will use the launcher for all languages in the build. The second one will use the same launcher for C
and CXX
but not for other languages. The two methods are slightly different as the second one would allow for a different launcher for different languages. The second method allows for finer control and may be preferred. There are also environment variables and directory and target properties for control which items use a launcher.
https://crascit.com/2016/04/09/using-ccache-with-cmake/
How ccache improves building speed?
Consider the case where you switch to some older branch of your project - that you did compile in the past and that ccache has cached, but CMake sees as "almost all files have changed and must be recompiled" - that's where you see a massive gain.
Another situation is where you have deleted your build directory (for some good reason) and now have to rebuild everything. ccache
is also a huge help there.
Also; ccache
is trivial to set up and is thenceforth completely invisible / transparent, so there really is no reason to not use it. When it helps it usually helps a lot, when it does not help it doesn't hurt.
CMake wants to use ccache instead of gcc
You are not supposed to change CMAKE_C_COMPILER
and CMAKE_CXX_COMPILER
from within the CMakeLists. Instead you should give them at the command line when running CMake for the first time:
cmake -D CMAKE_CXX_COMPILER=/path/to/g++ ..
Note that this value has no effect after the first run of CMake! So if you want to switch to a different compiler you will either have to delete the CMake cache or switch to a new build directory.
On the other hand, not having to specify this lengthy option for subsequent CMake runs of course also saves a lot of typing.
Using compiler prefix command(s) with CMake (distcc, ccache)
Since CMake 3.4.0 there has been a CMAKE_<LANG>_COMPILER_LAUNCHER variable and corresponding target property <LANG>_COMPILER_LAUNCHER. So if your project is C-only you would do something like:
cmake -DCMAKE_C_COMPILER_LAUNCHER=ccache /path/to/source
CCACHE_PREFIX=distcc make -j`distcc -j`
If you have a C++ project, use -DCMAKE_CXX_COMPILER_LAUNCHER=ccache
.
Or, make your CMakeLists.txt
smart and use ccache automatically if it can be found:
#-----------------------------------------------------------------------------
# Enable ccache if not already enabled by symlink masquerading and if no other
# CMake compiler launchers are already defined
#-----------------------------------------------------------------------------
find_program(CCACHE_EXECUTABLE ccache)
mark_as_advanced(CCACHE_EXECUTABLE)
if(CCACHE_EXECUTABLE)
foreach(LANG C CXX)
if(NOT DEFINED CMAKE_${LANG}_COMPILER_LAUNCHER AND NOT CMAKE_${LANG}_COMPILER MATCHES ".*/ccache")
message(STATUS "Enabling ccache for ${LANG}")
set(CMAKE_${LANG}_COMPILER_LAUNCHER ${CCACHE_EXECUTABLE} CACHE STRING "")
endif()
endforeach()
endif()
Related Topics
Why Do We Require Requires Requires
Writing Function Definition in Header Files in C++
C++11: Number of Variadic Template Function Parameters
Why Doesn't Emplace_Back() Use Uniform Initialization
Cannot Get Opencv to Compile Because of Undefined References
Differencebetween a MACro and a Const in C++
What Std::Locale Names Are Available on Common Windows Compilers
Reading and Writing a Std::Vector into a File Correctly
Is There Any Way a C/C++ Program Can Crash Before Main()
How to Throttle the Bandwidth of a Socket Connection in C
Using Cmake to Generate Visual Studio C++ Project Files
How to Do Static Linking of Libwinpthread-1.Dll in Mingw
Need Iterator When Using Ranged-Based for Loops
Serializing and Deserializing JSON with Boost
Popen Simultaneous Read and Write
How to Count Clock Cycles with Rdtsc in Gcc X86
Converting Between Local Times and Gmt/Utc in C/C++
Is There Any Standard Way of Embedding Resources into Linux Executable Image