为什么在 C++ 头文件中使用 #ifndef 和 #define?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1653958/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why are #ifndef and #define used in C++ header files?
提问by Asad Khan
I have been seeing code like this usually in the start of header files:
我经常在头文件的开头看到这样的代码:
#ifndef HEADERFILE_H
#define HEADERFILE_H
And at the end of the file is
在文件的末尾是
#endif
What is the purpose of this?
这样做的目的是什么?
回答by LiraNuna
Those are called #include guards.
这些被称为#include 警卫。
Once the header is included, it checks if a unique value (in this case HEADERFILE_H
) is defined. Then if it's not defined, it defines it and continues to the rest of the page.
一旦包含标头,它就会检查是否定义了唯一值(在本例中为HEADERFILE_H
)。然后,如果它没有定义,它会定义它并继续到页面的其余部分。
When the code is included again, the first ifndef
fails, resulting in a blank file.
当再次包含代码时,第一个ifndef
失败,导致一个空白文件。
That prevents double declaration of any identifiers such as types, enums and static variables.
这可以防止双重声明任何标识符,例如类型、枚举和静态变量。
回答by Roy
#ifndef <token>
/* code */
#else
/* code to include if the token is defined */
#endif
#ifndef
checks whether the given token has been #defined
earlier in the file or in an included file; if not, it includes the code between it and the closing #else
or, if no #else
is present, #endif
statement. #ifndef
is often used to make header files idempotent by defining a token once the file has been included and checking that the token was not set at the top of that file.
#ifndef
检查给定的令牌是#defined
在文件中还是在包含的文件中更早;如果没有,它包括它和结束之间的代码,#else
或者,如果不#else
存在,则包括#endif
语句。#ifndef
通常用于通过在包含文件后定义标记并检查标记未设置在该文件的顶部来使头文件具有幂等性。
#ifndef _INCL_GUARD
#define _INCL_GUARD
#endif
回答by Sandeep_black
This prevent from the multiple inclusion of same header file multiple time.
这可以防止多次包含相同的头文件。
#ifndef __COMMON_H__
#define __COMMON_H__
//header file content
#endif
Suppose you have included this header file in multiple files. So first time __COMMON_H__ is not defined, it will get defined and header file included.
假设您已将这个头文件包含在多个文件中。所以第一次 __COMMON_H__ 没有定义,它将被定义并包含头文件。
Next time __COMMON_H__ is defined, so it will not include again.
下次定义 __COMMON_H__ 时,将不再包含。
回答by Mohit Jain
They are called ifdef or include guards.
它们被称为 ifdef 或包含守卫。
If writing a small program it might seems that it is not needed, but as the project grows you could intentionally or unintentionally include one file many times, which can result in compilation warning like variable already declared.
如果编写一个小程序,似乎不需要它,但随着项目的增长,您可能有意或无意地多次包含一个文件,这可能会导致编译警告,如已声明的变量。
#ifndef checks whether HEADERFILE_H is not declared.
#define will declare HEADERFILE_H once #ifndef generates true.
#endif is to know the scope of #ifndef i.e end of #ifndef
If it is not declared which means #ifndef generates true then only the part between #ifndef and #endif executed otherwise not. This will prevent from again declaring the identifiers, enums, structure, etc...
如果它没有被声明,这意味着#ifndef 生成真,那么只有#ifndef 和#endif 之间的部分被执行,否则不执行。这将防止再次声明标识符、枚举、结构等......