﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-mengkai-随笔分类-c/c++基础知识</title><link>http://www.cppblog.com/mengkai/category/17831.html</link><description /><language>zh-cn</language><lastBuildDate>Tue, 11 Oct 2011 19:29:28 GMT</lastBuildDate><pubDate>Tue, 11 Oct 2011 19:29:28 GMT</pubDate><ttl>60</ttl><item><title>转载csdn上关于详解关于#include "stdafx.h"</title><link>http://www.cppblog.com/mengkai/archive/2011/10/10/157981.html</link><dc:creator>mengkai</dc:creator><author>mengkai</author><pubDate>Mon, 10 Oct 2011 09:01:00 GMT</pubDate><guid>http://www.cppblog.com/mengkai/archive/2011/10/10/157981.html</guid><wfw:comment>http://www.cppblog.com/mengkai/comments/157981.html</wfw:comment><comments>http://www.cppblog.com/mengkai/archive/2011/10/10/157981.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/mengkai/comments/commentRss/157981.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/mengkai/services/trackbacks/157981.html</trackback:ping><description><![CDATA[<span style="color: #333333; font-family: Arial; font-size: 14px; line-height: 26px; "><div align="center"><strong><span style="font-size: 10pt; ">关于#include&nbsp;"stdafx.h"</span></strong></div><div style="margin-top: 7.85pt; margin-right: 7.85pt; margin-bottom: 12pt; margin-left: 7.85pt; "><strong><span style="font-size: 10pt; ">（1）</span></strong><span style="font-size: 10pt; ">Standard Application Frame Extend没有函数库，只是定义了一些环境参数，使得编译出来的程序能在32位的操作系统环境下运行。Windows和MFC的include文件都非常大，即使有一个快速的处理程序，编译程序也要花费相当长的时间来完成工作。由于每个.CPP文件都包含相同的include文件，为每个.CPP文件都重复处理这些文件就显得很傻了。为避免这种浪费，AppWizard和VisualC++编译程序一起进行工作，如下所示：<br />1.AppWizard建立了文件stdafx.h，该文件包含了所有当前工程文件需要MFCinclude 文件。且这一文件可以随被选择的选项而变化。<br />2.AppWizard然后就建立stdafx.cpp。这个文件通常都是一样的。<br />3.然后AppWizard就建立起工程文件，这样第一个被编译的文件就是stdafx.cpp。<br />4当VisualC++编译stdafx.cpp文件时，它将结果保存在一个名为stdafx.pch的文件里。(扩展名pch表示预编译头文件。)( 预编译头文件通过编译stdafx.cpp生成，以工程名命名，由于预编译的头文件的后缀是&#8220;pch&#8221;，所以编译结果文件是projectname.pch。)<br />5.当VisualC++编译随后的每个.cpp文件时，它阅读并使用它刚生成的.pch文件。<br />VisualC++不再分析Windows include文件，除非你又编缉了stdafx.cpp或stdafx.h。这个技术很精巧，你不这么认为吗？(还要说一句，Microsoft并非是首先采用这种技术的公司，Borland才是。)在这个过程中你必须遵守以下规则：<br />1.你编写的任何.cpp文件都必须首先包含stdafx.h。<br />2.如果你有工程文件里的大多数.cpp文件需要.h文件，顺便将它们加在stdafx.h(后部)上，然后预编译stdafx.cpp。<br />3.由于.pch文件具有大量的符号信息，它是你的工程文件里最大的文件。<br />如果你的磁盘空间有限，你就希望能将这个你从没使用过的工程文件中的.pch文件删除。执行程序时并不需要它们，且随着工程文件的重新建立，它们也自动地重新建立</span></div><div style="margin-top: 7.85pt; margin-right: 7.85pt; margin-bottom: 12pt; margin-left: 0in; "><strong><span style="font-size: 10pt; ">(2)</span></strong><span style="font-size: 10pt; ">stdafx.h文件中包含了一些必要的头文件（如afxwin.h），对应于stdafx.h有一个stdafx.cpp文件，该文件内包含一句： #include "stdafx.h"，其作用是令编译器编译出一个stdafx.obj预编译头文件（pre-compile header，需要设置编译选项），在下次编译时以降低总的编译时间。若使用ClassWizard定义新类，则有可能在stdafx.h中增加新的 include files。比如，若选用MFC template classes，stdafx.h中便会增加：#include &lt;afxtempl.h&gt;。</span></div><div style="margin-top: 7.85pt; margin-right: 7.85pt; margin-bottom: 12pt; margin-left: 0in; "><strong>(3)</strong>注：<span style="font-size: 10pt; ">1.afxwin.h是MFC编程的必需文件，其中包含如CString，CEdit类运行所必需的头文件，最好保证该句在头文件首行；它还会调用windows.h，改头文件包含有数据类型的定义、API入口点定义和其它有用的参数信息；</span></div><div style="margin-top: 7.85pt; margin-right: 7.85pt; margin-bottom: 12pt; margin-left: 0in; "><span style="font-size: 10pt; ">2.非MFC工程使用MFC库时最常见的问题就是windows.h重复包含错误：fatal error C1189: #error :&nbsp; WINDOWS.H already included.&nbsp; MFC apps must not #include &lt;windows.h&gt;；<br /><br />3.#define WIN32_LEANAND_MEAN，在windows的头文件中拒绝接受MFC类库，以加速编译时间；</span></div><div style="margin-top: 0in; margin-right: 7.85pt; margin-bottom: 7.85pt; margin-left: 0in; "><span style="font-size: 10pt; ">4.afx - afx中的af指的是Application Frame的缩写，曾经有一个技术开发团队专门作Application Frame，后来给这个团队命名用afx，x本身没有含义，只不过构成一个响亮的口号，后来就一直沿用下来。</span></div><div style="margin-top: 0in; margin-right: 7.85pt; margin-bottom: 7.85pt; margin-left: 0in; "><span style="font-size: 10pt; "><br />5.建立了一个新的空的工程,项目中的stdafx.cpp使用的是Create Precompiled Header (/Yc)，而其它.cpp是用的Use Precompiled Header (/Yu)，并且Create/Use PCH Trhough File是stdafx.h<br /><br /></span></div><div>&nbsp;</div><pre style="word-wrap: break-word; white-space: normal; ">&nbsp;</pre><div><span style="font-size: 10pt; "><br />&nbsp;<br /><br /></span></div><div><strong><span style="font-size: 10pt; ">(4)</span></strong><span style="font-size: 10pt; ">stdafx是预编译头文件。你可以从VC++集成环境菜单Project/Settings...中</span></div><div><span style="font-size: 10pt; ">Project&nbsp;Settings&nbsp;Dialog/C/C++/Category:Precompiled&nbsp;Headers/Use&nbsp;precompiled&nbsp;header&nbsp;file(.pch)的Check&nbsp;Box中看到。已经将Stdafx.h文件作为预编译的头文件来使用。预编译头文件是在编译所有Code之前，首先进行的动作。通过解析这个文件，取得定义的结构和参数。这样就不用在编译每个文件时都重新进行解析。提高编译速度。stdafx.h这个名称是可以改变的，你可以指定预编译头文件的名称。这个只在vc中有用,并不是c++的特性,vc中可以在这里声明全局变量和ID的地方</span></div><div>&nbsp;</div><div><strong><span style="font-size: 10pt; ">(5)</span></strong><span style="font-size: 10pt; ">&nbsp;fatal error C1083: Cannot open include file: 'stdafx.h': No such file or directory</span></div><pre style="word-wrap: break-word; white-space: normal; "><strong>A</strong>如果根本没有stdafx.h,你为何要包含它.一般只有大工程才需要预编译头文件.stdafx.h</pre><pre style="word-wrap: break-word; white-space: normal; ">删除这一行#include "stdafx.h"</pre><pre style="word-wrap: break-word; white-space: normal; "><strong>B</strong>&nbsp;project-&gt; Settings-&gt;c/c++ category-&gt;Precomiled Headers 选择第一个 Not using precompiled headers</pre><div><strong>C</strong>&nbsp;Project-&gt;Settings-&gt;C/C++-&gt;Project Options中把/Fp"Debug/Your_Project_Name.pch"和/Yu"stdafx.h"两项删掉就可以了。要注意原来在stdafx.h内包含的文件要包含到各个.cpp文件中.</div><pre style="word-wrap: break-word; white-space: normal; "><strong>D</strong>在*.cpp中的开头加入#include "stdafx.h".(#include "stdAfx.h" 放到另外#include的前面，也就是程序的最前面。使用预编译头文件需要把它放到程序最前面，否则它前面的内容会被忽略)</pre><pre style="word-wrap: break-word; white-space: normal; "><strong>E</strong>&nbsp;rebuild all</pre><pre style="word-wrap: break-word; white-space: normal; ">&nbsp;</pre><div><strong><span style="font-size: 10pt; ">(6)</span></strong><span style="font-size: 10pt; ">设置了预编译，如果不加#include "stdafx.h", 就会报这个错： fatal error C1010:</span></div><div><span style="font-size: 10pt; ">unexpected end of file while looking for precompiled header directive。编译器通过一个头文件stdafx.h来使用预编译头文件。stdafx.h这个头文件名是可以在project的编译设置里指定的。编译器认为，所有在指令#include "stdafx.h"前的代码都是预编译的，它跳过#include "stdafx. h"指令，使用projectname.pch编译这条指令之后的所有代码。因此，所有的CPP实现文件第一条语句都是：#include "stdafx.h"。</span></div></span><img src ="http://www.cppblog.com/mengkai/aggbug/157981.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/mengkai/" target="_blank">mengkai</a> 2011-10-10 17:01 <a href="http://www.cppblog.com/mengkai/archive/2011/10/10/157981.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>