StdAfx.h and StdAfx.cpp

  

StdAfx.h and StdAfx.cpp

------以下内容来自《Programming windows with MFC》

AppWizard-generated projects speed program builds by taking advantage of a feature of Visual C++ known as precompiled headers. As a result of build settings implemented by AppWizard, all header files that are #included in StdAfx.h are precompiled into files named projectname.pch and StdAfx.obj so that once compiled, they don't have to be compiled again. AppWizard #includes StdAfx.h in the CPP files that it generates, and inside StdAfx.h, it adds #includes for core MFC header files such as Afxwin.h. You can add #includes of your own for other MFC header files, for C run-time header files, and for static header files of other types. Do not #include header files that are subject to change as the application is being developed, or you'll lose the benefits of using precompiled headers.

 

An interesting aside to a discussion of precompiled headers is the fact that Visual C++ effectively ignores statements that appear in a source code file before the statement that #includes StdAfx.h. That means code like this will compile just fine:

 

kjasdfj;oai4efj
#include "Stdafx.h"

Why is this fact important? Because more than one MFC programmer has been bitten by code like this:

#include <math.h>
#include "Stdafx.h"

Put the #include for Math.h after the #include for StdAfx.h (or better yet, put it inside StdAfx.h) to avoid this kind of error.

 

 Visual C++用预编译方法来提高速度。由于AppWizard的设置,StdAfx.h文件中包含的所有的头文件都被预编译到文件:工程名.pch和StdAfx.obj,因此它们只编译一次,不会重复编译。

 

一个有趣的现象就是在#include "StdAfx.h"文件之前的代码会被忽略。以下代码也可以通过编译:

你可能会问这有什么重要呢?

因为有很多程序员会这样写他们的程序:

 

因此为了避免错误最好是把要包含的文件写在#include "Stdafx.h"文件后。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值