../
2016-05-31: The /* Programming Comments */ documents have moved, and will no longer be updated or maintained at this location! Please update your bookmarks: http://www.ccoderun.ca/programming/.

Summary

CCache is used to speed up compilation. It caches compiled object files, and if asked to recompile the same source file with the same options, serves back the cached file(s). If a source file is a miss in the cache, it then calls gcc or g++ to compile the source as usual.

Installing CCache

Installing is very simple:

sudo apt-get install ccache

This should have installed a number of symbolic links for the usual C and C++ compilers:

ls -l /usr/lib/ccache/ lrwxrwxrwx 1 root root 16 Feb 6 23:41 c++ -> ../../bin/ccache* lrwxrwxrwx 1 root root 16 Feb 6 23:41 c89-gcc -> ../../bin/ccache* lrwxrwxrwx 1 root root 16 Feb 6 23:41 c99-gcc -> ../../bin/ccache* lrwxrwxrwx 1 root root 16 Feb 6 23:41 cc -> ../../bin/ccache* lrwxrwxrwx 1 root root 16 Feb 6 23:41 g++ -> ../../bin/ccache* lrwxrwxrwx 1 root root 16 Feb 6 23:41 g++-4.8 -> ../../bin/ccache* lrwxrwxrwx 1 root root 16 Feb 6 23:41 gcc -> ../../bin/ccache* lrwxrwxrwx 1 root root 16 Feb 6 23:41 gcc-4.8 -> ../../bin/ccache* lrwxrwxrwx 1 root root 16 Feb 6 23:41 i686-linux-gnu-g++ -> ../../bin/ccache* lrwxrwxrwx 1 root root 16 Feb 6 23:41 i686-linux-gnu-g++-4.8 -> ../../bin/ccache* lrwxrwxrwx 1 root root 16 Feb 6 23:41 i686-linux-gnu-gcc -> ../../bin/ccache* lrwxrwxrwx 1 root root 16 Feb 6 23:41 i686-linux-gnu-gcc-4.8 -> ../../bin/ccache*

To get CCache to work, you must ensure that /usr/lib/ccache is at the start of your PATH. With bash, this normally means modifying ~/.bashrc. With fish, you'd run set -U fish_user_paths /usr/lib/ccache $fish_user_paths.

Make sure that everything is setup correctly by running which gcc g++ to verify that the links from ccache are picked up first:

which gcc g++ /usr/lib/ccache/gcc /usr/lib/ccache/g++

Configuration

By default, ccache will grow to 1 GiB in size. This can be determined by running:

ccache --show-stats cache directory /home/stephane/.ccache cache hit (direct) 0 cache hit (preprocessed) 0 cache miss 0 files in cache 0 cache size 0 Kbytes max cache size 1.0 Gbytes

As usual, use the man page for details. Examples of several useful ccache commands:

CMake and CCache

CMake stores the absolute path of the previously found C and C++ compilers. This means if you have a CMake project already setup, and you install CCache, all your CCache stats will stay at zero since the old compilers are called directly. To get things working, you'll have to delete a few CMake-generated files and get CMake to re-detect the necessary binaries. This is easy to do:

cd build rm -rf CMakeFiles CMakeCache.txt cmake -DCMAKE_BUILD_TYPE=Release ..

You'll know that CMake found CCache because the C and C++ compilers it reports should be the ones from /usr/lib/ccache/:

cmake -DCMAKE_BUILD_TYPE=Release .. -- The C compiler identification is GNU 4.8.4 -- The CXX compiler identification is GNU 4.8.4 -- Check for working C compiler: /usr/lib/ccache/cc -- Check for working C compiler: /usr/lib/ccache/cc -- works -- Detecting C compiler ABI info -- Detecting C compiler ABI info - done -- Check for working CXX compiler: /usr/lib/ccache/c++ -- Check for working CXX compiler: /usr/lib/ccache/c++ -- works ...etc...

 

Last modified: 2016-02-07
Stéphane Charette, stephanecharette@gmail.com
../