Understanding pre-compiled Headers

Source: Internet
Author: User
Tags bit set
Header file pre-compilation refers to some of the MFC standard header files used in a project (such as Windows. h. afxwin. h) pre-compilation. Later, this header file will not be compiled and only the pre-compilation results will be used. This will speed up compilation and save time.

I. Pre-compiled header file description

Header file pre-compilation refers to some of the MFC standard header files used in a project (such as Windows. h. afxwin. h) pre-compilation. Later, this header file will not be compiled and only the pre-compilation results will be used. This will speed up compilation and save time.
The precompiled header file is generated by compiling stdafx. cpp and named after the project name. Because the precompiled header file is suffixed with "PCH", the compilation result file is projectname. PCH.
The compiler uses the pre-compiled header file through a header file stdafx. h. The stdafx. h header file name can be specified in the project compilation settings. The compiler considers that allCodeAll are pre-compiled. It skips the # include "stdafx. H" command and uses projectname. PCH to compile all the code after this command.
Therefore, the first statement of all CPP implementation files is: # include "stdafx. H ".
In addition, every implementation file CPP contains the following statement:
# Ifdef _ debug
# UNDEF this_file
Static char this_file [] = _ file __;
# Endif
This indicates that if a debug version is generated, it indicates the name of the current file. _ File _ is a macro, which is assigned the name of the file being compiled during compiler compilation.
VC. by default, the pre-compiled header (/YU) is used. there is always a fatal error c1010 during post-Compilation of the H file: An Unexpected File end error occurs when you look for the pre-compilation header command. The solution is to add # include "stdafx. H ", or type project properties, find the" C/C ++ "folder, and click the" pre-compilation Header "property page. Modify the "Create/use precompiled header" attribute to "do not use precompiled header ".

Ii. Overview of C/C ++ header files

C. Traditional C ++
# Include <assert. h> // set the insertion point
# Include <ctype. h> // Character Processing
# Include <errno. h> // defines error codes.
# Include <float. h> // floating point number processing
# Include <fstream. h> // file input/output
# Include <iomanip. h> // parameterized input/output
# Include <iostream. h> // Data Stream Input/Output
# Include <limits. h> // defines the maximum constants of various data types.
# Include <locale. h> // defines the localization function
# Include <math. h> // defines mathematical functions.
# Include <stdio. h> // define Input/Output Functions
# Include <stdlib. h> // defines miscellaneous functions and memory allocation functions.
# Include <string. h> // string processing
# Include <strstrea. h> // array-based input/output
# Include <time. h> // defines the time function.
# Include <wchar. h> // wide character processing and input/output
# Include <wctype. h> // wide character Classification

Standard C ++ (same as above)
# Include <algorithm> // General STL Algorithm
# Include <bitset> // STL bit set container
# Include <cctype>
# Include <cerrno>
# Include <clocale>
# Include <cmath>
# Include <complex> // plural class
# Include <cstdio>
# Include <cstdlib>
# Include <cstring>
# Include <ctime>
# Include <deque> // STL double-end queue container
# Include <exception> // exception handling class
# Include <fstream>
# Include <functional> // STL defines operation functions (instead of operators)
# Include <limits>
# Include <list> // STL linear list container
# Include <map> // STL ing container
# Include <iomanip>
# Include <IOS> // Basic Input/Output support
# Include <iosfwd> // pre-declaration used by the input/output system
# Include <iostream>
# Include <istream> // basic input stream
# Include <ostream> // Basic output stream
# Include <queue> // STL queue container
# Include <set> // STL collection container
# Include <sstream> // string-based stream
# Include <stack> // STL stack container
# Include <stdexcept> // standard exception class
# Include <streambuf> // underlying input/output support
# Include <string> // string class
# Include <utility> // STL common template class
# Include <vector> // STL dynamic array container
# Include <cwchar>
# Include <cwctype>

Using namespace STD;

C99 added

# Include <complex. h> // plural Processing
# Include <fenv. h> // floating point Environment
# Include <inttypes. h> // integer format conversion
# Include <stdbool. h> // Boolean Environment
# Include <stdint. h> // integer environment
# Include <tgmath. h> // general mathematical macros

Iii. Origin of preprocessing

In the historical development of C ++, many language features (especially the obscure language) come from the C language. preprocessing is one of them. C ++ inherits the C language Preprocessor from the C language (C language Preprocessor, called CPP by Dr. Bjarne, is short for C program Preprocessor ).

Iv. Common preprocessing functions

The main function of the pre-processor is to replace a resource with the built-in pre-processing functions. The most common pre-processing methods include file inclusion, Conditional compilation, layout control, and macro replacement.
File Inclusion: # include is the most common preprocessing, mainly used as a reference combination source for files.ProgramBody.
Conditional compilation: # If, # ifndef, # ifdef, # endif, # UNDEF and so on are also common preprocessing tasks. They are mainly selected during compilation, comment out some specified code to achieve version control and prevent repeated inclusion of files.
Layout control: # pragma, which is an important aspect of application preprocessing. The main function is to provide unconventional control flow information for the Compilation Program.
Macro replacement: # define, which is the most common usage. It can define symbol constants, function functions, renaming, String concatenation, and other functions.

5. Pre-processing commands

The format of preprocessing commands is as follows:

# Directive tokens

# The symbol should be the first non-null character in this line. Generally, we place it at the starting position. If the command line cannot be placed, you can use "" for control, for example:

# Define error if (error) Exit (1)

Equivalent

# Define Error
If (error) Exit (1)

The following describes common preprocessing commands:

# Define macro definition
# UNDEF undetermined macros
# Include text inclusion
# Ifdef compile if the macro is defined
# Ifndef compile if the macro is not defined
# Endif end compilation block control
# Compile the Code if the expression is non-zero
# Compile else as the remaining options for other preprocessing
# Elif
# Line: change the current number of lines and file name
# Error output an error message
# Pragma provides unconventional control flow information for the Compilation Program

Next we will describe these preprocessing items one by one. Considering the importance and complexity of macros, let's put them to the end.

Vi. File Inclusion commands

This kind of preprocessing is the most common method, which is usually used in programming. The most common usage is:

# Include <iostream> // standard library header file
# Include <iostream. h> // old standard library header file
# Include "Io. H" // custom header file
# Include "../file. H" // header file under the parent directory of UNIX
# Include "/usr/local/file. H" // full path under UNIX
# Include ".. file. H" // header file under the parent directory of DoS
# Include "usrlocalfile. H" // full path under DOS

Note the following two points:

1. Do we use <iostream> or <iostream. h>?
We advocate using <iostream> instead of <iostream. h>:
First, the. h header file was abandoned by the Standards Committee as early as September. We should keep up with the standards to adapt to the development of the times. Second, iostream. h only supports narrow character sets, while iostream supports narrow/wide character sets. In addition, the iostream standard has made many changes, and both interfaces and implementations have changed. Finally, all iostream components are put into namespace STD to prevent name contamination.

2. What is the difference between <Io. h> and "Io. H?

In fact, their only difference is that the search path is different: For # include <Io. h>, the compiler starts searching for # include "Io. H ", the compiler starts searching from the user's working path.

VII. Compile control commands

The main purpose of these commands is to select and comment out some specified code during compilation to achieve version control and prevent repeated inclusion of files. Format:

1. If identifier is a defined symbol, the your code will be compiled; otherwise, it will be removed.

# Ifdef identifier
Your code
# Endif

2. If identifier is an undefined symbol, the your code will be compiled; otherwise, it will be removed.

# Ifndef identifier
Your code
# Endif

3. If expression is not zero, the your code will be compiled; otherwise, the code will be removed.

# If expression
Your code
# Endif

4. If identifier is a defined symbol, your code1 will be compiled; otherwise, your code2 will be compiled.

# Ifdef identifier
Your code1
# Else
Your code2
# Endif

5. If epression1 is non-zero, compile your code1. Otherwise, if expression2 is non-zero, compile your code2. Otherwise, compile your code3.

# If expressin1
Your code1
# Elif expression2
Your code2
# Else
Your code3
# Enif

In addition to the commonly used compilation commands in the preceding section, other pre-compilation Commands include # Line, # error, # pragma, let's talk about it briefly.

# Line Syntax:

# Line number filename

Example: # line 30 a.h

The file name a. h can be omitted without writing. This command can change the current row number and file name. For example, the preceding preprocessing command can change the current row number to 30 and the file name to A. H. It seems useless at first, but he is still a bit useful, that is, it is used in the compilation of the compiler, we know that the compiler will generate some intermediate files during the C ++ source code compilation process. Using this command, we can ensure that the file name is fixed and will not be replaced by these intermediate files, which is conducive to analysis.

# Error Syntax:

# Error info

For example:

# Ifndef Unix
# Error this software requires the Unix OS.
# Endif

This command mainly gives an error message. In the above example, if it is not in a Unix environment, it will output this software requires the Unix OS. Then it will induce the compiler to terminate. So in general, the purpose of this command is to give some information before the program crashes.

# Pragma is non-uniform and relies on various compiler producers. For example, in VC ++:

# Pragma comment (Lib, "dlltest. lib ")

Import the database to dlltest. Lib.

VIII. pre-defined identifier

To process some useful information, preprocessing defines some preprocessing identifiers. Although the preprocessing identifiers of various compilers are different, they all process the following four types:

_ File _ name of the file being compiled
_ Line _ the row number of the file being compiled
_ Date string at the time of compilation, for example, "25 Jan 2006"
_ Time string of the Compilation Time, for example, "12:30:55"

Example: cout <"the file is:" <__file _ "<"! The lines is: "<__ line __< <Endl;

9. Pre-processing

We will not discuss how to replace the # include pre-processing command here. C ++ does not provide an alternative form for # include, but namespace provides a scope mechanism that supports combination in some way and can improve the behavior of # include, but we still cannot replace # include.
# Pragma should be regarded as a dispensable preprocessing command. According to Bjarne, the father of C ++, it is: "# pragma is often used to hide semantic deformation in the compilation system, or to provide language extensions with special semantics and clumsy syntax."
For # ifdef, we are still helpless, even if we use the if statement and constant expression, it is still not enough to replace it, because the body of an if statement must be syntactically correct to meet the class check, even if he is in a branch that will never be executed.

10. Additional instructions on pre-compiled header files

This section describes how to use the pre-compilation function of vc6. Due to the complicated usage of pre-compilation, only the most important pre-compilation commands:/YU,/YC,/Yx, /FP. For more information, see:
Msdn-> Visual Studio 6.0 document-> visual c ++ 6.0 document-> VC ++ programmer guider-> compiler and linker-> details-> creating precompiled header files

Concepts of precompiled headers:

the so-called pre-compilation header is to pre-compile the part of the code in a project and put it in a file (usually. PCH Extension). This file is called the precompiled header file. The pre-compiled code can be any C/C ++ code, or even an inline function, however, it must be stable and won't be changed frequently during project development. If the code is modified, re-compile the pre-compiled header file. Note that it takes a lot of time to generate a precompiled header file. At the same time, you must note that the pre-compiled header file is usually large, usually 6-7 M. Clear unused pre-compiled header files in time.
you may ask: The current compiler has the time stamp function. when compiling the entire project, the compiler only compiles the modified files, instead of compiling files that have not been modified since the previous compilation. So why do we need to pre-compile the header file? Here, we know that the compiler is compiled in units of files. After a file is modified, it will re-compile the entire file. Of course, all the header files contained in this file are East and West (. eg macro, Preprocessor) must be processed again. The pre-compiled header file of VC stores this part of information. To avoid re-processing these header files every time.
as described above, the role of precompiled header files is of course to increase the speed of downloading. With this header file, you do not need to compile code that does not need to be changed frequently every time. Of course, the compilation performance is improved.
to use a pre-compiled header, you must specify a header file, which contains code that we will not change frequently and other header files, then we use this header file to generate a pre-compiled header file (. PCH file) We all know stdafx. h. Many people think that this is a system-level file provided by VC, a header file carried by the compiler. Actually not. This file can be of any name. We will examine a typical pre-compiled header file of the MFC dialog based program generated by Appwizard. (Because Appwizard specifies how to use the pre-compiled header file. The default value is stdafx. H, which is the name of VC ). The header file contains the following header files:

# Include <afxwin. h> // MFC core and standard components
# Include <afxext. h> // MFC extensions
# Include <afxdisp. h> // MFC automation classes
# Include <afxdtctl. h> // MFC support for Internet Explorer 4 common controls
# Include <afxcen. h>

These are the necessary header files used by MFC. Of course, we are unlikely to modify these header files in our project, so they are stable.
Then how can we specify it to generate the pre-compiled header file. We know that a header file cannot be compiled. Therefore, we need a CPP file to generate the. PCH file. The default value of this file is stdafx. cpp. There is only one code in this file: # include "stdafx. H ". The reason is, of course, we only need to compile it-that is to say, it only needs its. cpp extension. We can use the/YC compilation switch to specify stdafx. cpp to generate a. PCH file, and use the/FP compilation switch to specify the name of the generated PCH file. Open the project-> setting-> C/C ++ dialog box. Point category to the precompiled header. Select the entire project in the Tree View on the left.

we can use the pre-compiled header function. Note:
1): If/Yu is used, pre-compilation is used. the beginning of a CPP file. I emphasize that it is the beginning of a file, including the file that you specified to generate a PCH file. h file (default is stdafx. h) otherwise there will be problems. If you do not include this file, you will be notified of unexpected file end. If you do not include it at the beginning, you can try the following and you will find it amazing.
2) If you accidentally lose the PCH file, you only need to let the compiler generate a PCH File Based on the above analysis. That is to say, you can recompile stdafx. cpp (that is, the CPP file of the specified/YC. Of course, you can rebuild all. Simply select the CPP file and press Ctrl + F7.

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.