#ifndef, #define, #endif role

The main purpose of the conditional indicator #ifndef is to prevent repeated inclusion and compilation of header files

Refer to the following two links:

In fact, "repeated reference" means that a header file is included multiple times in the same cpp file. This kind of error is often caused by include nesting. For example: there is an ah file #include "c.h"and the b.cpp file is imported at this time #include "a.h", and #include "c.h"this will cause repeated references to ch.


A few important questions:

  • #ifndefAre there any requirements for the following content?

In fact, you can add anything after #ifndef , as long as its uniqueness is guaranteed, and the file name is just for easy distinction . It is only used to define a preprocessor variable.


  • #ifndefWhat if the content behind the two header files is the same?

The preprocessing block is generated through the "#ifndef/#define/#endif" structure. Although it can avoid the same header file being included and repeatedly referenced multiple times, there is also a fatal shortcoming, that is, once it is accidentally included in different header files If the same macro name is defined , the problem is more troublesome. For example, it may lead to problems such as clearly seeing that there is a header file, but the compiler insists that the declaration cannot be found. In order to avoid this situation and ensure the uniqueness of the macro name, it is recommended that the header file be named based on the full path of the source code tree of the project as suggested by Google . The naming format is:
<PROJECT>_<PATH>_<FILE>_H_
among them, PROJECT represents the project name, PATH represents the relative path of the header file, FILE represents the file name, and then " H " as the suffix. For example, in the project CashRegister, there is a parser header file under a subfolder named xml in the directory where the project is located, and the macro definition is as follows:

#ifndef CASHREGISTER_XML_PARSER_H_
#define CASHREGISTER_XML_PARSER_H_
/*声明、定义语句*/
#endif

Refer to self -conditional compilation instructions (symbols), a complete guide to C language conditional compilation instructions


  • Is it necessary to join in all header files #ifndef, #define, #endifthese codes?

It is not necessary to add, but in any case, use #ifnde xxx #define xxx #endif or other methods to avoid repeated inclusion of header files, there are only benefits and no harm . I personally feel that cultivating a good programming habit is an important branch of learning programming.


  • Why #ifndefare the following content all capitalized?

Because #ifndefit is essentially a macro definition, it is customary to capitalize.


  • Sometimes it can be written as #ifndef _HEADER_H_, how to understand the underscore?

The underscore "__" belongs to the content of programming style and has no effect on the program. You can also use underscores without underscores, and it is up to personal habits to use several underscores; it can even be written #ifndef HEADER.H, because the string after #ifndef, whether it is HEADER.H or __HEADER_H, has nothing to do with the name of the header file. It's just for the convenience of reading the program. In fact, as long as the string is legal .
Note: The functions at the beginning _and __above are generally dedicated functions, which are generally related to a specific system. If you want to have better portability, you should avoid using them. Generally, only system library functions and macros that have been widely used are eligible to start with _ or even __, in order not to conflict with user-defined names, so B. Stroustup warned us not to use _ or in "The C++ Programming Language" The identifier at the beginning of __, this is also a question of programming style.

Guess you like

Origin blog.csdn.net/lyh458/article/details/114992623