我在查看 github 上的 doom3 代码时发现了一些不寻常的地方。几个文件只有一个包含一个名为 idlib/precompiled.h 的文件,这个文件包含几个其他头文件,如
...
#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>
...
和程序头
#include "../framework/BuildVersion.h"
#include "../framework/BuildDefines.h"
#include "../framework/Licensee.h"
#include "../framework/CmdSystem.h"
#include "../framework/CVarSystem.h"
我想知道是否有什么好的理由,因为这是我第一次看到这样的东西
最佳答案
这称为预编译 header 。主要好处是大大提高了编译速度。
precompiled.h
中的所有头文件每个项目只编译一次。如果没有预编译头文件,每个头文件内容将被编译多次:对于包含它的每个 .cpp 文件。
关于c++ - 将通用文件包含在一个头文件中有什么好处?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8431041/