A brief talk on #ifndef/#define/#endif的用法 in C + + header file

Source: Internet
Author: User

Many people have seen " #ifndef/#define/#endif used in the header file to prevent the header file from being repeatedly referenced." But what does it mean to understand "repeated references"? What happens if the header file is repeatedly referenced? are #ifndef/#define/#endif These codes included in all header files ?

1   actually" repeated references "means a header file in the same cpp include include nested. such as: The existence of a.h file #include   "c.h" b.cpp file imported #include   "A.H"   and #include  " C.h " repeat contains.

2 . Consequences of repeated references to header files:

(1) Some header file repeated reference, just increase the workload of the compilation work, will not cause too much problem, just the compilation efficiency is low, but for large projects, compile efficiency is very important.

(2) Some header files are duplicated, which can cause compilation errors, such as defining a global variable in a header file or writing the implementation of a function rather than a declaration ( although this method is not recommended, but is actually allowed by the C specification ), This can cause duplicate definitions.

3, is not all the header files to add this code?

Not necessarily, but in any case, the #ifndef/#define/#endif or other way to avoid the duplication of header files, only the benefits of no harm. Developing a good programming habit is an important branch of learning programming. so when writing a header file, it's best to write the content between #ifndef and #endif .

Here's how to use the #ifndef/#define/#endif:

#ifndef __xxx_h__    // means  "If not define __XXX_H__" which is not included XXX.h     #define __xxx_h __   // define __xxx_h__   ... // the code that should have been written in the header file here #endif       //

if XXX.h is not defined the definition here is XXX.h , and then run the content inside, if the next time you go to this file, then #ifndef 's judgment, then content between the #ifndef与 #endif is not loaded again

However, it must be remembered that the preprocessor still reads the entire header file, even if all the contents of the header file are ignored. Because this processing slows down the compilation speed, you should avoid multiple inclusions if possible.

Add:

1.#pragma的用法

#pragma once ...        // the code that should have been written in the header file here

#pragma once is a preprocessing directive that joins this instruction at the beginning of the header file: This header file is compiled only once and is guaranteed by the compiler: the same file will not be included multiple times. Note that the "same file" here refers to a physical file, not two files of the same content. The advantage is that you don't have to bother thinking about a macro name, and of course there's no strange problem with the macro name collision.

Summary :

#ifndef,#define,#endif is a macro definition in the C + + language that avoids multiple compilations of files through macro definitions. So in all support C + + language compiler is effective, good portability, so if you write a program to cross-platform, it is best to use this way. But the disadvantage is that the macro name does not conflict.

#pragma can avoid name collisions , the disadvantage is that if a header file has multiple copies, this method does not guarantee that they will not be included repeatedly. And not all compilers support this approach.

A brief talk on #ifndef/#define/#endif的用法 in C + + header file

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.