#ifndef #define #endif Prevent the header file from being repeatedly referenced

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.