This article presents a C++ design pattern to speed up the compilation
of your programs, especially if you have a lot of header files to
include and your changes often result in a lot of recompiling.
1. Before the pattern
Suppose we have six source files,
a.hh,
a.cc,
b.hh,
b.cc,
c.hh and
c.cc.
The file a.hh contains header file information for the a.cc file. The
file a.cc might look like this:
#include"a.hh"//// functions and variables and classes specific to a//
2. After the pattern
To speed up the compilation of these files it is usually wise to
rename
a.cc → a.ch
b.cc → b.ch
c.cc → c.ch
Where ch is a combination of the letters c and h.
The cc files have been renamed to ch files because under
the design pattern the files will not be compiled separately,
instead they will themselves be included by another cc
file called x.cc. Here is what x.cc will look like:
#include"a.ch"#include"b.ch"#include"c.ch"
Compiling x.cc is then equivalent to compiling a.cc, b.cc
and c.cc, and usually faster in most cases, provided that you have
a lot of header files to include and you need to "touch" all of the
files a.hh, b.hh and c.hh often. The benefits of this
pattern become invalidated as the number and size of the source files
increases.