C-Language header files avoid duplicate inclusions, #pragma once and #ifndef differences __c language

Source: Internet
Author: User

In general, we write function declarations, class definitions, template definitions, and so on in a header file, where necessary, the corresponding header file is included with #include in the source file (*.cpp file). But the header file also allows other headers to be included, which makes it unavoidable that a header file is repeatedly included. We can use compilation preprocessing commands to prevent this from happening.

For example, if you want to make sure that the header file max.h is not duplicated, you can take the following form:

The first preprocessing command is that if the maxmin_h is not true, this file is not included, the source code behind this command is valid (equivalent: ' If the door is not closed, please come in ');

The second preprocessing command puts the Maxmin_h to true (the equivalent of asking you to plug in the door and not let the second person in).

The last preprocessing command is to mark the scope of the source program that accepts the above processing (the equivalent of you have come to the back door).

Assume that there are several header files and their containing relationships as follows:

File1.h,file2.h,file3.h,file4.h,file5.h,main.cpp

Then: File3.h contains file1.h,file2.h,file4.h containing file1.h,file2.h,file5.h containing file3.h,file4.h. Will cause the file1 and file2 to be included repeatedly in the File5, and the error will be caused when compiling.

  Workaround 1:

1: Application #ifndef

#define

#endif

That is, each file is defined in the following situations (take File1.h as an example):

#ifndef H_file1

#define H_file1

#include <stdio.h>

#include <math.h>

.....

#endif

File3.h:

#ifndef H_file3

#define H_file3

#include <stdio.h>

#include <math.h>

#inlcude "File1.h"

#include "File2.h"

.....

#endif

  method Two: The head definition of each file: #pragma once (used to interpret the contents of this file only once)

Example: Fiel1.h:

#pragma once

#include <stdio.h>

#include <math.h>

.....

File3.h:

#pragma once

#include <stdio.h>

#include <math.h>

#include "File1.h"

.....

2, #pragma the difference between once and #ifndef

This is a more commonly used instruction, as long as the beginning of the first file to add this command to ensure that the header file is compiled once

1) #pragma once is used to prevent a head file from being include multiple times,

#pragma once is compile-related, which means it can be used on this compilation system, but not necessarily in other compiling systems, that is, poor portability, but now it is basically the definition of each compiler.

2) #ifndef, #define, #endif用来防止某个宏被多次定义.

#ifndef, #define, #endif这个是C + + language-related, which is a macro definition in the C + + language, to avoid multiple compilations of files through a macro definition. So it works on all compilers that support C + + languages, and if you're writing a program that's cross-platform, it's best to use this approach


1 #ifndef方式 and 2 #pragma once way

There is not much difference between the two compilers that can support both, but there are still subtle differences.

Mode one:

#ifndef __somefile_h__

#define __somefile_h__

..//Some statements

#endif

Mode two:

#pragma once

..//Some statements

1 #ifndef的方式依赖于宏名字不能冲突, this will not only ensure that the same file will not be included multiple times, but also ensure that the content of the same two files will not be inadvertently included. Of course, the disadvantage is that if the different headers of the macro name accidentally "Crash", may lead to the existence of the header file, but the compiler insists that can not find the state of the statement

2 #pragma once is guaranteed by the compiler: the same file will not be included multiple times. Note that the word "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 to think of a macro name, of course, there will be no macro name collision caused by strange problems. The disadvantage is that if a header file has multiple copies, this method does not guarantee that they will not be included repeatedly. Of course, the repeated inclusion is more likely to be found and corrected than the "Can't find declaration" problem caused by the macro name collision.

3 Mode one is supported by the language so the transplant is good, the way two can avoid the name conflict

#pragma once method is produced after #ifndef, so many people may not even have heard of it. At present, it seems #ifndef more respected. Because #ifndef is naturally supported by language and is not limited by the compiler, the #pragma once approach is not supported by older versions of the compiler, in other words, it's not good enough for compatibility. Maybe, in a few years, when the old compiler dies, it's not a problem.




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.