使用ccache加快编译速度
背景¶
ccache是一个编译器缓存,可以将编译的结果缓存起来。这样尽管第一次编译会花费长一点的时间,不过之后再次编译将变得非常非常快。
安装¶
使用¶
安装之后基本不用进行什么配置就可以直接使用了。例如之前要编译一个test.c文件要执行命令:
改成
ccache默认是将结果缓存保存到 $HOME/.ccache 目录下。如果想要修改这个目录,可以修改 CCACHE_DIR 环境变量。例如:
但是如果只是这样修改,使用ccache -s命令看是没有看到效果的。
所以我们ccache -h看下命令介绍:
└─[$] <> ccache -h
Usage:
    ccache [options]
    ccache compiler [compiler options]
    compiler [compiler options]          (via symbolic link)
Options:
    -c, --cleanup         delete old files and recalculate size counters
                          (normally not needed as this is done automatically)
    -C, --clear           clear the cache completely (except configuration)
    -F, --max-files=N     set maximum number of files in cache to N (use 0 for
                          no limit)
    -k, --get-config=K    get the value of the configuration key K
    -M, --max-size=SIZE   set maximum size of cache to SIZE (use 0 for no
                          limit); available suffixes: k, M, G, T (decimal) and
                          Ki, Mi, Gi, Ti (binary); default suffix: G
    -o, --set-config=K=V  set configuration key K to value V
    -p, --print-config    print current configuration options
    -s, --show-stats      show statistics summary
    -z, --zero-stats      zero statistics counters
    -h, --help            print this help text
    -V, --version         print version and copyright information
See also <https://ccache.samba.org>.
发现可以通过--set-config=K=V设置,但K是我们我们不知道,先用ccache -p找到K,最终我们设置为:
看下ccache -s,发现成功了。
└─[$] <> ccache -s
cache directory                     /home/solo/ext-data/.ccache
primary config                      /home/solo/.ccache/ccache.conf
secondary config      (readonly)    /etc/ccache.conf
stats updated                       Wed Feb 23 11:18:44 2022
cache hit (direct)                 57342
cache hit (preprocessed)             441
cache miss                         59571
cache hit rate                     49.24 %
called for link                        2
called for preprocessing            3326
unsupported code directive            15
no input file                       1197
cleanups performed                     0
files in cache                    178520
cache size                          27.4 GB
max cache size                      50.0 GB
总结¶
安装¶
默认配置¶
默认缓存目录为$HOME/.ccache,如果需要更新默认缓存目录,则在~/.ccache/ccache.conf 中配置cache_dir = /home/solo/ext-data/.ccache