翱翔天空11.4 提升编译速度 ccache distcc
程序员常常为编译大型程序时较长的编译时间苦恼。福音来了,使用ccache 和distcc,可以显著提高gcc和g++的编译速度。
使用非常简单
A ccache
1 安装ccache
$ sudo apt-get install ccache
2使用ccache
$ ccache gcc a.cpp
可以在~/.bin下加入几个软链接
$ ln -s /usr/bin/ccache gcc
$ ln -s /usr/bin/ccache g++
然后修改.bashrc 加入如下行
PATH=~someuser/.bin:$PATH
$ . ~/.bashrc
以后直接使用gcc 和g++命令时也能高速快捷了。赶快试试吧。
管理ccache
Ccache 会在用户根目录下存放一些缓冲文件,可以使用ccache 命令来管理,比如
$ ccache -s
可以查看现在缓冲文件的统计信息,其他用法可查看帮助
$ ccache –h
比如想设定缓冲文件夹的总大小等。
B distcc
Distcc 可以让其他机器一起编译项目
1 客户端client、服务器server 上distcc安装
root@client:~$ apt install distcc
root@server:~$ apt install distcc
2 服务器启动distccd
在其他机器上也安装distccd 安装完后使用如下命令启动distccd
--allow 参数指明了允许连接的客户端ip ,使用点分十进制
root@server:~$ distccd --user=nobody --daemon --allow=10.73.139.40/22
可以将该命令写入/etc/rc.local,这样机器启动后自动启动distccd
root@test72:~# cat /etc/rc.local
#!/bin/sh
distccd --user=nobody --daemon --allow=10.73.139.40/22
3 客户端设定DISTCC_HOSTS环境变量指明client要连接的服务端ip列表
假定服务器ip 是10.190.4.2 以空格分割。
root@client:~$ export DISTCC_HOSTS='10.190.4.2 localhost'
完成!
现在可以试一下
root@client:~# cat a.cpp
#include
int main(){
std::printf("hello, distcc!\n");
}
root@client:~# distcc g++ a.c
root@client:~# ./a.out
hello, distcc!
我们用大型项目git试试编译速度。
首先 需要创建一个make 的别名
Client $ alias make='make –j 32'
然后编译git
Client $ make
同时可以在另一个标签中查看编译的情况
Client $ distccmon-text 2
11000 Compile credential.stdout.ubuntu.10990 10.190.4.2[0]
11001 Compile credential.stdout.ubuntu.10984 10.190.4.2[1]
10931 Compile imap-send.stdout.ubuntu.10906. localhost[0]
10897 Compile fast-impor.stdout.ubuntu.10887 localhost[1]
………………………………………
可以看到服务器也在帮我们编译一些源码文件。
服务器越多越好,如果有两台服务器和本机一起编译,官方说能将任务速度提高到原来的2.6倍。