Ccache - A Rapid and Efficient Compiler Cache For GCC G++
Ccache - A Rapid and Efficient Compiler Cache For GCC G++
The process of building large projects that have C/C++ sources is time-
consuming. Packages such as the Linux kernel, OpenSSH and FFmpeg take a lot
of time to build, making it an expensive process. We do have the ‘make’ utility
to optimise the build process, but there are times when it is ineffective (like
when we ‘make clean’ and build from scratch). So ‘make’ does not take care of
such cases. Also, when we extract sources from a tarball, a rebuild happens
every time we do so.
A rebuild of sources is inefficient if we have already built the same sources
earlier. This duplication of work causes a waste of CPU resources and
development time.
The solution
The solution is ccache, a persistent (on-disk) C/C++ compiler cache that
achieves fast compilation. It is an open source tool to optimise the build process
of source code. It supports C/C++ and the Objective-C/C++ languages.
The features of ccache are:
Installation
Use the following code to install ccache:
Using ccache
There are two ways to use ccache:
1. You can either prefix your compilation commands with ccache, or…
2. Mask the compiler by creating a symbolic link (named as the compiler) to
ccache.
Let’s use the second method on Ubuntu. We need to prefix /usr/lib/ccache to
the PATH environment variable. Please run the following on bash shell.
export PATH=/usr/lib/ccache:$PATH
After installing and updating PATH variable, let’s check the correctness of the
compilers, as follows:
$ which gcc
/usr/lib/ccache/gcc
$ which g++
/usr/lib/ccache/g++
$ ccache -s
cache directory /home/vkanaujia/.ccache
primary config /home/vkanaujia/.ccache/ccache.conf
secondary config (readonly) /etc/ccache.conf
cache hit (direct) 0
cache hit (preprocessed) 0
cache miss 0
files in cache 0
cache size 0.0 kB
max cache size 10.0 GB
$ wget http://ffmpeg.org/releases/ffmpeg-3.0.2.tar.bz2
$ bunzip2 ffmpeg-3.0.2.tar.bz2
$ tar xvf ffmpeg-3.0.2.tar
$ cd ffmpeg-3.0.2/
$ time make
real 12m18.544s
user 10m34.876s
sys 1m12.988s
$ ccache -s
cache directory /home/vkanaujia/.ccache
primary config /home/vkanaujia/.ccache/ccache.conf
secondary config (readonly) /etc/ccache.conf
cache hit (direct) 0
cache hit (preprocessed) 0
cache miss 1507
called for link 4
files in cache 4538
cache size 239.6 MB
max cache size 10.0 GB
$ make clean
$ time make
real 1m20.949s
user 1m4.824s
sys 0m5.924s
Here we see that with ccache, compilation is 800 per cent faster!