The include files for Windows and MFC are very large. Even if there is a fast processing program, it takes quite a long time to compile the program. Since each. cpp file contains the same include file, it is silly to process these files repeatedly for each. cpp file.
To avoid this waste, Appwizard works with the VisualC ++ compiler as follows:
◎ Appwizard creates the stdafx. h file, which contains all the mfcinclude files required by the current project file. This file can change with the selected options.
◎ Appwizard, then stdafx. cpp is created. This file is usually the same.
◎ Then Appwizard creates the project file, so that the first compiled file is stdafx. cpp.
◎ When Visual C ++ compiles the stdafx. cpp file, it stores the result in a file named stdafx. PCH. (The extension PCH indicates the precompiled header file .)
◎ When Visual C ++ compiles each subsequent. cpp file, it reads and uses the. PCH file it just generated. VisualC ++ does not analyze windowsinclude files any longer, unless you have compiled stdafx. cpp or stdafx. h.
This technology is very sophisticated. Don't you think so? (Also, Microsoft is not the first company to adopt this technology, Borland is .) In this process, you must follow the following rules:
◎ Any. cpp file you write must first contain stdafx. h.
◎ If you have a majority of. cpp files in the project file that require. H files, add them to stdafx. H (back) and pre-compile stdafx. cpp.
◎ Because the. PCH file has a lot of symbolic information, it is the largest file in your project file.
If your disk space is limited, you want to delete the. PCH file from the project file you have never used. They are not required when the program is executed, and they are automatically re-established as the project file is re-established.
Stdafx. cpp and stdafx. h are pre-compiled header file groups. Let's take a look at their content first:
// Stdafx. h: includefileforstandardsystemincludefiles,
// Orprojectspecific1_defilesthatareusedfrequently,
// Arechangedinfrequently
//
# If! Defined (afx_stdafx_h000046c144a1_935d_4d36_9a04_f77c1728143a00000000ded _)
# Defineafx_stdafx_h _ 46c144a1_935d_4d36_9a04_f77c1728143a1_encoded _
# If_msc_ver> 1000
# Pragmaonce
# Endif // _ msc_ver> 1000
// Todo: referenceadditionalheadersyourprogramrequireshere
// {Afx_insert_location }}
// Microsoftvisualc ++ willinsertadditionaldeclarationsimmediatelybeforethepreviousline.
# Endif //! Defined (afx_stdafx_h000046c144a1_935d_4d36_9a04_f77c1728143a00000000ded _)
// Stdafx. cpp: sourcefilethatincludesjustthestandardincludes
// Example01.pchwillbethepre-compiledheader
// Stdafx. objwillcontainthepre-compiledtypeinformation
# Include "stdafx. H"
// Todo: referenceanyadditionalheadersyouneedinstdafx. h
// Andnotinthisfile
First, let's look at the first two and last lines of stdafx. H, which is called the header file protection mechanism. In this file header, first determine whether the DEFINE _ macro is defined. If it is undefined (apparently at the beginning), run the following code: define this macro, then write the header file content, followed by a # endif. Therefore, if the header file is # include twice, the macro _ macro has been defined for the first time, and the "macro undefined" condition is not met when the header file is included for the second time, therefore, the header file content will not be repeatedly included. This avoids the direct or indirect inclusion of a header file multiple times. The macro name starts with VC. The reason why the macro name is so long is to hope that it will not be repeated with the user-defined things, I don't think any programmer will think of such a strange name in his own code? Then we can find a line in this file: "// todo: referenceadditionalheadersyourprogramrequireshere ". This prompts you to write the # include macro of all header files used in the project here, and then write it in all. # include "stdafx. H ", there are many advantages: first, it is convenient. When we want to change the include or not of some header files, we only need to modify the content in stdafx. H, instead of changing the # include statement in each source file. Another point is that there is a mechanism called pre-compilation in VC: As we can see, stdafx. in CPP, only one line of code exists: # include "stdafx. H ", when Vc is compiling, it will first compile this file to generate. PCH file, and then compile other # include "stdafx. H "file, you only need to connect to this. the PCH file does not need to be compiled again. the header file contained in H, which will greatly improve the compilation efficiency (of course, the implementation of this technology is not so simple, so I will not discuss it for a long time ). You don't have to worry about other content in stdafx. h.
Because the. c file cannot contain stdafx. H, you can use project settings to set its pre-compilation header to "not used":
- The Project Settings dialog box is displayed.
- Select C/C ++
- Select precompilation header for category
- Select not to use the pre-compiled header.