Use Ccache and compile much faster

A tool to make your compiler fly!

Use Ccache and compile much faster

Ccache is a software development tool that caches builds so that next time the same build can be avoided and the results can be removed from the cache.

This can greatly speed up recompilation time. Detection is done by hash of different types of information that must be exclusive to the compilation and then using the hash sum to identify the cached output.

Ccache is licensed under the GNU General Public License.


To install Ccache you can build it from scratch and install it with the tools CMake and GNU Make by cloning the source code from the repository on GitHub:
cd ccache
cmake -B build -D CMAKE_BUILD_TYPE=Release .
cd build && make
sudo make install

But, Cache is available in most package manager repositories. For example, to install on Ubuntu:

sudo apt install ccache

Example of use

We know that compilations that use RegEx tend to take a little longer than normal. Suppose you have this code in C++:

#include <iostream>
#include <regex>

int main(){
   std::string html = "<a href=\"\">This is a link</a>";
   std::regex tags("<[^>]*>");
   std::string remove{};
   std::cout << std::regex_replace(html, tags, remove) << '\n';
   return 0;

To speed up compilation, you must pass the compilation command to ccache:

ccache g++ regex.cpp

To compile with GCC

Or, if it is with Clang:

ccache clang++ regex.cpp

You can link ccache to be used as a default, e.g.: ln -s ccache /usr/local/bin/g++.

For parameter tips and optimizations, see the help or manual:

man ccache
cache --help

Also visit the repository and the official website:

cpp clanguage


YouTube channel


Marcos Oliveira

Marcos Oliveira

Software developer

Related articles