Using CCache to speed up compilation

Ccache is a compiler cache for C and C++. It does what its function implies: caches the results of compilations. ccache will cache previous compilations, detect when the same compilation is being done again, and reuse its cache instead of recompiling the source code again.

Installation
ccache is typically available in your operating system’s package manager of choice. On Ubuntu/Debian, the installation is

sudo apt-get install ccache

On RHEL/CentOS:

sudo yum install ccache

Use Ccache
After the installation, there are basically two ways to use ccache,
1, you can just add ccache in from the compiler commands like “ccache gcc or ccache g++”,
2, or, create symbolic link as below:

[/tmp 0]$ ls -l /usr/lib/ccache/
total 0
lrwxrwxrwx 1 root root 16 Nov 4 19:09 c++ -> ../../bin/ccache
lrwxrwxrwx 1 root root 16 Nov 4 19:09 c89-gcc -> ../../bin/ccache
lrwxrwxrwx 1 root root 16 Nov 4 19:09 c99-gcc -> ../../bin/ccache
lrwxrwxrwx 1 root root 16 Nov 4 19:09 cc -> ../../bin/ccache
lrwxrwxrwx 1 root root 16 Nov 4 19:09 g++ -> ../../bin/ccache
lrwxrwxrwx 1 root root 16 Nov 4 19:09 g++-4.7 -> ../../bin/ccache
lrwxrwxrwx 1 root root 16 Nov 4 19:09 gcc -> ../../bin/ccache
lrwxrwxrwx 1 root root 16 Nov 4 19:09 gcc-4.4 -> ../../bin/ccache
lrwxrwxrwx 1 root root 16 Nov 4 19:09 gcc-4.7 -> ../../bin/ccache
lrwxrwxrwx 1 root root 16 Nov 4 19:09 x86_64-linux-gnu-g++ -> ../../bin/ccache
lrwxrwxrwx 1 root root 16 Nov 4 19:09 x86_64-linux-gnu-g++-4.7 -> ../../bin/ccache
lrwxrwxrwx 1 root root 16 Nov 4 19:09 x86_64-linux-gnu-gcc -> ../../bin/ccache
lrwxrwxrwx 1 root root 16 Nov 4 19:09 x86_64-linux-gnu-gcc-4.4 -> ../../bin/ccache
lrwxrwxrwx 1 root root 16 Nov 4 19:09 x86_64-linux-gnu-gcc-4.7 -> ../../bin/ccache

With the above settings, we have to add /usr/lib/ccache to the FRONT of your path:

export PATH= /usr/lib/ccache:$PATH

Then, when you run gcc or g++, it should use ccache instead. The “which” command will show you whether or not that’s the case:

$ which gcc
/usr/lib/ccache/gcc


Ccache Example: Use Ccache for compiling

We will show an example here to use cache to compile a kernel;

[/tmp/linux-2.6.34 0]$ uname -a
Linux admon-AP 2.6.37-gentoo #1 SMP PREEMPT Wed Jan 12 20:06:14 CST 2011 x86_64 AMD Athlon(tm) II X4 630 Processor AuthenticAMD GNU/Linux
[/tmp/linux-2.6.34 0]$ grep "make" build
28:make -j4 ARCH=arm CROSS_COMPILE="ccache arm-linux-" O=$outdir $@
[/tmp/linux-2.6.34 0]$ time ./build
real 3m4.146s
user 10m30.640s
sys 0m37.138s
[/tmp/linux-2.6.34 0]$ ./build clean
[/tmp/linux-2.6.34 0]$ time ./build
real 0m23.714s
user 0m31.603s
sys 0m12.777s

In this typical usage case,the performance boosted by 9 times.

Settings of Ccache
You can check its settings with –help parameter, or read its man page. There are some variables mentioned in the man page, and make it more handy. For example, this shows you how to run ccache together with distcc:

export CCACHE_PREFIX=distcc
Share Button

One thought on “Using CCache to speed up compilation

  1. Hi Dear, are you in fact visiting this site on a regular basis, if so
    after that you will definitely obtain nice knowledge.

Leave a comment

Your email address will not be published. Required fields are marked *