Speeding up your recurring gcc compilations with ccache

Document created by Vincent Stehle Employee on Jun 24, 2013Last modified by Vincent Stehle Employee on Aug 19, 2014
Version 5Show Document
  • View in full screen mode


ccache is a C compiler cache.

ccache can save a large amount of compilation time on recurring builds and builds restarted from a clean repository after make clean or git clean. It is well suited for e.g. u-boot and Linux compilation.


Caching the host compiler


Caching "native" builds is easily done by adding in the beginning of your $PATH a special directory, which contains links to ccache to override the usual compiler.


On e.g. Debian this directory is readily available as /usr/lib/ccache, So you can do:


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


Typical links found in this folder are:

  c++ -> ../../bin/ccache

  cc -> ../../bin/ccache

  g++ -> ../../bin/ccache

  gcc -> ../../bin/ccache




Caching the cross compiler


Caching cross-compiled builds can be done in the same way as native builds, provided you create links of the form e.g. arm-linux-gnueabihf-gcc pointing to ccache.


But there is an even more convenient way for those projects, which rely on a $CROSS_COMPILE environment variable (as is the case for e.g. u-boot and Linux). You can prefix the cross compiler with ccache there in e.g. the following way:


  $ export CROSS_COMPILE="ccache arm-linux-gnueabihf-"


Monitoring efficiency


Now that your builds are cached, you might want to see how much is "spared" with this technique. ccache -s will tell you all sorts of statistics, such as:


  cache directory                     /home/vstehle/.ccache

  cache hit (direct)                 10852

  cache hit (preprocessed)            3225

  cache miss                         19000

  called for link                    33267

  called for preprocessing            9463

  compile failed                         3

  preprocessor error                     1

  couldn't find the compiler           117

  unsupported source language          921

  unsupported compiler option         2167

  no input file                      31681

  files in cache                     51694

  cache size                           1.3 Gbytes

  max cache size                       4.0 Gbytes


Here you see a somewhat typical 50%/50% hit/miss ratio.




See Also

  • ccache is usually supported natively by build systems, such as Buildroot or Yocto.