Recently, due to work, I started to write C ++ code. Since I have been writing java for more than two years, the content of C ++ is still in small projects during the university period. This time, it is a big project for more than 120 sub-projects, I want to figure out all the things I didn't understand in college. First of all, because I use VS to write code, one of its features is pre-compilation.
All stdafx. h names are called Standard Application Framework Extensions.
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 all commands in the # include "stdafx. h "the previous code is pre-compiled, and it skips # include" stdafx. h "command, using projectname. pch compiles all the code after this command.
Therefore, the first statement of all the MFC implementation files is: # include "stdafx. h ".
Detailed working principles and functions
There is no function library in stdafx. h, but some environment parameters are defined so that the compiled program can run in a 32-bit operating system environment.
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 edited 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.
You can change the name of stdafx if you think it looks uncomfortable. The method is as follows:
1. Delete stdafx. h and stdafx. cpp from the current project.
2. Change stdafx. h and stdafx. cpp to any name you want, such as myafx. h and myafx. cpp.
3. Change "# include" stdafx. h "in myafx. cpp to" # include "myafx. h ""
4. Add myafx. h and myafx. cpp to the current project respectively.
5. Go to project property settings-> C/C ++-> pre-compilation header, and change stdafx. h to myafx. h.
6. go to solution manager and go to myafx. on cpp, right-click-> properties-> C/C ++-> pre-compilation header and set the first item as "Create Pre-compilation Header.
7. Change all # include "stdafx. h" in the project to # include "myafx. h"
8. Save, compile, and modify successfully
1. Delete stdafx. h and stdafx. cpp from the current project.
2. Change stdafx. h and stdafx. cpp to any name you want, such as myafx. h and myafx. cpp.
3. Change "# include" stdafx. h "in myafx. cpp to" # include "myafx. h ""
4. Add myafx. h and myafx. cpp to the current project respectively.
5. Go to project property settings-> C/C ++-> pre-compilation header, and change stdafx. h to myafx. h.
6. go to solution manager and go to myafx. on cpp, right-click-> properties-> C/C ++-> pre-compilation header and set the first item as "Create Pre-compilation Header.
7. Change all # include "stdafx. h" in the project to # include "myafx. h"
8. Save, compile, and modify successfully