高速的C/C++编译工具——ccache 使用简介:编译提速 () (linux(non-win) /windows)

高速的C/C++编译工具——ccache 使用简介:编译提速 () (linux(non-win) /windows)高速的 C C 编译工具 ccache 使用简介 编译提速 ccache

大家好,欢迎来到IT知识分享网。

目录

高速的C/C++编译工具——ccache :通用

ccache使用简介

Qt编译提速:针对 Qt

Integrating ccache into your build

distcc  高速的C/C++编译工具——ccache 使用简介:编译提速 () (linux(non-win) /windows)

官方手册:Ccache manual (non-windows(e. linux) /Windows

———————————————–

关联参考:

提高Qt Creator编译速度的7种方法,亲测可行(★firecat推荐★)  高速的C/C++编译工具——ccache 使用简介:编译提速 () (linux(non-win) /windows)  << 方法1:工程.pro文件加入预编译机制,PRECOMPILED_HEADER << 实测:相比之下,最有效

  高速的C/C++编译工具——ccache 使用简介:编译提速 () (linux(non-win) /windows)

提高Qt Creator编译速度的7种方法:Qt Creator 的编译如此之慢?(*)  高速的C/C++编译工具——ccache 使用简介:编译提速 () (linux(non-win) /windows)

要点:

1. 方法1:预编译头文件

工程.pro文件加入预编译机制,PRECOMPILED_HEADER

错误信息:

fatal error: iostream: No such file or directory

最主要原因就是:

iostream是c++的头文件,需要使用g++指令而不是gcc。

代码文件命名时,后缀名 .c 和 .cpp 是不一样的。会导致C++的库无法识别。

解决 1法:[Error] iostream.h: No such file or directory的解决办法:更改文件后缀  高速的C/C++编译工具——ccache 使用简介:编译提速 () (linux(non-win) /windows)

解决 2法:fatal error: iostream: 没有那个文件或目录 原因与解决方法:使用命名空间  https://www.fujieace.com/c-c/fatal-error-iostream.html

解决 3法:(实际用了这个)

#if defined __cplusplus

# include <iostream>

#endif //__cplusplus

2. 方法2:本文,ccache << 提速明显

从 Qt 5.9 开始,有一个更简单的方式:

load(ccache)

3. 测试如下,原因待分析

 .

 .

 .

结论:

———————————————–

ccache 的用法

ccache 常用命令

  

ccache -C # 清除所有

ccache -M 128M  #设置最大 128MB

ccache -M 2G  #设置最大 2GB ?

在 .pro文件中设置:

load(ccache)

————————————————-

官网 ,及其他

Ccache — Compiler cauche

Latest release

Platform Archive
All (source code release) ccache-4.8.2.tar.gz (signature)
ccache-4.8.2.tar.xz (signature)
Darwin (macOS) universal binary release ccache-4.8.2-darwin.tar.gz (signature)
Linux x86_64 binary release ccache-4.8.2-linux-x86_64.tar.xz (signature)
Windows i686 binary release
Note: B-level support
ccache-4.8.2-windows-i686.zip (signature)
Windows x86_64 binary release
Note: B-level support
ccache-4.8.2-windows-x86_64.zip (signature)

See also the ccache 4.8.2 release notes.

Notes

 You can also find older releases on the releases page on GitHub.

———————-

存放的旧版本

https://www.samba.org//ftp/ccache/

———————————————–

Qt 出现 [debug/main.o] Error 2

Qt编译错误: [debug/main.o] Error 2 问题(ccache 造成)

在 linux上,正常。

使用了ccache

    QMAKE_CXX = ccache $$QMAKE_CXX

注释掉重新编译一遍。

问题,应该是 linux安装了 ccache;而 win上没有安装?????

———————————————–

参考:

高速的C/C++编译工具——ccache 使用简介:编译提速 ()  << 提速明显
https://blog.csdn.net/ken2232/article/details/

===========================

Ccache — a fast C/C++ compiler cache 

  https://ccache.dev/documentation.html

官方手册:Ccache manual (non-windows(e. linux) /Windows)

Configuration

Ccache’s default behavior can be overridden by options in configuration files, which in turn can be overridden by environment variables with names starting with CCACHE_. Ccache normally reads configuration from two files: first a system-level configuration file and secondly a cache-specific configuration file. The priorities of configuration options are as follows (where 1 is highest):

  1. Command line settings in KEY=VALUE form. Example:
    ccache debug=true compiler_check="%compiler% --version" gcc -c example.c
  2. Environment variables.
  3. The cache-specific configuration file (see below).
  4. The system (read-only) configuration file <sysconfdir>/ccache.conf (typically /etc/ccache.conf or /usr/local/etc/ccache.conf).
  5. Compile-time defaults.

As a special case, if the environment variable CCACHE_CONFIGPATH is set it specifies the configuration file, and the system configuration file won’t be read.

Location of the configuration file

non-Windows OS

The location of the cache-specific configuration file is determined like this on non-Windows systems:

  1. If CCACHE_CONFIGPATH is set, use that path.
  2. Otherwise, if the environment variable CCACHE_DIR is set then use $CCACHE_DIR/ccache.conf.
  3. Otherwise, if cache_dir is set in the system configuration file then use <cache_dir>/ccache.conf.
  4. Otherwise, if there is a legacy $HOME/.ccache directory then use $HOME/.ccache/ccache.conf.
  5. Otherwise, if XDG_CONFIG_HOME is set then use $XDG_CONFIG_HOME/ccache/ccache.conf.
  6. Otherwise, use $HOME/Library/Preferences/ccache/ccache.conf (macOS) or $HOME/.config/ccache/ccache.conf (other systems).

Windows OS

注:环境变量设置起来,好像有点麻烦?

个人电脑使用,将 ccache.exe 直接拷贝到:C:\Windows\System32 的目录下,就可以了。

windows下载的只是一个免安装的 exe文件,因此,需要用户自行设置。如路径等等。

高速的C/C++编译工具——ccache 使用简介:编译提速 () (linux(non-win) /windows)

On Windows, this is the method used to find the configuration file:

  1. If CCACHE_CONFIGPATH is set, use that path.
  2. Otherwise, if the environment variable CCACHE_DIR is set then use %CCACHE_DIR%/ccache.conf.
  3. Otherwise, if cache_dir is set in the system configuration file then use <cache_dir>\ccache.conf. The system-wide configuration on Windows is %ALLUSERSPROFILE%\ccache\ccache.conf by default. The ALLUSERSPROFILE environment variable is usually C:\ProgramData.
  4. Otherwise, if there is a legacy %USERPROFILE%\.ccache directory then use %USERPROFILE%\.ccache\ccache.conf.
  5. Otherwise, use %LOCALAPPDATA%\ccache\ccache.conf if it exists.
  6. Otherwise, use %APPDATA%\ccache\ccache.conf.

See also the cache_dir configuration option for how the cache directory location is determined.

Configuration file syntax

Configuration files are in a simple “key = value” format, one option per line. Lines starting with a hash sign are comments. Blank lines are ignored, as is whitespace surrounding keys and values. Example:

# Set maximum cache size to 10 GB: max_size = 10G

Boolean values

Some configuration options are boolean values (i.e. truth values). In a configuration file, such values must be set to the string true or false. For the corresponding environment variables, the semantics are a bit different:

  • A set environment variable means “true” (even if set to the empty string).
  • The following case-insensitive negative values are considered an error (instead of surprising the user): 0, false, disable and no.
  • An unset environment variable means “false”.

Each boolean environment variable also has a negated form starting with CCACHE_NO. For example, CCACHE_COMPRESS can be set to force compression and CCACHE_NOCOMPRESS can be set to force no compression.

===========================

Integrating ccache into your build

Using ccache together with qmake and QtCreator – Embedded bits and pixels

ccache configuration

One can configure ccache in many different ways. The default config (on Ubuntu 18.04) seems to be a maximum cache size of 5 GB. This can easily be changed. More information can be found here.

Qt编译提速

背景:编译大型项目文件时,速度较慢,此前已经使用参数-J3使用3个CPU进行编程,但是效果一般,编译速度依旧很慢。

解决方法:使用ccache,是一个编译器缓存,ccache并不是真正去读取、理解并解释其内容,只是将最终的文本拷贝到文件中,使得它可以立即被编译。

步骤:

  1.     下载安装ccache,sudo apt-get install ccache
  2.     ccache -M 6G ,划分6G空间给ccache使用。
  3.     Qt中配置使用,如图。

CONFIG += ccache   #在 .pro文件中添加,即可。

高速的C/C++编译工具——ccache 使用简介:编译提速 () (linux(non-win) /windows) 结果:第一次编译未见效果,第二次便速度飞起。

ccache –version    #查看ccache版本信息

ccache -M 512M     #可随时根据具体需求,来调整ccache的大小

ccache -s                #查看ccache的信息

ccache -C               #清除所有

掌握这个小技巧,让你的 C++ 编译速度提升 50 倍!

https://zhuanlan.zhihu.com/p/

二、使用 ccache 编译器缓存

ccache(全称:compiler cache)是一个编译器缓存,该工具会高速缓存编译生成的信息,并在编译的特定部分使用高速缓存的信息,比如头文件,这样就节省了通常使用 cpp 解析这些信息所需要的时间。

▓ qmake 设置

打开 .pro,添加以下配置,ccache 就可以工作了:

QMAKE_CXX= ccache $$QMAKE_CXX 
Qt 5.9 开始,有一个更简单的方式:
load(ccache)

▓ cmake 配置

在 CMakeLists.txt 中添加以下配置,将 ccache 作为编译命令和链接命令的启动器:

find_program(CCACHE_FOUND ccache) if(CCACHE_FOUND) set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE ccache) set_property(GLOBAL PROPERTY RULE_LAUNCH_LINK ccache) endif(CCACHE_FOUND)

如何加速Linux下的编译速度(加速make) ()

总结一下:

  • tmpfs: 解决IO瓶颈,充分利用本机内存资源
  • make -j: 充分利用本机计算资源
  • distcc: 利用多台计算机资源
  • ccache: 减少重复编译相同代码的时间

这些工具的好处都在于布署的成本相对较低,综合利用这些工具,就可以轻轻松松的节省相当可观的时间。上面介绍的都是这些工具最基本的用法,更多的用法可以参考它们各自的man page。

  • distcc

一台机器的能力有限,可以联合多台电脑一起来编译。这在公司的日常开发中也是可行的,因为可能每个开发人员都有自己的开发编译环境,它们的编译器版本一般是一致的,公司的网络也通常具有较好的性能。这时就是distcc大显身手的时候了。

使用distcc,并不像想象中那样要求每台电脑都具有完全一致的环境,它只要求源代码可以用make -j并行编译,并且参与分布式编译的电脑系统中具有相同的编译器。因为它的原理只是把预处理好的源文件分发到多台计算机上预处理、编译后的目标文件的链接和其它除编译以外的工作仍然是在发起编译的主控电脑上完成所以只要求发起编译的那台机器具备一套完整的编译环境就可以了。

distcc安装后,可以启动一下它的服务:

/usr/bin/distccd --daemon --allow 10.64.0.0/16

默认的3632端口允许来自同一个网络的distcc连接。

然后设置一下DISTCC_HOSTS环境变量,设置可以参与编译的机器列表。通常localhost也参与编译,但如果可以参与编译的机器很多,则可以把localhost从这个列表中去掉,这样本机就完全只是进行预处理、分发和链接了,编译都在别的机器上完成。因为机器很多时,localhost的处理负担很重,所以它就不再“兼职”编译了。

export DISTCC_HOSTS=&quot;localhost 10.64.25.1 10.64.25.2 10.64.25.3&quot;

然后与ccache类似把g++,gcc等常用的命令链接到/usr/bin/distcc上就可以了。

在make的时候,也必须用-j参数,一般是参数可以用所有参用编译的计算机CPU内核总数的两倍做为并行的任务数。

同样测试一下:

一台双核计算机,make -j4:23分16秒

两台双核计算机,make -j4:16分40秒

两台双核计算机,make -j8:15分49秒

跟最开始用一台双核时的23分钟相比,还是快了不少的。如果有更多的计算机加入,也可以得到更好的效果。

在编译过程中可以用distccmon-text来查看编译任务的分配情况。distcc也可以与ccache同时使用,通过设置一个环境变量就可以做到,非常方便。

如何提高工程编译速度 ()

https://cloud.tencent.com/developer/article/

Linux Qt 平台为例测试

编译总量 = 编译速度 * 编译时间

按照上面等式进行优化,则:

在编译总量不变的前提下,要减少编译时间,则可提升编译速度;

或在编译速度不变的情况下,较少编译总量;

再或者既减少编译总量又提高编译速度。

C/C++编译特点

C/C++ 编译单位是文件,无法进行跨模块优化,C++20可支持。

将.o文件链接在一起时很难并行。

预处理过程可并行进行。

提高编译速度

1 减小编译总量

包含大量头文件会导致预处理时间变长,针对此尽量使用前置声明。

Pimpl(Private Implementation)模式与不透明指针。

高内聚、低耦合,减少模块间的依赖。

删除不必要的头文件。

2 提高编译速度

在提升编译速度上,可以增加编译机器数量或者提高单机编译能力。针对整体工程。

2.1 提高单机编译能力

多核编译

make -j*

#减少重复编译相同代码的时间

ccache

解决IO瓶颈,充分利用内存资源,头文件非常多,预处理时需要反复从磁盘读取文件

tmpfs

2.2 使用多机进行

distcc :分布式编译,利用多台计算机资源共同编译一个程序,

   可用于内核或者大型程序(未做测试),简单讲链接过程在主机进行。

icecc : distcc升级版

Qt多核编译与ccache使用具体操作流程

1 Qt 设置多核编译

Qt侧边栏 Projects -> Build -> Build Steps

使用ccache加速Qt编译过程 ()

之前听说过ccache可以加快大型工程的编译速度,一直没时间使用,最近看到一篇关于加速Qt编译过程的文章,

就自己动手试了一下,因为Ubuntu版本是14.10 有点老,只能通过源码方式安装,下面只记载了安装过程中出现的问题

因为网上有很多安装教程,这里就不列了。

1. ccache的安装

xmllint和xsltproc会提示出错,参考这篇文章解决

2. ccache的使用

确定ccache的位置

#which ccache

root@vmw:/usr/local/bin# which gcc

/usr/bin/gcc  此时gcc还是原始的gcc,默认的gcc是在/usr/bin下,

通过如上命令设置过之后,系统就会使用/usr/local/bin下的gcc了,即包含了ccache封装的版本。原因是:

PATH环境变量中,/usr/local/bin在/usr/bin之前,所以会优先使用此gcc

declare -x PATH=”/usr/local/bin/ccache:/opt/tools/docbook-xsl-1.76.0/tools/bin:/opt/hisi-linux/x86-arm/arm-hisiv500-linux/target/bin:/opt/kaifaban/i.mx28x/tools/gcc-4.4.4-glibc-2.11.1-multilib-1.0/arm-fsl-linux-gnueabi/bin:/opt/hisi-linux/x86-arm/arm-hisiv400-linux/target/bin:/opt/hisi-linux/x86-arm/arm-hisiv300-linux/target/bin:/opt/tools/qt5.5/Tools/QtCreator/bin:/opt/hisi-linux/x86-arm/arm-hisiv200-linux/target/bin:/opt/hisi-linux/x86-arm/arm-hisiv100-linux/target/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin”

#which gcc

/usr/local/bin/gcc 此时的gcc已经是ccache封装的版本

交叉编译Qt时,需要修改qmake.conf中对应的:

QMAKE_LINK_SHLIB        = ccache arm-hisiv200-linux-g++

测试时,第一次编译Qt编译时间还是很长,查询编译日志可以发现ccache的身影,

make clean之后再编,奇迹发生了,日志的打印速度变得飞快,编译时间和之前不是一个数量级的。

参考:

linux运行qt速度慢,Linux下QT Creator 编译|构建源代码速度慢|加速的问题

https://mp.csdn.net/mp_blog/creation/editor/

QMAKE_CXX = ccache g++

===============================

ccache实际运行测试

需要设置 6GB吗?ccache -M 6G ,划分6G空间给ccache使用。

实际上,测试结果如下:

对于这个具体的测试代码来说,可能 128M 就可以了 ?

#设置为 256M

         

#设置完成的初始状态

                 

#运行了一次构建 Build之后的状态状态         

ccache 语法:官方文档

$ ccache -h

                

Common 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)
        –config-path PATH     operate on configuration file PATH instead of the
                               default





    -h, –help                 print this help text

                

Options for scripting or debugging:
        –checksum-file PATH   print the checksum (64 bit XXH3) of the file at
                               PATH
        –dump-manifest PATH   dump manifest file at PATH in text format
        –dump-result PATH     dump result file at PATH in text format



See also the manual on <https://ccache.dev/documentation.html>.

===============================

高速的C/C++编译工具——ccache

1、简介

       ccache(“compiler cache”的缩写)是一个编译器缓存,该工具会高速缓存编译生成的信息,并在编译的特定部分使用高速缓存的信息, 比如头文件,这样就节省了通常使用 cpp 解析这些信息所需要的时间。如果某头文件中包含对其他头文件的引用,ccache会用那个文件的 cpp-parsed版本来取代include声明,不是真正去读取、理解并解释其内容,ccache 只是将最终的文本拷贝到文件中,使得它可以立即被编译。ccache是以空间换取速度,ccache非常适合经常make clean(或删除out目录)后重新编译的情况。

(1)安装

apt-get install ccache

(2)查看安装位置

which ccache

(3)配置环境变量

       在~/.bashrc(或/etc/profile)中添加:

高速的C/C++编译工具——ccache 使用简介:编译提速 () (linux(non-win) /windows)

 注:设置cache位置

       默认情况下cache(缓存)会保存在~/.ccache目录下,如果要设置主目录位于NFS或其他非本地文件系统上,可获得更高的速度,如存储器支持,速度可提高10%到25%。在配置中添加如下配置:

(4)使配置生效

source .bashrc

(5)设置ccache大小

ccache -M 50G

       ccache的大小是一个动态的值,每次改变.ccache文件的位置,max cache size都会被初始化。该设置会保存到CCACHE_DIR中,且该命令是长效的,不会因系统重启而失效。使用ccache第一次编译后能够明显提高make clean以后再次的编译速度。

ccache –version

(7)查看ccache的信息

ccache -s

另:清除所有:ccache -C

 ccache -C

       ccache 主要是用来优化编译的。有两种方式使用ccache:

(1)选择性使用ccache:

       使用”ccache gcc”或”ccache g++”代替”gcc”或”g++” ,以 gcc 为第一个参数调用 ccache,而不是调用gcc。例如,要在命令行中编译一个文件,您通常会使用:

$ gcc foo.c

       要使用 ccache,您应该输入:

$ ccache gcc foo.c

       设置 CC 环境变量的值,配置makepkg,进入到你的/etc/makepkg.conf中加入下面几行:

       或者直接在命令行输入:

$ export set CC=’ccache gcc’

vim ~/.bash_profile

把/usr/lib/ccache/bin路径加到PATH下:

PATH=/usr/lib/ccache/bin:$PATH

这只是对ccache所自带的gcc起作用,有时候我们需要使用交叉编译工具,这时,仅仅通过以上操作是无法执行ccache编译的,通常会提示xxx路径没有这个编译工具,这时需要这样做:

(a)查看安装路径

$ which ccache

(b)创建文件夹

$ mkdir ~/.bin

(c)设置链接

同理,其他编译工具类似方式指定。

(d)PATH设置

将~/.bin/放在 arm-linux-gcc等的PATH的前面。如:

然后重启即可

(e)$ which arm-linux-gcc

/home//.bin/arm-linux-gcc 确认

这样每次启动g++的时候都会启动/usr/lib/ccache/bin/g++,而不会启动/usr/bin/g++,效果跟使用命令行ccache g++效果一样。每次用户登录时,使用g++编译器时会自动启动ccache。

如果您只是想为一个项目启用 ccache,比如说编译 Perl 等第三方工具时,那么您或者可以使用第二种方式,或者可以告知配置脚本或 make 命令使用哪个C编译器。

       默认情况下cache(缓存)会保存在~/.ccache目录下。此目录可自行修改。下面列举了一些选项让您可以更深入地控制高速缓存设置:

       discc是Samba小组开发的一个工具,它让您可以将编译过程分布到多台机器上。只需要为 make 添加多任务选项(使用 -j 命令行选项),它就可以有效地提高同步编译的数目。distcc系统的工作方式是,每台主机上有一个后台进程,接收最终预解析格式的源文件,然后在本地进行编译,返回生成的对象文件。

如果使用得当,在每加入一个新的同样节点时,编译时间通常应该会以稍微低于线性的比率下降,不过您将只会在那些远不只一个源文件的项目上看到这样的影响,因为 distcc 只是分布全部源文件。

由于 distcc 所分布的是解析过的文件,所以您可以组合 ccache,它可以加速 C 预处理过程部分,同时distcc 可以完成到对象代码的实际编译。要以这种方式来使用 distcc 和 ccache,需要在主机上配置distcc,在主要的开发机器上配置 distcc 和 ccache。

使用 ccache 和 distcc 所需要的环境变量:

       环境变量定义如下:

       现在,当运行 make 时,如果使用了 -j 选项来指定要执行的同步编译的数目,则首先使用 ccache 解析文件(如果需要,使用高速缓存),然后将其分布到某个 distcc 主机。

尽管 distcc 加速了编译过程,但是它没有改变环境的基本限制。例如,您不应该将 make 执行的同步作业的数目设置得大于可用 CPU 数目的两倍。例如,如果您有四台两路机器,那么将作业值设置为超过 16 的值时将不再会观察到有多大改善。

参考:

https://ccache.dev/

高速的C/C++编译工具——ccache_king_weng的博客-CSDN博客

Qt编译提速_雲烟的博客-CSDN博客

ccache使用简介_悦码客的博客-CSDN博客

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://haidsoft.com/127242.html

(0)
上一篇 2025-09-12 17:26
下一篇 2025-09-12 17:45

相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注微信