好久没写C++程序了,碰到了很多问题。伤心中,加油,一点一点慢慢来,重拾的过程,理解能力还是不错的,呵呵,自我安慰 一下。
以前在VC6中写的简单的单文件的C++单文件程序中没注意到"stdafx.h"这个文件,先把它拿来看一看:
1、当我们建立一个空工程时,当然你里面是什么文件都没有的
2、当我们建立一个“hello world”工程是,出现了两个不速之客
这个程序可以运行,看我们的主函数:
// del1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
int main(int argc, char* argv[])
{
printf("Hello World!\n");
return 0;
}
我们没有看到有“error C2065: 'printf' : undeclared identifier”的错误产生,看来stdio.h这个头文件已经编译到本工程中了,那么,他在哪儿呢?
//StdAfx.cpp文件
// stdafx.cpp : source file that includes just the standard includes
// del1.pch will be the pre-compiled header
// stdafx.obj will contain the pre-compiled type information
#include "stdafx.h"
// TODO: reference any additional headers you need in STDAFX.H
// and not in this file
//---------------------------------------------------------------------------------------------
//StdAfx.h文件:
// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//
#if !defined(AFX_STDAFX_H__8B8838C4_046A_4930_B82A_68E89A686214__INCLUDED_)
#define AFX_STDAFX_H__8B8838C4_046A_4930_B82A_68E89A686214__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
#include //<<<--------------------------------------------------------看这里
// TODO: reference additional headers your program requires here
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_STDAFX_H__8B8838C4_046A_4930_B82A_68E89A686214__INCLUDED_)
3、我们再建立一个空的MFC工程:
她引入了更多的头文件:
#include
#include // MFC core and standard components
#include // MFC extensions
#include // MFC support for Internet Explorer 4 Common Controls
#ifndef _AFX_NO_AFXCMN_SUPPORT
#include // MFC support for Windows Common Controls
#endif // _AFX_NO_AFXCMN_SUPPORT
#include
那他究竟起到的作用是什么呢?我们需要他的理由是什么呢??
1、名称:
StdAfx:Standard Application Fram Extend:头文件预编译
预编译头文件通过编译stdafx.cpp生成,以工程名命名,由于预编译的头文件的后缀是“pch”,所以编译结果文件是projectname.pch。
stdafx.h这个头文件名是可以在project的编译设置里指定的。
2、预编译范围:
编译器认为,所有在指令#include "stdafx.h"前的代码都是预编译的,它跳过#include "stdafx. h"指令,使用projectname.pch编译这条指令之后的所有代码。
3、她做了些什么:
定义了一些环境参数,使得编译出来的程序能在32位的操作系统环境下运行。 (参看上面)
4、她是怎样工作的:
Windows和MFC的include文件都非常大,即使有一个快速的处理程序,编译程序也要花费相当长的时间来完成工作。由于每个.CPP文件都包含相同的include文件,为每个.CPP文件都重复处理这些文件就显得很傻了。
为避免这种浪费,AppWizard和VisualC++编译程序一起进行工作,如下所示:
◎AppWizard建立了文件stdafx.h,该文件包含了所有当前工程文件需要的MFCinclude文件。且这一文件可以随被选择的选项而变化。
◎AppWizard然后就建立stdafx.cpp。这个文件通常都是一样的。
◎然后AppWizard就建立起工程文件,这样第一个被编译的文件就是stdafx.cpp。
◎当VisualC++编译stdafx.cpp文件时,它将结果保存在一个名为stdafx.pch的文件里。(扩展名pch表示预编译头文件。)
◎当VisualC++编译随后的每个.cpp文件时,它阅读并使用它刚生成的.pch文件。VisualC++不再分析Windowsinclude文件,除非你又编辑了stdafx.cpp或stdafx.h。
这个技术很精巧,你不这么认为吗?(还要说一句,Microsoft并非是首先采用这种技术的公司,Borland才是。)在这个过程中你必须遵守以下规则:
◎你编写的任何.cpp文件都必须首先包含stdafx.h。
◎如果你有工程文件里的大多数.cpp文件需要.h文件,顺便将它们加在stdafx.h(后部)上,然后预编译stdafx.cpp。
◎由于.pch文件具有大量的符号信息,它是你的工程文件里最大的文件。
如果你的磁盘空间有限,你就希望能将这个你从没使用过的工程文件中的.pch文件删除。执行程序时并不需要它们,且随着工程文件的重新建立,它们也自动地重新建立。
好啦,大篇幅复制粘贴,谁让自己是那种砸破砂锅问到底的类型呢。