问题引入
C/C++语言是编译型的语言,随着项目的逐渐增大,会让项目的编译时间显著变长。这对写代码而言是一个糟糕的体验。因为是快速检查代码循环的时间变长,打断查找问题的思路。
尤其是在使用各种措施,比如前向声明,少使用模板(减少预编译时间)的情况下,依然不能显著减少编译时间。那么分布式编译就有了用武之地
distcc
distcc是一个开源的C/C++的分布式编译工具, 使用C/S架构,将代码基于分布式编译,从而加快编译速度。按照distcc的官网说法,可以布置在pc上,并且设置上限,来使用一些额外的算力。
debian的安装
最喜欢的linux发行版就是Debian。所以,我们用debian作为例子。debian需要安装如下的dist相关的包。
Note: 所有加入分布式编译的服务器都需要按照如下处理
debian version
➜ ~ sudo lsb_release -a
No LSB modules are available.
Distributor ID: Debian
Description: Debian GNU/Linux 11 (bullseye)
Release: 11
Codename: bullseye
需要安装的包
~ sudo apt list --installed | grep distcc
可以得到如下的输出:
#pump模式,相对于基本模式,会把预处理的部分也会分布式到各自的server。
distcc-pump/stable,now 3.4+really3.3.5-3 amd64 [installed]
#distcc本尊
distcc/stable,now 3.4+really3.3.5-3 amd64 [installed]
#gnome gui下的一个小工具,可以查看分布式编译器的工作状态
distccmon-gnome/stable,now 3.4+really3.3.5-3 amd64 [installed]
安装命令
sudo apt install update
sudo apt install distcc-pump distcc distccmon-gnome
Note 如果觉得apt的命令装包太慢,移步去看看apt-fast这个小工具
起服务
不通过源码安装,而通过上述方式安装有一个优点就是相应的启动脚本,以及配置脚本已经就位到相应的位置,只需要稍微配置一下就可以了。
修改配置脚本
distcc的配置文件在/etc/init.d/distcc
配置文件的选项都有很好的注释,可以直接阅读。
# Defaults for distcc initscript
# sourced by /etc/init.d/distcc
#
# should distcc be started on boot?
#
# STARTDISTCC="true"
STARTDISTCC="true"
#
# Which networks/hosts should be allowed to connect to the daemon?
# You can list multiple hosts/networks separated by spaces.
# Networks have to be in CIDR notation, e.g. 192.168.1.0/24
# Hosts are represented by a single IP address
#
# ALLOWEDNETS="127.0.0.1"
ALLOWEDNETS="127.0.0.1 192.168.0.6"
#
# Which interface should distccd listen on?
# You can specify a single interface, identified by it's IP address, here.
#
# LISTENER="127.0.0.1"
LISTENER="192.16