it must have been seen by many people. #ifndef/#define/#endif in the header file prevents the header file from being repeatedly referenced. But what does it mean to understand "repeated references"? Is it possible to include this header file in a different two files using include? What happens if the header file is repeatedly referenced? Are all header files to be added to #ifndef/#define/#endif these codes?
In fact, "repeated reference" refers to a header file in the same CPP file is include multiple times, this error is often caused by the include nested. For example: There is a a.h file # include "C.h" and the B.cpp file imports the # include "A.h" and the # include "C.h" will cause c.h Duplicate references at this time.
Consequences of repeated references to header files:
Some header file re- references just increase the workload of the compilation work, do not cause too much problem, just a little bit less compile efficiency, but for the big project, compiling inefficient that would be a painful thing.
Some header files are repeatedly included, which can cause errors, such as defining global variables in the header file (although this is not recommended, but it is true that the C specification allows it) to cause duplicate definitions.
Are all header files to be added to #ifndef/#define/#endif these codes?
Answer: Not necessarily add, but in any case, with Ifnde XXX #define XXX#ENDIF or other ways to avoid the header file duplication, only the benefits of no harm. Individuals feel that developing a good programming habit is an important branch of learning programming.
Here is a #ifndef/#define/#endif的格式:
#ifndef a_h means "if not define A.H" if it does not exist A.H
The next statement should be a # define a_h to introduce A.H
The last sentence should be written #endif otherwise no need to introduce
--------------------------------------------------------------------------------------------------
#ifndef Graphics_h//Prevent Graphics.h from being repeatedly referenced
#define Graphics_h
#include <math.h>//header file referencing the standard library
...
#include "header.h"//header file referencing non-standard libraries
...
void Function1 (...); global function declaration
...
Class Box//struct declaration
{
...
};
#endif
--------------------------------------------------------------------------------------------------
#ifndef #define #endif Prevent the header file from being repeatedly referenced