# Ifndef/# define/# endif usage in c/c ++ header files,
Presumably many people have read "# ifndef/# define/# endif used in the header file to prevent this header file from being repeatedly referenced ". But can you understand the meaning of "being repeatedly referenced? What will happen if the header file is repeatedly referenced? Is it necessary to add # ifndef/# define/# endif code to all header files?
1. In fact, "repeated reference" means that a header file is included multiple times in the same cpp file. This error is often caused by include nesting. For example,. h file # include "c. h "and B. # include ". h "and # include" c. h "this will cause c. h.
2. Consequences of repeated header file references:
(1) Some header files are repeatedly referenced, which only increases the compilation workload and does not cause too many problems. It is only because the compilation efficiency is low, however, compilation efficiency is very important for large projects.
(2) Some header files are repeatedly included, which may cause compilation errors. For example, if a global variable is defined in the header file or the function implementation is written rather than Declaration (although this method is not recommended, but it is indeed allowed by the C Specification). This will lead to repeated definitions.
3. Are these codes included in all header files?
It is not necessary to add it, but in any case, use # ifndef/# define/# endif or other methods to avoid repeated inclusion of header files. The only advantage is that there is no harm. Cultivating a good programming habit is an important branch of learning programming. Therefore, when writing header files, it is best to write the content between # ifndef and # endif.
# Ifndef/# define/# endif usage is given below:
# Ifndef _ XXX_H _ // indicates "if not define _ XXX_H _", that is, XXX is not included. h # define _ XXX_H _/defines _ XXX_H __... // put the code that should have been written in the header file # endif // otherwise, no definition is required.
If XXX is not defined. h, XXX is defined here. h, and then run the content in it. If the file is still included next time, the # ifndef judgment will be performed. Then the content between # ifndef and # endif will not be loaded again.
However, it must be remembered that the Preprocessor still reads the entire header file, even if all the content in this header file will be ignored. This processing will slow down compilation, so if possible, avoid multiple inclusion.
Supplement:
1. # pragma usage
# Pragma once... // put the code that should have been written in the header file here
# Pragma once is a pre-processing instruction. Adding this instruction at the beginning of the header file indicates that this header file is compiled only once and is guaranteed by the compiler that the same file will not be contained multiple times. Note that the "same file" here refers to a physical file, not two files with the same content. The advantage is that you don't have to think about a macro name any more. Of course, there won't be any strange problems caused by the macro name collision.
Summary:
# Ifndef, # define, # endif is a macro definition in C/C ++, which avoids multiple compilation of files. Therefore, it is effective and portable in all compilers that support the C ++ language. Therefore, it is best to use this method if the program to be written is cross-platform. However, macro names cannot conflict with each other.
# Pragma can avoid name conflicts. The disadvantage is that if a header file has multiple copies, this method cannot ensure that they are not repeatedly included. Not all compilers support this method.