简体   繁体   English

如何在 C++ 的 header 文件中确定适当的条件包含标识符 (#ifndef)?

[英]How to determine the appropriate identifier of conditional inclusion (#ifndef) in header files for C++?

I was using an IDE that gave me the following line when I created a new HeaderFile.h当我创建一个新的 HeaderFile.h 时,我使用的是 IDE 它给了我以下行

#ifndef CODE_HEADERFILE_H

but I've also seen usages such as:但我也看到了以下用法:

#ifndef _HeaderFile_
#ifndef HEADERFILE_H

is the label completely arbitrary given that it will be called if the header file is loaded multiple times?如果 header 文件被多次加载,label 是完全任意的吗? And what would happen if another different header file had the same identifier and both where included somewhere?如果另一个不同的 header 文件具有相同的标识符并且两者都包含在某个地方会发生什么?

Include guard define naming is personal, but conventionally it's in the form of包括守卫定义命名是个人的,但通常它的形式是

HEADER_FILE_NAME_H

But you can pick whatever name you'd like as long as it doesnt clash with other include guards otherwise only 1 file will end up getting included.但是你可以选择任何你想要的名字,只要它不与其他包含保护冲突,否则只有 1 个文件最终会被包含。

There's also #pragma once , although it's not supported by the standard, all major compilers support it.还有#pragma once ,虽然标准不支持它,但所有主要编译器都支持它。 This eliminates the need for include guards all together.这消除了一起包含防护的需要。

How to determine the appropriate identifier of conditional inclusion (#ifdef) in header files for C++?如何在 C++ 的 header 文件中确定适当的条件包含标识符 (#ifdef)?

In the exactly same way as you would determine the name of any variable, macro, function or a type:与确定任何变量、宏、function 或类型的名称完全相同的方式:

  1. Firstly, consult the language rules on what identifiers are reserved to the language implementation, and do not use any of those identifiers.首先,请查阅语言规则,了解为语言实现保留哪些标识符,不要使用任何这些标识符。

     #ifndef _HeaderFile_

    This is an example of a reserved identifier.这是保留标识符的示例。 Do not define it unless you are implementing the standard library.除非您正在实现标准库,否则不要定义它。

  2. Next, rule out any identifier used for any other purpose in the program.接下来,排除在程序中用于任何其他目的的任何标识符。 If you have variable named x , then do not use that as a header guard.如果您有名为x的变量,则不要将其用作 header 保护。 If you have one header guard named y , then do not use that as another header guard.如果您有一个名为y的 header 防护装置,则不要将其用作另一个 header 防护装置。 If you have a function named XYZ_H , then do not use that as a header guard.如果您有一个名为XYZ_H的 function ,则不要将其用作 header 防护装置。

  3. Now, choose any identifier from the remaining names.现在,从其余名称中选择任何标识符。 There should be plenty to choose from.应该有很多可供选择的。

is the label completely arbitrary label 是完全任意的

Only as much as any other variable, macro, function or type name is arbitrary.只有任何其他变量、宏、function 或类型名称是任意的。 You simply need to come up with a unique name.您只需要想出一个唯一的名称。

You don't however need to refer to the header guard macro in any other context, so you don't need to worry about it being meaningful unlike most other names in the program.但是,您无需在任何其他上下文中引用 header 保护宏,因此您无需担心它与程序中的大多数其他名称不同。

And what would happen if another different header file had the same identifier and both where included somewhere?如果另一个不同的 header 文件具有相同的标识符并且两者都包含在某个地方会发生什么?

What happens to ifndef FOO when FOO is defined?定义 FOO 时ifndef FOO FOO发生什么? The pre-processor will remove everything after the directive until the next #endif directive (or #else , but that is rare with header guards).预处理器将删除指令之后的所有内容,直到下一个#endif指令(或#else ,但这在 header 防护装置中很少见)。 This is how header guards prevent multiple inclusions into same file: Subsequent inclusions are made empty by the pre-processor directive.这就是 header 防护如何防止多个包含在同一个文件中:预处理器指令将后续包含设为空。 In case multiple headers share the same guard, the one that is included second will be empty.如果多个标头共享同一个保护,则第二个包含的标头将为空。 This will probably be a problem.这可能会是一个问题。

maybe, It all depends on you and your company or your group's code style.也许,这完全取决于您和您的公司或您的团队的代码风格。 When I write #ifdef or #ifndef, usually, I write the statement like that below: 1) #ifndef HEADERFILE_H 2) #ifndef HEADER_FILE_H 3) #ifndef PATH_TO_YOUR_HEADERFILE_HEADERFILENAME_H当我写#ifdef 或#ifndef 时,通常我会写如下语句: 1) #ifndef HEADERFILE_H 2) #ifndef HEADER_FILE_H 3) #ifndef PATH_TO_YOUR_HEADERFILE_HEADERFILENAME_H

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM