I. Pre-compiled header file usage experience:
If the Pre-compiled header file is used correctly, it does greatly improve the programming efficiency (How much time are you waiting for the compilation to complete? A lot of it, this time is generally boring, helpless, a waste of time ). However, it is too easy to use errors. The following are some common mistakes.
1) include your own header file in the pre-compiled header file (If your header file does not change frequently, you can also)
Cause: Your header files are usually changed, which leads to re-Compilation of pre-compiled stuff, reducing the compilation speed.
2) include pre-compiled header files in other header files.
Suppose your other header files include pre-compiled header files. If someone else references your header file and does not set it as a pre-compiled header file, the person who references your header file will suffer.
Original cause: Because you used it. the pre-compiled header file is included in the hfile. In his project, VS can determine that it is a pre-compiled header and can also find the required PCH and PDB files. So there is no impact on the people who write this. h file. But as a customer, you work in your project and you include his h header file. At this time, vs cannot determine that the stdafx included in his header file is a pre-compiled header file, compile common files. It can be imagined that, in his stdafx, if there is a large external library (such as the inventor TLB, It is very slow and we have made this mistake), the Compilation speed is simply suffering. The most terrible thing is that you need to re-compile any simple modification in the future, which is exactly the opposite of the problem solved by the Pre-compilation.
The following describes how to use the pre-compiled header file and enjoy the improvement of the Compilation speed brought by the Pre-compiled header file:
1) Add an stdafx. h file (name is whatever you want, and the default name provided by vs is used here. include the header file to be used in the H file (usually an external library, you can also add the infrequently changed header files you write)
2) Add a stdafx. cpp file and include "stdafx. H"
3) set project properties to use precompiled header and stdafx. h.
4) stdafx. cpp properties --> C/C ++-> precompiled is set to create precompiled header, stdafx. h
Ii. Concepts and functions of precompiled header files
The so-called pre-compilation header refers to the part of a project.Code, Pre-compiled to put 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 pre-compiled 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 the compiler compiles the entire project, it 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 files. After a file is modified, it will re-compile the entire file. Of course, all the header files contained in this file (. 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.
2. Use of pre-compiled headers:
To use the pre-compiled header, we 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 the stdafx. h file. 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 MFC dialog based generated by Appwizard.Program. (Because Appwizard specifies how to use the pre-compiled header file. The default value is stdafx. H, which is the name of VC ). We will find that this 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 do 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. In the figure, we can see/FP "Debug/PCH" in the project options (the white place in the lower right corner. PCH ", which is generated by the specified. the name of the PCH file. The default value is usually <Project Name>. PCH
Then, select stdafx. cpp in the Tree View on the left, and the original project option becomes the source file option (originally a project, now a file, of course changed ). Here we can see the/YC switch. The function of/YC is to specify this file to create a PCH file. The file name after/YC is the header file that contains stable code. Only one file in a project can have the YC switch. VC compiles stdafx. cpp into an OBJ file and a PCH file based on this option.
In the command line, we can see/YU "stdafx. H"/FP "Debug/depmodify. PCH"
Select another CPP file. Here, precomplier chooses use ......... Header file is the stdafx. h file that we specified to create the PCH file. In fact, the settings in the project are used here, (1)/Yu "stdafx. h ".
In this way, we have set the pre-compiled header file. That is to say, we can use the pre-compiled header function.
1): If/Yu is used, that is, pre-compilation is used. the beginning of the CPP file (I emphasize it once) contains the file you specified to generate the 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 re-compile 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.