3

I was looking at the doom3 code on github and I notice something unusual. Several files have only one include for a file called idlib/precompiled.h and this file have includes for several other headers like

...
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <assert.h>
#include <time.h>
#include <ctype.h>
#include <typeinfo>
#include <errno.h>
#include <math.h>
...

and to program headers

#include "../framework/BuildVersion.h"
#include "../framework/BuildDefines.h"
#include "../framework/Licensee.h"
#include "../framework/CmdSystem.h"
#include "../framework/CVarSystem.h"

I wonder if there's any good reason for that, because that's the first time I see such thing

Jens Mühlenhoff
  • 14,565
  • 6
  • 56
  • 113
ibrabeicker
  • 1,786
  • 2
  • 19
  • 31

2 Answers2

3

This is called precompiled headers. The main benefit is dramatically increased compilation speed.

All the headers in precompiled.h are compiled only once per project. Without precompiled headers, each header content would be compiled multiple times: for each .cpp file it is included in.

Stephan
  • 4,187
  • 1
  • 21
  • 33
  • so if I put a header that is used in most part of the system and change it, it would still require almost full rebuild or only the precompiled header would need recompilation? – ibrabeicker Dec 09 '11 at 15:31
  • The precompiled header needs recomplilation. In general this means a full rebuild, since the precompiled header file is included everywhere. – Stephan Dec 12 '11 at 14:52
  • If your compiler doesn't support precompilation this will massively increase compilation -time-. Whether it decreases or increases compilation speed if it does depends entirely on whether or not you ever adjust a header file. If you do, everything will have to be recompiled, which I doubt will improve compilation time. – Clearer Dec 14 '14 at 15:45
2

Dependencies are best served with an include-once in the using source (pragma, guarding defines). That involves a very small overhead as you get a tree of repeated includes: including a header while including a header.

However for the standard libraries are/were sometimes not so well organized and to provide a standard base of headers this was easiest. It also reflects a kind of "module" idea, bundled headers for the basic layer.

As to local includes: it is likely to be non-object-oriented lazyiness, not expressing dependencies.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138