使用cache加快编译速度的命令详解

2022-07-28,,,,

ubuntu 安装ccache

  1. sudo apt-get install ccache
  2. 安装完后确认安装执行which ccache
$ which ccache
/usr/bin/ccache

3.在 ~/.bashrc 或者 ~/.zshrc文件内追加以下内容

# ccache
export use_ccache=1
export ccache_sloppiness=file_macro,include_file_mtime,time_macros
export ccache_umask=002

source /.bashrc或者/.zshrc
4. ccache默认设置的5g磁盘空间,正常来说够用,如果担心不够可以改大一些,
ccache -m 30g
5. 通过版本确认安装成功

$ ccache --version
ccache version 3.4.1
copyright (c) 2002-2007 andrew tridgell
copyright (c) 2009-2018 joel rosdahl

6.可以通过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

使用libzmq测试ccache

1.通过github下载 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.

2.在 libzmq目录内建立 build目录

3.修改cmakelists.txt, '+'后面的代表新增

──────┬───────────────────────────────────────────────────────────────────────────────────────
       │ 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)

4.在build目录执行cmake ..
打印显示 -- use ccache 表示启用ccache,但需要注意的事,每个项目在第一次启用ccache时,不会加快编译速度,而是把编译缓存保存到 /home/username/.ccache目录,供以后编译使用

$ 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 
...

5.使用 /usr/bin/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

6.rm -rf * 删除build目录内的所有文件

7.重新 cmake ..

8.使用 /usr/bin/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

到此这篇关于使用cache加快编译速度的文章就介绍到这了,更多相关cache加快编译速度内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

《使用cache加快编译速度的命令详解.doc》

下载本文的Word格式文档,以方便收藏与打印。