10

There is very little documentation on enabling ccache on GNU/Linux. Here is a response from launchpad.net:

At the moment, I think the best way to enable ccache is to add "/usr/lib/ccache" to the front of your path. If you want to enable it for all users by default, change the PATH variable in /etc/environment.

Can someone give me more information on enabling ccache?

Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
Arky
  • 380
  • 1
  • 4
  • 15
  • 3
    Hard to enable something you're not using. And how you're want to use it is a key. For make or compatible, export CC=ccache should work, if your makefile isn't too tricky one. – keltar Dec 18 '12 at 09:18

5 Answers5

7

Download latest version of ccache for better performance.

After Downloading, Follow the steps as mentioned below:

A) Tar the file using the following command :

 $tar -xvf ccache-3.2.4.tar.bz2
 
 Note : I'm using ccache 3.2.4 Version.You can download the latest one.

B) Go inside ccache-3.2.4 folder and run the following commands:

 $./configure
 $./make 
 $ sudo make install

C) Go to your .bashrc and insert the following :

 export CCACHE_DIR=/home/user_name/.ccache
 export CCACHE_TEMPDIR=/home/user_name/.ccache

 Note : Fill user_name with your User Name

D) Save your Bashrc and source it

 $ source ~/.bashrc

E) To check ccache is working or not type :

 ccache -M 10G : To Set the ccache Size to 10GB

F) To check ccache is working or not type :

 ccache -s : To check ccache statistics 
Community
  • 1
  • 1
Ankit Raj
  • 917
  • 1
  • 7
  • 18
6

The ccache manual has a section called Run modes which describes the official ways of activating ccache, so I suggest reading the manual.

Also, as you already noted, Linux distributions often set up a /usr/lib/ccache directory which is designed to be prepended to PATH.

Joel Rosdahl
  • 846
  • 9
  • 12
5

There are at least two methods:

i) Override the CC, CXX, ... flags in a Makefile. Within the R framework, a system and optional user configuration file is read, and I simply set

VER=4.7
CC=ccache gcc-$(VER)
CXX=ccache g++-$(VER)
SHLIB_CXXLD=g++-$(VER)
FC=ccache gfortran
F77=ccache gfortran

which also allows me to switch back and forth between gcc versions. Now all compilations involving R use ccache.

ii) For other uses, I have deployed the fact that /usr/local/bin/ is checked prior to /usr/bin. So one can do

root@max:/usr/local/bin# ls -l gcc
lrwxrwxrwx 1 root root 15 Jan 27 11:04 gcc -> /usr/bin/ccache
root@max:/usr/local/bin# ./gcc --version
gcc (Ubuntu/Linaro 4.7.2-2ubuntu1) 4.7.2
Copyright (C) 2012 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

root@max:/usr/local/bin# 

and now gcc is invoked via ccache:

edd@max:/tmp$ cp -vax ~/src/progs/C/benfordsLaw.c .
`/home/edd/src/progs/C/benfordsLaw.c' -> `./benfordsLaw.c'
edd@max:/tmp$ time /usr/local/bin/gcc -c benfordsLaw.c 

real    0m0.292s
user    0m0.052s
sys     0m0.012s
edd@max:/tmp$ time /usr/local/bin/gcc -c benfordsLaw.c 

real    0m0.026s
user    0m0.004s
sys     0m0.000s
edd@max:/tmp$ 
Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
2

Another possibility (instead of export CC=ccache commented by Keltar), if $HOME/bin/ is listed in your $PATH before /usr/bin/, would be to make a symlink

 ln -s /usr/bin/ccache $HOME/bin/gcc

Then every execvp(3) of gcc would find that symlink

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
-1

Ubuntu install ccache

  1. sudo apt-get install ccache
  2. Confirm installation execution after installation "which ccache"
$ which ccache
/usr/bin/ccache
  1. Add the following contents to the "~/.bashrc" or "~/.zshrc==" file
# ccache
export USE_CCACHE=1
export CCACHE_SLOPPINESS=file_macro,include_file_mtime,time_macros
export CCACHE_UMASK=002

source "~/.bashrc" or "~/.zshrc" 4. The 5 GB disk space set by default for ccache is normally enough. If you are worried about it, you can increase it, ccache -M 30G 5. Confirm successful installation through version

$ ccache --version
ccache version 3.4.1

Copyright (C) 2002-2007 Andrew Tridgell
Copyright (C) 2009-2018 Joel Rosdahl
  1. You can view the current configuration through ccache - s
cache directory                     /home/username/.ccache
primary config                      /home/username/.ccache/ccache.conf
secondary config      (readonly)    /etc/ccache.conf
stats zero time                     Fri Jul 22 16:15:40 2022
cache hit (direct)                  4186
cache hit (preprocessed)             875
cache miss                          1069
cache hit rate                      82.56 %
called for link                      653
cleanups performed                     0
files in cache                      3209
cache size                          159.3 MB
max cache size                      30.0 GB

Use libzmq to test ccache

  1. Through github download the source code of libzmq
$ git clone  https://github.com/zeromq/libzmq.git
Cloning into 'libzmq'...
remote: Enumerating objects: 43791, done.
remote: Counting objects: 100% (36/36), done.
remote: Compressing objects: 100% (28/28), done.
remote: Total 43791 (delta 11), reused 24 (delta 8), pack-reused 43755
Receiving objects: 100% (43791/43791), 21.91 MiB | 1.03 MiB/s, done.
Resolving deltas: 100% (31951/31951), done.
  1. Create the build directory in the libzmq directory
  2. Modify CMakeLists. txt , '+' to add
──────┬───────────────────────────────────────────────────────────────────────────────────────
       │ File: CMakeLists.txt
───────┼──────────────────────────────────────────────────────────────────────────────────────
   1   │ # CMake build script for ZeroMQ
   2   │ project(ZeroMQ)
   3   │ 
   4   │ if(${CMAKE_SYSTEM_NAME} STREQUAL Darwin)
   5   │   cmake_minimum_required(VERSION 3.0.2)
   6   │ else()
   7   │   cmake_minimum_required(VERSION 2.8.12)
   8   │ endif()
   9   │ 
  10 + │ find_program(CCACHE_FOUND ccache)
  11 + │ if(CCACHE_FOUND)
  12 + │     set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE ccache)
  13 + │     set_property(GLOBAL PROPERTY RULE_LAUNCH_LINK ccache)
  14 + │     message(STATUS "use ccache")
  15 + │ endif(CCACHE_FOUND)
  16 + │ 
  17   │ include(CheckIncludeFiles)
  1. Execute "cmake .." In the build directory Print and display **"-- use ccache" **means enable ccache, but note that when each project is enabled ccache for the first time, it will not speed up the compilation speed, but save the compilation cache to the "/home/username/. cache" directory for later compilation
$ cmake ..
-- The C compiler identification is GNU 7.5.0
-- The CXX compiler identification is GNU 7.5.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- use ccache
-- Looking for pthread.h
-- Looking for pthread.h - found
-- Looking for pthread_create
-- Looking for pthread_create - not found
-- Looking for pthread_create in pthreads
-- Looking for pthread_create in pthreads - not found
-- Looking for pthread_create in pthread
-- Looking for pthread_create in pthread - found
-- Found Threads: TRUE 
...
  1. Use the "/usr/bin/time" command to record the compilation time
/usr/bin/time make -j3

result:
48.79user 14.25system 0:21.60elapsed 291%CPU (0avgtext+0avgdata 176036maxresident)k
0inputs+282248outputs (0major+2406923minor)pagefaults 0swaps
  1. "rm - rf *" delete build all files in the directory
  2. cmake ..
  3. Use the "/usr/bin/time" command to record the compilation time
/usr/bin/time make -j3

result:
2.78user 2.42system 0:02.15elapsed 241%CPU (0avgtext+0avgdata 23736maxresident)k
0inputs+21744outputs (0major+232849minor)pagefaults 0swaps

https://www.cnblogs.com/jiangyibo/p/16516932.html

kongshui
  • 1
  • 1