Skip to content

Instantly share code, notes, and snippets.

@zawzaww
Last active July 18, 2024 10:02
Show Gist options
  • Save zawzaww/9a380351f3e5a1c9c4cfc54ef4fdb913 to your computer and use it in GitHub Desktop.
Save zawzaww/9a380351f3e5a1c9c4cfc54ef4fdb913 to your computer and use it in GitHub Desktop.
Speeding Up C/C++ Builds Using Ccache (Compiler Cache)

Speeding Up C/C++ Builds Using Ccache

This page contains a guide on how to setup Ccache, Compiler Cache, and speed up the C/C++ compilation time using Ccache. And this guide will focus on the Linux x86_64 system.

Prerequisities

Install Ccache

Go to Ccache download page https://ccache.dev/download.html

Download the current, latest Ccache version. We'll use the Linux x86_64 binary release.

$ curl -LO https://github.com/ccache/ccache/releases/download/v4.10.1/ccache-4.10.1-linux-x86_64.tar.xz

Extract the downloaded Ccache tar.xz file.

$ tar -xf ccache-4.10.1-linux-x86_64.tar.xz
$ cd ccache-4.10.1-linux-x86_64

Install Ccache,

$ sudo make install

Then, check the Ccache version,

$ ccache -V
ccache version 4.10.1
Features: file-storage http-storage redis+unix-storage redis-storage

Copyright (C) 2002-2007 Andrew Tridgell
Copyright (C) 2009-2024 Joel Rosdahl and other contributors

Setup Ccache Environment

Setup the Ccache environment, cache directory and size.

Optionally, you can set the ccache size. The default size is 5Gi.

$ export CCACHE_DIR=~/.ccache && ccache -M 8Gi

Using Ccache (Compiler Cache)

In the User-space C/C++ Program

In this section, we will focus on speeding up the compilation of user-space C/C++ programs with Ccache.

Prefix your GCC command with ccache if you use GCC.

$ ccache gcc -c example.c

Or, if you use the Make build system,

$ make CC="ccache gcc" -j$(nproc --all)

In the Linux kernel

In this section, we will focus on speeding up the compilation of the Linux kernel with Ccache.

Previously, I wrote a blog post on speeding up the Linux kernel builds with Ccache. Please, see https://zawzaww.github.io/posts/speedup-linux-kernel-builds

Go to the Linux kernel source tree and configure the kernel.

$ make defconfig

To compile the Linux kernel with Ccache, you will need to add the CC="ccache gcc" option with the make command.

$ make CC="ccache gcc" -j$(nproc --all)

If you want to know the exact compilation time result, you need to use the time command.

$ time make CC="ccache gcc" -j$(nproc --all)

Or, if you want to compile the Linux kernel with Clang/LLVM.

$ time make CC="ccache clang" -j$(nproc --all)

The final testing result summary,

real     4m18.145s
user     5m20.449s
sys      3m50.917s

In the OpenWrt firmware

In this section, we will focus on speeding up the compilation of the OpenWrt firmware with Ccache.

Go to the OpenWrt source and update the feeds.

$ cd openwrt
$ ./scripts/feeds update -a
$ ./scripts/feeds install -a

Configure the OpenWrt firmware image before compile.

$ make menuconfig

Then, Go to Advanced configuration options --> Enable Use ccache

Or

Add the following config in .config file. Replace with your username.

CONFIG_CCACHE=y
CONFIG_CCACHE_DIR="/home/<username>/.ccache"

Then, compile the OpenWrt firmware image.

$ time make -j$(nproc --all)

The final testing result summary,

1002.00s   user
387.08s    system
362%       cpu
6:23.19    total

The Final Testing Result

Here is the final testing result summary for building the OpenWrt firmware for the x86_64 system on my laptop computer that has an 8-core CPU (Intel Core i7).

Without CCACHE

[30m 57.26s]

7607.09s   user
1221.91s   system
475%       cpu
30:57.26   total

With CCACHE

[6m 36.13s]

1040.70s  user 
395.87s   system 
362%      cpu 
6:36.13   total

Notes

  • Without Ccache, the compilation time 30m 57.26s.
  • With Ccache CONFIG_CCACHE=y, the compilation time is 6m 36.13.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment