Avoid duplicate definitions of variables and repeat the method of including header files

Source: Internet
Author: User
The effect of repeated inclusion: in the preprocessing pair, include the same file, the preprocessor will check if XXX has a definition and then decide whether to copy the content, duplicate contains will be the compiler check more than a few times. In addition, when using incremental compilation, this file changes, all the files that include this file need to be recompiled, even if you do not use any of the contents.
Avoidance methods:
1. Put the head file in the macro:
#ifndef logo (This logo can be defined as a random, but in order to prevent confusion, so generally will use their own file name: __wenjian_h__)
#define Logo
File contents
#endif

Before the header file definition is added
#pragma once (not very versatile)
You can prevent a header file from being included multiple times, preventing duplicate-defined errors.


In the header file generated with the VC6.0 Wizard, you can often see the following code snippet:

#if!defined (Afx_resizablelayout_h__included_)
#define Afx_resizablelayout_h__included_

#if _msc_ver > 1000
#pragma once
#endif//_msc_ver > 1000

...

#endif//!defined (AFX_RESIZABLELAYOUT_H__INCLUDED_)

Macro have a basic understanding of the friends should know that the header file in the following macro definition, is to avoid the same header file in the same. c files or. cpp files are included more than once.

#if!defined (XXX)
#define XXX

#endif


It's good to understand, but the next paragraph, especially #pragma once, is not very clear to me. The explanation for getting pragma once from MSDN is:

"Specifies that the file would be included (opened) is once by Thecompiler when compiling a source code file."

The main idea of the English note is also that #pragma once is to avoid duplicate file inclusion. Doubts about this arose, since the macro "#if!defined" already has this role, why should a "#pragma once"? I then searched the Internet for several answers, but everyone's answer is very vague, so I want to give up, no longer think about this problem, but still not very reconciled, then read the explanation of others. Suddenly, it seems to be a bit more enlightened. Although "#if!define" and "#pragma once" have features that avoid duplication, there are differences in implementation. One example is as follows:

Test1.h
#if!define (__testone_h_)
#define __testone_h_
...
#endif

Test2.h
#pragma once
...

Test.cpp
#include "Test1.h"//Line 1
#include "Test1.h"//Line 2
#include "Test2.h"//Line 3
#include "Test2.h"//Line 4

...

Header file Test1.h use macros to avoid duplication, Test2.h in the header file with #pragma once to avoid duplication. Compile Test.cpp, will need to open Test1.h two times, the first time found that the macro __testone_h_ no definition, then the macro definition, and then the second time you open Test1.h, found that the macro __testone_h_ has been defined, the compiler will skip the macro definition section, Know how to handle the #endif at the end of Test1.h.

Because the header file Test2.h uses #pragma once to avoid duplication of definitions, Test2.h will only be opened once during the compilation of Test.cpp, that is, when processing to line 3rd. Because Test2.h is using #pragma once, after finishing line 3rd, the compiler already knows that it contains a Test2.h, and when it (the compiler) processes the 4th line of code, it finds that Test2.h is already included, ignoring the 4th line of code, There is no need to open Test2.h again to judge.

Summing up, in addition to #pragma once is unique to the Microsoft compiler, with the macro and #pragma once approach to avoid repeating the header file, the main difference is that the method of macro processing will open the same file multiple times, and #pragma once will not be opened repeatedly, so # Pragma once can be faster.

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.