Ubuntu 安装 ccache
- 执行 sudo apt-get install ccache 命令进行安装。
- 安装完成后,运行 which ccache 确认可执行文件路径。
$ which ccache
/usr/bin/ccache
在 ~/.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-2026 Joel Rosdahl
可通过 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 2026
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
从 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.
在 libzmq 源码目录下创建 build 目录。
编辑 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)
进入 build 目录执行 cmake ..。若打印显示 '-- use 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
...
使用 /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
执行 rm -rf * 删除 build 目录内所有文件。
重新执行 cmake .. 配置项目。
再次使用 /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 加快编译速度内容请搜索本站以前的文章或继续浏览下面的相关文章希望大家以后多多支持本站!