400 028 6601

建站动态

根据您的个性需求进行定制 先人一步 抢占小程序红利时代

如何加快C++代码的编译速度转ccache-创新互联

http://www.cnblogs.com/baiyanhuang/archive/2010/01/17/1730717.html

  C++代码一直以其运行时的高性能高调面对世人, 但是说起编译速度,却只有低调的份了。比如我现在工作的源代码,哪怕使用Incredibuild调动近百台机子,一个完整的build也需要四个小时,恐怖!!!虽然平时开发一般不需要在本地做完整的build,但编译几个相关的工程就够你等上好一段时间的了(老外管这个叫monkey around,相当形象)。想想若干年在一台单核2.8GHZ上工作时的场景 - 面前放本书,一点build按钮,就低头读一会书~~~往事不堪回首。如何加快C++代码的编译速度转c
cache

可以想象,如果不加以重视,编译速度极有可能会成为开发过程中的一个瓶颈。那么,为什么C++它就编译的这么慢呢?

目前创新互联公司已为上1000家的企业提供了网站建设、域名、雅安服务器托管、网站托管、服务器租用、企业网站设计、双湖网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。

我想最重要的一个原因应该是C++基本的"头文件-源文件"的编译模型:

  1. 每个源文件作为一个编译单元,可能会包含上百甚至上千个头文件,而在每一个编译单元,这些头文件都会被从硬盘读进来一遍,然后被解析一遍。
  2. 每个编译单元都会产生一个obj文件,然后所以这些obj文件会被link到一起,并且这个过程很难并行。

这里,问题在于无数头文件的重复load与解析,以及密集的磁盘操作。

下面从各个角度给出一些加快编译速度的做法,主要还是针对上面提出的这个关键问题。

代码角度

综合技巧

编译资源

要提高速度,要么减少任务,要么加派人手,前面两个方面讲得都是减少任务,而事实上,在提高编译速度这块,加派人手还是有着非常重要的作用的。

另外,这本书谈了很多这方面的内容:大规模C++程序设计。     CCache

Using CCache to speed up compilation

CCache is nothing more than a cache for your compiler. ccache is usually very easy to install. Here’s an example for Ubuntu systems:
sudo apt-get install ccache
ccache will cache previous compilations, detect when the same compilation is being done again, and reuse its cache instead of recompiling the source code again. This can speed up your compilation by many orders of magnitude, especially in those situations where your file timestamps change, and make is triggering a recompile.
To enable ccache, simply add ‘/usr/lib/ccache’ to the beginning of your PATH. This directory contains symlinks to ccache, and ccache is smart enough to look at the name of the calling executable to determine which real executable to run. I.e. there is a symlink from ‘/usr/lib/ccache/g++’ to just ‘ccache’, but it actually runs the equivalent of ‘ccache g++’.
Using colorgcc to colorize output

colorgcc is a colorizer for the output of GCC, and allows you to better interpret the compiler warnings/errors.
To enable both colorgcc and ccache, perform the following steps:
Install colorgcc on an Ubuntu system with
sudo apt-get install colorgcc
To enable colorgcc, perform the following steps:
cp /etc/colorgcc/colorgccrc $HOME/.colorgccrc
edit the $HOME/.colorgccrc file, search for the following lines:
g++: /usr/bin/g++
gcc: /usr/bin/gcc
c++: /usr/bin/g++
cc: /usr/bin/gcc
g77: /usr/bin/g77
f77: /usr/bin/g77
gcj: /usr/bin/gcj
and replace them with:
g++: ccache /usr/bin/g++
gcc: ccache /usr/bin/gcc
c++: ccache /usr/bin/g++
cc: ccache /usr/bin/gcc
g77: ccache /usr/bin/g77
f77: ccache /usr/bin/g77
gcj: ccache /usr/bin/gcj
create a $HOME/bin or $HOME/sbin directory, and create the following softlinks in it
ln -s /usr/bin/colorgcc c++
ln -s /usr/bin/colorgcc cc
ln -s /usr/bin/colorgcc g++
ln -s /usr/bin/colorgcc gcc
make sure that $HOME/bin or $HOME/sbin is the first directory in your $PATH, e.g.:
export PATH=$HOME/bin:$PATH
or:
export PATH=$HOME/sbin:$PATH
depending on where you stored the colorgcc softlinks, so that when cc/gcc/g++/c++ is invoked the freshly created softlinks get activated first and not the global /usr/bin/{cc,gcc,g++,c++}.


分享名称:如何加快C++代码的编译速度转ccache-创新互联
本文网址:http://mzwzsj.com/article/csoios.html

其他资讯

让你的专属顾问为你服务