当使用visual C++时,总是包含了头文件stdafx.h(stdafx=Standard Application Fram Extend ), 新加的cpp文件如果包含stdafx.h还会提示出错,现在来解决这个疑问!
Windows和MFC的include文件都非常大,即使有一个快速的处理程序,编译程序也要花费相当长的时间来完成工作。为避免这种浪费,
,在stdafx.h中添加的是mfc用到的头文件和环境参数
比如:
#include <afxwin.h> // MFC core and standard components
#include <afxext.h> // MFC extensions
#include <afxole.h> // MFC OLE classes
#include <afxodlgs.h> // MFC OLE dialog classes
#include <afxdisp.h> // MFC Automation classes
......
等等,在生成stdafx.h头文件的同时,也生成了stdafx.cpp源文件,该源文件只包含#include "stdafx.h"语句,这是在编译过程中第一个被编译的文件,编译的结果保存在一个名为stdafx.pch(pch=pre-compile header)的文件里。 (扩展名pch表示预编译头文件。)当Visual C++编译随后的每个.cpp文件时,它阅读并使用它刚生成的.pch文件。 Visual C++不再分析Windows include文件,除非用户又编缉了stdafx.cpp或stdafx.h。
(实际上,我们写程序的时候也经常有这样一个习惯,把经常用到的常量,宏,函数放到一个头文件中,stdafx.h的作用和此类似。)
问题:为什么编写的任何.cpp文件都必须首先包含stdafx.h
原因:在cpp文件的属性里的Precompiled Headers选项中,stdafx.cpp使用的是Create Precompiled Header (/Yc),而其它.cpp是用的Use Precompiled Header (/Yu),并且Create/Use PCH Trhough File都是stdafx.h,也就是说,如果你的cpp文件不想用预编译头,可以更改这个cpp的属性的Precompiled Headers选项,不让它使用预编译头即可