在 C 和 C++ 中,为什么每个 .h 文件通常都用 #ifndef #define #endif 指令包围?
为什么每个 .h 文件都以 #ifndef #define #endif 开头?我们当然可以在没有这些指令的情况下编译程序。
Why does each .h file starts with #ifndef #define #endif? We can certainly compile the program without those directives.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这就是所谓的“包含守卫”。目的是防止文件被多次包含时必须被解析多次。
It's a so-called "include guard". The purpose is to prevent the file from having to be parsed multiple times if it is included multiple times.
它可以防止单个文件的多次包含。可以使用指令完成相同的操作
,但这些 #ifndef 是标准的,因此每个编译器都支持。
It prevents multiple inclusions of a single file. The same can be done using
directive, but those #ifndefs are standard thus supported by every compiler.
它被称为包含守卫。您可以在没有它们的情况下进行编写,直到您开始编写大型程序并发现您需要多次直接或间接从 .c 文件包含相同的 .h 文件。如果没有包含保护,您将得到多个定义错误,但是有了它们,头文件内容仅被解析一次并跳过所有后续时间,从而避免了这些错误。始终使用它们是一个好习惯。
It is called an include guard. You can write without them until you start writing large programs and find out that you need to include the same .h file more than once, directly or indirectly, from a .c file. Then without include guards you would get multiple definition errors, but with them, the header file contents are parsed only once and skipped all the subsequent times, avoiding those errors. It`s a good practice to always use them.
如果我理解正确,您想知道在没有包含防护的情况下,多次包含头文件是否会导致错误或危险行为。这是在排除多个定义等之后。
想象一下一个恶意程序员,其头文件没有包含保护。他的头文件定义了一个宏,
SZ
,这是一个用于静态分配数组的大小。程序员可以这样编写他的头文件:现在,如果包含该头文件一次,则
SZ
等于 1024。如果包含两次,SZ
将变为 128当然,大多数现实世界的程序员都没有恶意,没有人真正写出像上面这样的代码。请注意,C 标准允许
assert.h
多次#include
并具有不同的行为,具体取决于当时是否定义了NDEBUG
包含assert.h
。因此,assert.h 不能有包含防护。不过,这是一个功能,而不是一个错误。If I understand correctly, you want to know if, in the absence of include guards, can including a header file multiple times cause an error or dangerous behavior. This is after excluding multiple definitions, etc.
Imagine a malicious programmer, whose header file doesn't have the include guards. His header file defines one macro,
SZ
, which is a size that you use for your statically allocated arrays. The programmer could write his header file like this:Now, if you include the header file once, you get
SZ
equal to 1024. If you include it twice,SZ
becomes 128. Of course, most real-world programmers are not malicious, and no one really writes code like above.Note that the C standard allows
assert.h
to be#include
d more than once with different behavior depending upon whetherNDEBUG
is defined at the time of inclusion ofassert.h
. So,assert.h
can't have include guards. That is a feature, not a bug, though.如果头文件包含如下定义
than, being included several times without a guard, will produce a compilation error.
ifndef checks that some preprocessor variable is not defined (and it is not, for a first time), then defines it explicitly to avoid being captured again.
In MSVC you can also use
instead of
ifndef
's.If a header file contains definition like
than, being included several times without a guard, will produce a compilation error.
ifndef checks that some preprocessor variable is not defined (and it is not, for a first time), then defines it explicitly to avoid being captured again.
In MSVC you can also use
instead of
ifndef
's.