C + + preprocessing and header file protectors

Source: Internet
Author: User

Pre-treatment 1. Common preprocessing functions The primary function of preprocessor is to replace a resource with the built-in function of preprocessing, the most common preprocessing is: file contains, conditional compilation, layout control and macro substitution 4 kinds.
file contains: #include is one of the most common preprocessing, mainly as a reference to the file combination source program body.
conditional Compilation: #if, #ifndef, #ifdef, #endif, #undef等也是比较常见的预处理, mainly compile-time selective selection, comment out some of the specified code to achieve version control, to prevent duplication of the file containing functionality.
Layout Control: #pragma, this is also an important aspect of our application preprocessing, the main function is to provide the compiler with unconventional control flow information.
macro Substitution: #define, this is the most common use, it can define symbolic constants, function functions, renaming, string concatenation and other functions.
2. Common preprocessing directives: #define Macro Definitions
#undef undefined macros
#include text contains
#ifdef Compile if the macro is defined
#ifndef Compile if the macro is not defined
#endif End control of the compilation block
Compiling code #if expression nonzero
#else compiled as the remaining option for other preprocessing
#elif This is a combination of #else and # If options//examples later
#line change the current number of rows and file names
#error Output an error message
#pragma provide unconventional control flow information for the compiler
3. Preprocessing identifiers in order to handle some useful information, preprocessing defines some preprocessing identifiers, although the preprocessor identifiers of various compilers are different, but they will handle the following 4 types:
__FILE__ the name of the file being compiled
__line__ the line number of the file being compiled
__date__ date string for compile time, for example: "Jan 2006"
__time__ time string for compile time, for example: "12:30:55"
For example:
cout<<"Thefile is:"<<__file__"<<" is :"

Second, the head file protector

1. Conditional compilation

[HTML]View Plaincopy
#ifdef XXX   // or #ifndef  //...   (#else)  // ...   .. #endif  

For example:

#ifdef WINDOWS    // ....    // ....    #endif   #ifdef LINUX    // ....    // ....    #endif

2. Writing header file Protectors

The header file should contain a protector, even if the header files are not contained by other header files. Writing a header file protector is not difficult, and if the header file is included more than once, it avoids the hard-to-understand compilation errors.

The use of macro definitions and conditional compilation #ifndef indicates that the specified preprocessing variable is not defined. If the preprocessor variable is undefined, all subsequent instructions are processed until the #endif appears.
You can use these measures to prevent the inclusion of the same file multiple times:

/* * * Header file salesitem.h * * */   #ifndef sasesitem_h  #define salesitem_h  //... Here is the  content #endif  

The condition indicates that the #ifndef salesitem_h test salesitem_h preprocessor variable is undefined. If Salesitem_h is undefined, #ifndef test succeeds, and all rows following the #ifndef are executed until the #endif is found. Conversely, if salesitem_h is defined, then #ifndef indicates that the test is false, and that the code between the indicator and the #endif indicator is ignored.

To ensure that header files are processed only once in a given source file, we first detect #ifndef. The first time the header file is processed, the test succeeds because Salesitem_h is not yet defined. The next statement defines the Salesitem_h. That way, if we compile a file that contains the header file exactly once again. #ifndef indicates that Salesitem_h has been defined and ignores the remainder of the header file.

This strategy is quite effective when there are no two header files that define and use preprocessor constants with the same name. We can name preprocessor variables for entities defined in the header file (such as classes) to avoid the duplicate names of preprocessor variables. A program can contain only one class named Sales_item. By using the class name to make up the name of the header file and the preprocessor variable, it is possible that only one file will use the preprocessor variable.

Header File Protection method two--------------------------------------------------------------------------------------

#pragma once

This is a more commonly used instruction, as long as the header file at the beginning of the addition of this command will ensure that the header file is compiled once

#pragma once is used to prevent a header file from being multiple include, #ifndef, #define, #endif用来防止某个宏被多次定义.

#pragma once is a compilation-related, that is, this compilation system can be used, but in other compiled systems may not be, that is, the transplant is poor, but now basically every compiler has this definition.

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

Third, Comparison

#pragma once and #ifndef的区别

To avoid the same file being include multiple times

1 #ifndef方式
2 #pragma once way

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

#ifndef __somefile_h__ #define __somefile_h__//  Some declaration statements #endif

Way two:

#pragma once//  Some statement statements

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

#pragma once is guaranteed by the compiler: the same file will not be included more than once. 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. 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, duplicate inclusions are more susceptible to discovery and remediation than the "no claims" issue caused by macro name collisions.

The way one is supported by the language so the portability is good, the way two can avoid name collisions

C + + preprocessing and header file protectors

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.