关于预编译

今天在VS2012上面练习了一个很简单的程序,结果一编译就出问题了

 

   1: #include <iostream>
   2:  
   3: using namespace std;
   4:  
   5: int main() {
   6:     cout << "\aOperation\"HyperHype\"is now activated!\n";
   7:     cout << "Enter your agent code:_______\b\b\b\b\b\b\b";
   8:     long code;
   9:     cin >> code;
  10:     cout << "\aYou entered " << code << "...\n";
  11:     cout << "\aCode verified! Proceed with plan Z3!\n";
  12:     cin >>code;
  13:     return 0;
  14: }

 

然后出现了下面的错误

 
fatal error C1010: unexpected end of file while looking for precompiled header. Did you forget to add '#include "stdafx.h"' to your source?

 

在我们创建一个 Win32 Console Application的时候,VS为我们加上了,但是由于习惯,我就把它删了。

 

我就纳闷啦,不至于吧,这么简单的程序都不行?

按照思路百度了一下

 

当我们使用AppWizard来自动生成某些项目的时候,系统会自动把所需要include的头文件在stdafx.h中先include一下,这样,我们只需要直接include这个stdafx.h文件即可.因为同一个项目中的不同源文件CPP都包含相同的include文件,这样,为每个.CPP文件都重复include这些文件就显得很傻了。

具体在stdafx.h中需要include什么头文件,取决于用户在AppWizard中的选择.

比如:

#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是自动生成的.这就使得用户在开发中不必在每一个cpp文件中都烦琐的include头文件了,而且,维护起来也方便.

在生成stdafx.h头文件的同时,也生成了stdafx.cpp源文件,该源文件只包含#include "stdafx.h"语句,这是在编译过程中第一个被编译的文件,编译的结果保存在一个名为stdafx.pch的文件里。 (扩展名pch表示预编译头文件。)当Visual C++编译随后的每个.cpp文件时,它阅读并使用它刚生成的.pch文件。 Visual C++不再分析Windows  include文件,除非用户又编缉了stdafx.cpp或stdafx.h。

只在使用AppWizard来自动生成项目的时候,才出现.否则,就没有必要include此头文件stdafx.h了

 

解决方案

image

 

把这个选择 “Not Using Precompiled Headers”

 

但是,后面出现的问题是,我加了这个头文件,它又说我没有定义 cout和cin了。。。

posted @ 2014-03-16 22:52  此去经年  阅读(314)  评论(0编辑  收藏  举报