Linux series explanation - use ccache to improve compilation speed

It often takes several hours to compile a super-large project (such as the android system) on an ordinary computer, which greatly affects development and debugging. The good news is that using the ccache compilation cache mechanism can reduce compilation time to a certain extent.

* It is recommended to use the ccache project on github, compile the installation package locally after downloading the source code, and then install it to the ubuntu system.

1. Download ccache source code

sun@sun-pc:~/myproject$ git clone https://github.com/ccache/ccache.git
正克隆到 'ccache'...
remote: Enumerating objects: 23568, done.
remote: Counting objects: 100% (2505/2505), done.
remote: Compressing objects: 100% (359/359), done.
remote: Total 23568 (delta 2268), reused 2254 (delta 2145), pack-reused 21063
接收对象中: 100% (23568/23568), 10.49 MiB | 157.00 KiB/s, 完成.
处理 delta 中: 100% (17362/17362), 完成.

2. Compile and install ccache

sun@sun-pc:~/myproject$ cd ccache
sun@sun-pc:~/myproject/ccache$ mkdir build && cd build

#用cmake构建工程编译文件,如果没有用apt安装就行,构建成功后此文件夹会出现Makefile等文件
sun@sun-pc:~/myproject/ccache/build$ cmake -DCMAKE_BUILD_TYPE=Release ..

#编译ccache
sun@sun-pc:~/myproject/ccache/build$ make

#安装ccache,默认安装位置/usr/local/bin/
sun@sun-pc:~/myproject/ccache/build$ sudo make install

Problems and solutions:
(1) The following problems occur when cmake is executed, indicating that the CMake version is too low

sun@sun-pc:~/myproject/ccache/build$ cmake -DCMAKE_BUILD_TYPE=Release ..
CMake Error at CMakeLists.txt:1 (cmake_minimum_required):
  CMake 3.15 or higher is required.  You are running version 3.10.2


-- Configuring incomplete, errors occurred!

Solution: The software version of the apt source is generally lower, you can install the new version of CMake on snap

#使用snap安装cmake
sun@sun-pc:~/myproject/ccache/build$ sudo snap install cmake --classic

#如果没有配置snap的PATH,需要配置一下
sun@sun-pc:~/myproject/ccache/build$ PATH=$PATH:/snap/bin

#查看安装的cmake版本,可以看到这个版本是大于3.15的,符合要求
sun@sun-pc:~/3rdcode/ccache/build$ cmake --version
cmake version 3.24.1

CMake suite maintained and supported by Kitware (kitware.com/cmake).

(2) If the following problems occur during the make process, it is because the c++ version is incorrect, and the filesystem is a c++17 library. To install c++17, please refer to the Linux series explanation——Install gcc 9.1.0 on Ubuntu 18.04 ( C++17)

sun@sun-pc:~/myproject/ccache/build$ make
...
In file included from /home/sun/myproject/ccache/src/Args.cpp:21:0:
/home/sun/myproject/ccache/src/Util.hpp:25:10: fatal error: filesystem: 没有那个文件或目录
 #include <filesystem>
          ^~~~~~~~~~~~
compilation terminated.
src/CMakeFiles/ccache_framework.dir/build.make:75: recipe for target 'src/CMakeFiles/ccache_framework.dir/Args.cpp.o' failed
make[2]: *** [src/CMakeFiles/ccache_framework.dir/Args.cpp.o] Error 1
CMakeFiles/Makefile2:844: recipe for target 'src/CMakeFiles/ccache_framework.dir/all' failed
make[1]: *** [src/CMakeFiles/ccache_framework.dir/all] Error 2
Makefile:165: recipe for target 'all' failed
make: *** [all] Error 2

3. Configure environment variables

export USE_CCACHE=1                        #是否开启ccache,如果不用请设置为0
export CCACHE_COMPRESS=1                   #编译缓存是否压缩,压缩节省空间但性能略低。【 1:压缩 】【 0:不压缩 】
export CCACHE_EXEC=/usr/local/bin/ccache   #ccache的执行程序路径
export CCACHE_DIR=                         #这个是缓存文件路径,默认是 ~/.ccache目录
${CCACHE_EXEC} -M 50G                      #设置缓存目录大小,根据具体情况而定

Problem and solution:
If ${CCACHE_EXEC} -M 50Gan error similar to the following occurs during execution, the reason is that there is no GLIBCXX_3.4.26 symbol in libstdc++.so.6, and libstdc++.so.6 is a soft link, pointing to a certain version of libstdc++.so. 6.xx, so just copy the libstdc++.so library containing the GLIBCXX_3.4.26 symbol to the lib64 directory, and relink libstdc++.so.6 to this new library.

/usr/local/bin/ccache: /usr/lib/x86_64-linux-gnu/libstdc++.so.6: version `GLIBCXX_3.4.26' not found (required by /usr/local/bin/ccache)

The gcc 9.1.0 source package contains the libstdc++ library containing GLIBCXX_3.4.26. If you want to install gcc 9.1.0, please refer to the Linux series explanation——Ubuntu18.04 installs gcc 9.1.0 (C++17) .
You can use the strings command to view the symbols in the library, and then find the libc++ library file that meets the requirements. You can refer to the following commands:

find -name "libstdc++.so.*"
strings ./build/x86_64-pc-linux-gnu/libstdc++-v3/src/.libs/libstdc++.so.6.0.26 | grep GLIBCXX_3.4.26

4. Verify that ccache compiles faster

Test object: Android10 code of Qualcomm QCM6125 platform
Computer configuration: I7-10700 (8 cores and 16 threads), 32G RAM, SN550 1T solid state drive

The out directory will be deleted before each compilation, and the data is as follows:

#第一次编译的时间感觉有点怪,可能是我out目录忘了清理。第一次编译要从头开始缓存编译文件,正常要慢一些的。
共耗时: 128min 33s   第一次编译 -j8   USE_CCACHE=true  
共耗时: 93min 2s     第二次编译 -j8   USE_CCACHE=true
共耗时: 88min 32s    第三次编译 -j16  USE_CCACHE=true
共耗时: 204min 31s   第四次编译 -j16  USE_CCACHE=false
共耗时: 241min 28s   第五次编译 -j8   USE_CCACHE=false
共耗时: 91min 34s    第六次编译 -j8   USE_CCACHE=true

Guess you like

Origin blog.csdn.net/In_engineer/article/details/126453724