What does stdafx. h In C ++ mean?

Source: Internet
Author: User
Tags what header
From: http://chunqingu.blog.163.com/blog/static/2275535720115743951140/

 

When using Visual C ++, it always contains the header file stdafx. h, but does not know what to do.

Haha, I found the relevant information today and explained it as follows:

When we use Appwizard to automatically generate certain projects, the system will automatically include the header files in stdafx. h: Include the stdafx directly. h file. Because CPP contains the same include file for different source files in the same project, it is silly to include these files repeatedly for each CPP file.

What header files need to be included in stdafx. h depends on your selection in Appwizard.

For example:

# Include <afxwin. h> // MFC core and standard components

# Include <afxext. h> // MFC extensions

.......

This is much more convenient, so stdafx. H is automatically generated. This eliminates the need to include the header file in every CPP file during development, and facilitates maintenance.

Generate stdafx. the H header file also generates stdafx. CPP source file, which only contains # include "stdafx. H "Statement, which is the first compiled file during the compilation process. The compilation result is saved in a file named stdafx. in the PCH file. (The extension PCH indicates the precompiled header file .) when Visual C ++ compiles each subsequent. CPP file, it reads and uses the PCH file that it just generated. visual c ++ does not analyze Windows include files any longer, unless you have edited stdafx. h or stdafx. CPP.

After reading this explanation, I immediately started my experiment. My new windows Window project generated stdafx. h and stdafx. cpp.

In addition, in the main source file, the cinlude header file stdafx. h.

In this case, only the Appwizard is used to automatically generate a project. Otherwise, there is no need to include this header file stdafx. h.

Supplement:

Standard application frame extend

There is no function library in stdafx. h, but some environment parameters are defined so that the compiledProgramIt can run in a 32-bit operating system.
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.

 

 

How to use the pre-compilation technology of VC

I. concept:

1. Pre-Compilation: the compiler first compiles a file (called the pre-compilation header file) and saves the compilation result, if other source files include the pre-compiled header file, the required information is automatically extracted from the compilation result for compilation.

2. precompiled header file: The file used to save the compiled symbolic information (. PCH as the suffix)

3. Create precompiled header file ): let's say that source file a uses file B to "generate the pre-compilation result file", which means to save the compilation result of "B" to the pre-compilation result file when compiling. If you use the wizard, file a is "stdafx. cpp", and file B is "stdafx. H ". In stdafx. cpp, there is only one line of statement:

# Include "stdafx. H"

4. Use the pre-compiled header (using precompiled header): A source file (. CPP) through "stdafx. h. if. the first include statement of CPP is # include "stdafx. H ", then directly obtain the results of the pre-compiled result file, and no longer recompile" stdafx. h"

2. How does the wizard work?

1. Set the pre-compilation option of "stdafx. cpp" to "generate the pre-compilation result file" through the "stdafx. H" file ".

2. The pre-compilation options of other source files are set to "use the pre-compilation Header" through "stdafx. H"

Iii. Usage principles?

1. Write all the relatively stable header files (such as CRT, STL, and third-party fixed libraries) in stdafx. h. (Whether to use stdafx. h depends on your personal preferences, but use stdafx. h can be consistent with the Wizard)

2. Add # include "stdafx. H" to the first line of all source files ".

3. Some unmodifiable source files (ifCode, Do not have the permission to modify the code), set his pre-compilation option to "do not use the pre-compilation Header ". Note that you must not select "automatically generate pre-compiled headers" because it will. the result of H is rushed out (I don't know if it is a bug or a design problem ,. ^_^ .).

Iv. Q &

Q. Why not all of them use "automatically generate pre-compiled header files "?

A. There is no difference between "automatically generate pre-compiled header files" and "nothing". The compilation speed is not significantly improved.

Q. When manually adding a new source file to the project, similar errors often occur:

Fatal error c1010: unexpected end of file while looking for precompiled header Directive

A. Because the default setting of the Wizard is "using precompiled headers", the newly added file does not include "stdafx. H" in the first line ". The solution is either to "do not use the pre-compiled Header", or to add a line # include "stdafx. H"

Q. Do I always think stdafx. h and stdafx. cpp are bound to the compilation platform and are not portable?

A. Actually, there is no problem with writing stdafx. h. My solution is (stdafx. H content ):

-Begin of file stdafx. h

# Ifdef _ Win32

# Include "win. H" // indicates the public header file in the window.

# Else

# Include "Linux. H" // generic public header file in Linux

# Endif

# Include "CRT. H" // Generic C standard library

# Include "STL. H" // generic reference STL Library

-End of File

Or a little simpler. If it is not a VC compiler, stdafx. H will not write anything!

 

 

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.