在 C++ 中使用双包含守卫

2022-01-11 00:00:00 macros linker c++

所以我最近在我工作的地方进行了一次讨论,其中我质疑使用 双重 包含防护而不是单一防护.我所说的双重保护的意思如下:

So I recently had a discussion where I work, in which I was questioning the use of a double include guard over a single guard. What I mean by double guard is as follows:

头文件,header_a.hpp":

#ifndef __HEADER_A_HPP__
#define __HEADER_A_HPP__
...
...
#endif

当在任何地方包含头文件时,无论是在头文件中还是在源文件中:

When including the header file anywhere, either in a header or source file:

#ifndef __HEADER_A_HPP__
#include "header_a.hpp"
#endif

现在我明白在头文件中使用保护是为了防止多次包含已定义的头文件,这是常见的并且有据可查.如果宏已定义,则编译器会将整个头文件视为空白",并防止双重包含.很简单.

Now I understand that the use of the guard in header files is to prevent multiple inclusion of an already defined header file, it's common and well documented. If the macro is already defined, the entire header file is seen as 'blank' by the compiler and the double inclusion is prevented. Simple enough.

我不明白的问题是在 #include "header_a.hpp" 周围使用 #ifndef __HEADER_A_HPP__#endif.同事告诉我,这为夹杂物增加了第二层保护,但我看不出如果第一层绝对可以完成这项工作(或者确实可以?),那么第二层是如何有用的.

The issue I don't understand is using #ifndef __HEADER_A_HPP__ and #endif around the #include "header_a.hpp". I'm told by the coworker that this adds a second layer of protection to inclusions but I fail to see how that second layer is even useful if the first layer absolutely does the job (or does it?).

我能想到的唯一好处是它完全阻止了链接器查找文件的麻烦.这是为了缩短编译时间(没有提到它的好处),还是我没有看到这里有其他工作?

The only benefit I can come up with is that it outright stops the linker from bothering to find the file. Is this meant to improve compilation time (which was not mentioned as a benefit), or is there something else at work here that I am not seeing?

推荐答案

我很确定添加另一个包含保护是一种不好的做法,例如:

I am pretty sure that it is a bad practice to add another include guard like:

#ifndef __HEADER_A_HPP__
#include "header_a.hpp"
#endif

以下是一些原因:

  1. 为了避免双重包含,在头文件本身内添加一个通常的包含保护就足够了.它做得很好.在包含位置的另一个包含保护只会弄乱代码并降低可读性.

  1. To avoid double inclusion it is enough to add a usual include guard inside the header file itself. It does the job well. Another include guard in the place of inclusion just messes the code and reduces readability.

它增加了不必要的依赖.如果您在头文件中更改包含保护,则必须在包含头文件的所有位置进行更改.

It adds unnecessary dependencies. If you change include guard inside the header file you have to change it in all places where the header is included.

与整个编译/链接过程相比,这绝对不是最昂贵的操作,因此几乎无法减少总构建时间.

It is definitely not the most expensive operation comparing the whole compilation/linkage process so it can hardly reduce the total build time.

任何有价值的编译器已经优化了文件范围的include-guards.

相关文章