#ifndef headername_h #define HEADERNAME_H #endif use

Source: Internet
Author: User

Presumably a lot of people have seen it in the papers:#ifndef headername_h

#define Headername_h

This is usually written in a variety of macro definitions, the inclusion of other header files

#endif

The purpose of this is to prevent the header file from being repeatedly referenced .

What do you mean, "header file is repeatedly referenced"?

Answer: actually "Repeated references" refers to the fact that a header file is contained multiple times in the same CPP file, often due to the include caused by nested nesting.

For example: There is a.h file # include "C.h", and B.cpp file also include "A.h" and "# include" C.h ", this will cause c.h to be b.cpp repeated references.

Consequences of repeated references to header files:

Some header file re- references only increase the workload of the compilation work, does not cause too much problem, only is the compilation efficiency is low. But for big projects, it would be a painful task to compile inefficient.

While some header files are included repeatedly, this 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), and this header file repeatedly contains duplicate definitions that cause global variables .

Are all header files to be added to #ifndef/#define/#endif these codes?

Answer: Not necessarily add, but it is best to add! This is because: anyway, with ifnde xxx #define XXX #endif, only the benefits, no harm.

Also, individuals feel that developing a good programming habit is an important aspect of learning programming.

Here is a #ifndef/#define/#endif的格式:

First write: #ifndef a_h (meaning: "If not define A.H", that is, if A.H is not present)

Then write: #define A_H (then introduce A.H)

Last write: #endif (otherwise no need to introduce a.h, i.e. there is already a a.h, no need to introduce again)

Cases:
#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 headername_h #define HEADERNAME_H #endif use

Related Article

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.