c/c++--#include "stdafx.h" detailed __c++

Source: Internet
Author: User
Tags win32

Turn from: http://blog.csdn.net/qingkong8832/article/details/6695123
1, new project with VS2008, select "Win32"-"Win32 Console Application"-Enter Name: test-"OK"

"Next"-By default, we all have a check mark before "Empty project" to create a pure C or C + + program.

Here we select the default, "Precompiled headers" before the tick, and then "finish"

2, in the test project, the header file has stdafx.h and targetver.h, the source file has Stdafx.cpp and Test.cpp, and there is a ReadMe.txt file.

3, the default main function is

#include "stdafx.h"  


int _tmain (int argc, _tchar* argv[])  
{return  
    0;  
}  

Unlike our usual program, we usually write the following form:

#include  <stdio.h>  

int main ()  
{return  
       0;  

4, we want to tangle, here how did not stdio.h, replaced is stdafx.h it.
We open the StdAfx.h file as follows:

StdAfx.h: Standard system contains files containing files,//  
or  
project-specific inclusion files//  

#pragma once  

#include that are frequently used but infrequently changed Targetver.h "  

#include <stdio.h>  
#include <tchar.h>  



As you can see, here are the stdio.h library files, and why are they included here? We are not in a hurry to answer.
First Look, Stdafx.cpp file:

Stdafx.cpp: Source files//test.pch that include only standard include files  
will  
contain precompiled type information as precompiled Headers//stdafx.obj  

#include "stdafx.h"  

//TODO: In STDAFX. H  
//reference any required additional header files, not referenced in this file

This only contains the library file, no operation, it is what to do with it.
In ReadMe.txt, we see this passage:

Other standard documents:
StdAfx.h, StdAfx.cpp
These files are used to generate a precompiled header (PCH) file named Test.pch and a precompiled type file named StdAfx.obj.

The original precompiled header file was generated.

5, the encyclopedia on the following:

First, what is precompiled header?   
The so-called precompiled head, is the head of the file in advance compiled into a binary intermediate format for subsequent compilation process to use. Precompiled headers are physically the same as the usual. obj files, but do not confuse this intermediate format with the. o/.obj/.a/.lib format, they are completely different. So the precompiled header file has different attributes and target files (although they all belong to some sort of intermediate file). The. h,.c,.cpp file that is compiled into the precompiled header is compiled only once, as the part of the precompiled header does not change, and this part is not recompiled during subsequent compilation. And then greatly improve the speed of compilation, and easy to the right file management, but also to eliminate the duplication of the inclusion problem. --but there are similar places, for example, they are incompatible ^_^ between compilers, that is, you can not take VC generated precompiled head to GCC to use. Even the extension of different names, VC is familiar to everyone. PCH, and GCC's, is. GCH.     
second, when to use precompiled headers?   
When most. c or. cpp files require the same header file.   
When some code is heavily reused.   
when importing some different libraries There are implementations of functions that generate confusion.  

6, you can know precompilation, is to improve the speed of compilation.
Take another look at the following passage

StdAfx.h 1 names in English are all called: Standard application Fram Extend so-called head file precompilation, is the use of a Project (project) in some MFC standard header files (such as Windows.H, Afxwin.H) Pre Compile first, and later compile the project, no longer compile this part of the header file, only use the precompiled results.   
  This will speed up compilation and save time.   
  The precompiled header file is generated by compiling stdafx.cpp, named after the project name, and the compiled result file is projectname.pch because the suffix of the precompiled header file is "PCH". The compiler uses a precompiled header file StdAfx.h through a header file. StdAfx.h this header filename can be specified in project's compilation settings. The compiler believes that all code before the instruction #include "stdafx.h" is precompiled and skips #include "stdafx.   
  H "instruction, using PROJECTNAME.PCH to compile all the code after this instruction.  

Therefore, all MFC implementation files The first statement is: #include "stdafx.h".   
  2 detailed working principle and function there is no function library in stdafx.h, only some environment parameters are defined, so that the compiled program can run in 32-bit operating system environment. Windows and MFC include files that are very large, and even if you have a quick handler, it takes a considerable amount of time for the compiler to complete the work. Because of each. The CPP file contains the same include file for each.   
  CPP files are silly to duplicate these files. To avoid this waste, AppWizard and visualc++ compiler work together, as follows: AppWizard established the file stdafx.h, which contains all the Mfcinclude files required for the current engineering file.   
  This file can be changed with the selected option. AppWizard then set up stdafx.cpp.   
  This file is usually the same.   
  Then AppWizard built the project file, so the first compiled file is stdafx.cpp. When visualc++ compiles the Stdafx.cpp file, it saves the results in a file named Stdafx.pch. (The extension PCH represents the precompiled header file.) When visualc++ compiles each subsequent. cpp file, it reads and uses the. pch it just generated.File.   
  Visualc++ no longer analyze windowsinclude files unless you edit stdafx.cpp or stdafx.h. This technique is very ingenious, don't you think so? (To say, Microsoft is not the first company to adopt this technology, Borland is.)   
  In this process you must observe the following rules: any. cpp files you write must first contain stdafx.h.   
  If you have the. h file for most of the. cpp files in the engineering files, add them to the stdafx.h (back), and then precompile the stdafx.cpp.   
  Because the. pch file has a large number of symbolic information, it is the largest file in your engineering file. If your disk space is limited, you want to remove the. pch file from the project file that you have never used.   They are not needed to execute programs, and they are automatically restarted as engineering files are built.

Points:
(1). Windows and MFC include files that are very large, and even if you have a quick handler, it takes a considerable amount of time for the compiler to complete the work. Because of each. The CPP file contains the same include file for each. CPP files are silly to duplicate these files.
(2). In MFC, any. cpp files that you write must first contain stdafx.h.

(3). Because A. pch file has a large number of symbolic information, it is the largest file in your engineering file and can be deleted when it is not needed.

7, we can set our own precompiled file, not necessarily if stdafx.h

Right-click the Stdafx.cpp file and select Properties-"C + +"-"Precompiled Headers"-"Create/Use Precompiled Headers"

You can see 3 options: "Create Precompiled Headers," "Use Precompiled Headers," and "Do not use precompiled headers."

Stdafx.cpp definitely chose to "Create Precompiled Headers"

============================================================================

Right-click the Test.cpp file and select Properties-"C + +"-"Precompiled Header"-"Create/Use Precompiled Headers"

Visible, it selects "Use Precompiled Headers", the file selected below is the StdAfx.h file, and the path to the PCH file is indicated below

================================================================================

How to create your own precompiled files, in fact, the process as we said above.
If the project is very large, a lot of header files, and a few header files are often used, then

1, write all these headers into a header file, like writing to PREH.H

2, write a preh.c, inside only one sentence: #include "preh.h", in order to survive PCH precompiled files

3, for PREH.C, set "Create Precompiled Header" in "Properties", set "Use Precompiled headers" for other. c files.

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.