Вопрос

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

Это было полезно?

Решение

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.

Другие советы

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.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top