简体   繁体   English

将通用文件包含在一个头文件中有什么好处?

[英]What's the benefit of including common files in one single header file?

I was looking at the doom3 code on github and I notice something unusual. 我正在查看github上的doom3代码,我发现了一些不寻常的东西。 Several files have only one include for a file called idlib/precompiled.h and this file have includes for several other headers like 有几个文件只包含一个名为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>
...

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. precompiled.h中的所有头文件每个项目只编​​译一次。 Without precompiled headers, each header content would be compiled multiple times: for each .cpp file it is included in. 如果没有预编译头文件,每个头文件内容将被多次编译:对于每个.cpp文件,它都包含在内。

Dependencies are best served with an include-once in the using source (pragma, guarding defines). 在使用源(pragma,guarding definitions)中使用include-once可以最好地使用依赖关系。 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 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用静态库比包含头文件有什么好处? - What is the benefit to using a static library over including a header file? 没有实现文件时包括头文件吗? - Including header files when there's no implementation file? 包含标头和C ++文件有什么区别? - What's the difference between including a header and a C++ file? 在任何头文件中只定义一个类而不包括包含这样一个类的整个头文件的目的是什么? - What is the purpose of defining just one class in any header file and not including the entire header file containing such a class? 包括具有一个头文件和单独源文件的特定于平台的库 - Including platform-specific libraries with one header file and separate source files 消除在客户端的函数声明头文件中使用C ++类头文件时的方法 - Ways of eliminating including C++ class header files when they was used in the client's function declaration head file 将头文件包含到头文件中,而不将其内容暴露给includer - Including a header file into a header file without exposing it's content to the includer 在 C++ 的多个文件中包含单头库 - Including single-header-library in multiple files in C++ 包括头文件(包括它们自己) - Including header files (including themselves) 多个源文件和头文件访问公共头文件 - Multiple source and header files accessing a common header file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM