Languages like C++ are still alive, in full development and continue to have their niche in the market with famous multiplatform development environments such as QT or wxWidgets. In these languages the developer performs a real compilation and the result is an executable binary.
Who has not suffered long compilation cycles in large projects, especially when it is necessary to carry out complete compilations of the code or when the software does not have a good organization of headers and dependencies, one of those headers is modified, and create a full build unexpectedly?
In this article I present you CCACHE (4.3 at the time of writing this article), a necessary tool that will allow us to reduce compilation times between 2 and 3 times (depending on the hit rate of the cache). CCACHE acts as an intermediary in the compilation, scanning the source code to detect if there have been real changes using techniques such as comparison of modification dates and hash codes. Binaries are cached compressed using a highly optimized compression algorithm to save disk space.
Information extracted from the official web page, but we will not perform "make install" as we will install it manually:
tar -xzvf ccache-4.3.tar.gz cd ccache-4.3 mkdir build cd build cmake -DCMAKE_BUILD_TYPE=Release .. make
After the compilation we will have the binary "ccache" in our build folder.
In order for our system to recognize "ccache" we will add the build folder to our system path. This line can be added to ".bashrc" to make it persistent.
Ccache is compatible with the GCC and Clang compilers, it can be used on Windows, Linux and MAC, and although it does not support all compilation flags, it is usable in most cases.
After making ccache available in our path we will use it as follows:
# gcc ccache gcc foo.c -o foo.o # g++ ccache g++ foo.cpp -o foo.o # clang ccache clang foo.c -o foo.o # clang++ ccache clang++ foo.cpp -o foo.o
We can also use ccache through CMake by modifying the compiler to use, in the same way as we have done previously. The best way to pass the compiler to CMake is through the environment variables "CC" for the C compiler and "CXX" for the C ++ compiler respectively, before calling cmake:
export CC=ccache gcc export CXX=ccache g++ cmake /path/to/your/project
These commands will help us to configure and to know the status of ccache at any time:
Shows the current statistics of the cache (hits / misses), size, directories where the cache resides and configuration.
$ccache -s cache directory /home/myuser/.cache/ccache primary config /home/myuser/.config/ccache/ccache.conf secondary config (readonly) /usr/local/etc/ccache.conf stats updated Wed Jun 23 13:29:41 2021 cache hit (direct) 26 cache hit (preprocessed) 496 cache miss 494 cache hit rate 51.38 % called for link 65 no input file 1 cleanups performed 16 files in cache 260 cache size 206.0 MB max cache size 5.0 GB
Information about compression ratio.
$ccache -x Scanning... 100.0% [===========================================================] Total data: 205.5 MB (206.0 MB disk blocks) Compressed data: 205.5 MB (12.6% of original size) - Original data: 1.6 GB - Compression ratio: 7.919 x (87.4% space savings) Incompressible data: 0.0 kB
Option -C (Capital):
Although it is not necessary to clear the cache, since CCACHE recycle the files when they reach the configuration values "max cache size" and will never occupy more than this size, with this option we have the possibility to empty the cache.
Override configuration settings
Ccache has some default values, but we can override them through its configuration file "ccache.cof" which must be located in the place indicated by the "ccache -s" command. In our case "/home/myuser/.config/ccache/ccache.conf" and if the file does not exist it will have to be created in that location.
Ccache.conf is a plain text file with the format key=value.
In case we have plenty of hard disc and the cache miss rate is high, we can increase the size of the cache by adding the nextlines to the configuration file:
# Set maximum cache size to 10 GB: max_size = 10G
To test that the changes have been applied, just run "ccache -s" again and see how the value has increased from 5 to 10 GB
You can find the full command and option list in CCACHE official docs.