现在的位置: 首页 > 综合 > 正文

Cannot open include file: ‘stdafx.h ‘: No such file or directory

2013年09月07日 ⁄ 综合 ⁄ 共 7232字 ⁄ 字号 评论关闭

提示:Cannot open include file: 'stdafx.h ': No such file or directory

#include       "stdAfx.h "     放到另外#include的前面,也就是程序的最前面。使用预编译头文件需要把它放到程序最前面,否则它前面的内容会被忽略
-------------------------------------------------------------------------------
The way in which you have set up your workspace when creating a new project does not require the #include "stdafx.h", therefore delete this line (or comment it out).
------------------------------------------------------------------------------
预编译头文件(一般扩展名为.PCH),是把一个工程中较稳定的代码预先编译好放在一个文件(.PCH)里.这些预先编译好的代码可以是任何的C/C++代码--甚至可以是inline函数,只它们在整个工程中是较为稳定的,即在工程开发过程中不会经常被修改的代码.
为什么需要预编译头文件?一言以蔽之:提高编译速度.一般地,编译器以文件为单位编译,如果修改了一工程中的一个文件则所有文件都要重新编译,包括头文件里的所有东西(eg.Macro宏,Preprocessor预处理),而VC程序中,这些头文件中所包括的东西往往是非常大的,编译之将占很长的时间.但它们又不常被修改,是较稳定的,为单独的一个小文件而重新编译整个工程的所有文件导致编译效率下降,因此引入了.PCH文件.
如何使用预编译头文件以提高编译速度?要使用预编译头文件,必须指定一个头文件(.H),包含我们不会经常修改的代码和其他的头文件,然后这个头文件(.H)来生成一个预编译头文件(.PCH)VC默认的头文件就是StdAfx.h,因为头文件是不能编译的,所以我们还需要一个.CPP文件来作桥梁,VC默认的文件为StdAfx.cpp,这个文件里只有一句代码就是:#include "StdAfx.h".接下来要用它生成.PCH文件,涉及到几个重要的预编译指令:/Yu,/Yc,/Yx,/Fp.简单地说,/Yc是用来生成.PCH文件的编译开关.在Project->setting->C/C++的Category里的Precompiled Header,然后在左边的树形视图中选择用来编译生成.PCH文件的.CPP文件(默认即StdAfx.cpp)你就可以看到/Yc这个开关,它表示这个文件编译了以后是否生成.PCH文件(可能/Yc的c表示create)./Fp指令指定生成的.PCH文件的名字及路径(可能/Fp的p代表path)./Yu的u即use使用,工程中只要包括了.H文件的文件都会有这个/Yu指令.如果选择自动Automatic...的话则原来为/Yc的地方就换成了/Yx指令.如果选择自动,则每次编译时编译器会看以前有没有生成过.PCH文件,有则不现生成否则就再次编译产生.PCH文件.
注意:
A,实际上,由Appzard项目向导生成的默认的头文件及CPP文件StdAfx.h和StdAfx.cpp可以是任何名字的.原因很简单.但如果你要这样做就要记得修改相应的Project->setting...下的几个预编译指令(/Yc,/Yu,/Yx,/Fp)的参数.

B.在任何一个包括了将要预编译的头文件而使用了.PCH文件的工程文件的开头,一定必须要是在最开头,你要包含那个指定生成.PCH文件的.H文件(通过.CPP文件包括,默认为StdAfx.cpp),如果没包括将产生我最开头产生的错误.如果不是在最开头包括将产生让你意想不到的莫名其妙错误,如若不信,盍为试之?
C.预编译文件.PCH生成之很耗时间,而且生成之后它也很占磁盘空间,常在5-6M,注意项目完成之后及时清理无用的.PCH文件以节约磁盘空间.
D.如果丢了或删了.PCH文件而以后要再修改工程文件时,可将指定的/Yc的.CPP文件(默认为StdAfx.cpp)重新编译一次即可再次生成.PCH文件,不用傻傻的按F7或Rebuild All

(貌似这个人很牛B 文学功底也不浅 但是说起来话却怎么这么让人难以理解 这段话我就看了好多遍....
   -By:铁骨)
----------------------------------------------------------------------------
Problem
This message appears during the compilation of Microsoft Visual? C++ code generated by IBM Rational? Rose?:
"Cannot open include file: 'stdafx.h': No such file or directory"?  
  
Cause
As the error message says, the compiler is finding an include statement for the header file "stdafx.h", but can't find the file itself.
Rose is generating the following line: #include "stdafx.h" by default.
#include "stdafx.h"
The file stdafx.h is usually used as a precompiled header file. Precompiled headers help to speed up compilation when a group of files in a project do not change.  
  
Solution
If a precompiled header is not used, this include shouldn't get generated in the code. To turn it off, open the Visual C++ Component Properties dialog and in the tab "Includes" delete the text in the "Initial Source Includes".
Another possibility is to create an empty "stdafx.h" file.
For more information about precompiled header files, refer to the Microsoft knowledge base.
--------------------------------------------------------------------------------------------
#include"stdafx.h"这个语句的作用,是将该项目的目录中的stdafx.h这个文件包含到程序里,而你之所以遇到那个错误,是因为你的项目目录中没有stdafx.h这个文件
该文件如果被需要,那么在你新建项目的时候,Visual studio会自动为你生成一个,否则的话你可以去掉#include"stdafx.h"即可

看到很多程序前面都要加一句#include"stdafx.h"
这个语句并不是每个程序所必须有的

所谓头文件预编译,就是把一个工程(Project)中使用的一些MFC标准头文件(如Windows.H、Afxwin.H)预先编译,以后该工程编译时,不再编译这部分头文件,仅仅使用预编译的结果。这样可以加快编译速度,节省时间。
预编译头文件通过编译stdafx.cpp生成,以工程名命名,由于预编译的头文件的后缀是“pch”,所以编译结果文件是projectname.pch。
编译器通过一个头文件stdafx.h来使用预编译头文件。stdafx.h这个头文件名是可以在project的编译设置里指定的。编译器认为,所有在指令#include "stdafx.h"前的代码都是预编译的,它跳过#include "stdafx. h"指令,使用projectname.pch编译这条指令之后的所有代码。
因此,所有的CPP实现文件第一条语句都是:#include "stdafx.h"。
-------------------------------------------------------------------------------------------
簡單來說,stdafx.h 和 precompile header 有關。如果你在編譯器環境設定不使用 procompile header 的話,就可以不要理會 stdafx.h/.cpp 這東西。不過對編譯大型的程式來說,使用 precompile header 編譯會比較省時間(precompile header 把常用到的又不常變動的 library source 先預先編譯,就像先預先做成 .lib 一樣,不過 source 有變,precompile header 可以更新)

一些小問題!請問一下:
(1).
.pch 檔是不是就是 precompile header ?那它和 stdafx.h 有什麼不同。
(2).
而當我在project/setting 內設定時,要選擇那一個呢!
Automatic use of precompile headers
Create precompile header file [ .pch ]
Use precompile header file [ .pch ]
有.pch 的選項是不是就要輸入 ProjectName.pch,那它和 stdafx.h 有什麼關係?
(3).
還有大大所以講的:
『把常用到的 Standard C++ Library 的一部份(如 STL)先弄到 precompile header 裡來增加編譯的效率』
如何做呢?
是將那些 iostream.h、vector.h 、list.h 放到 precompile header 嗎?
那是放到那一檔?
謝謝!
--------------------------------------------------------------------------------

(1)
.pch 就是預先編譯後的產物,stdafx.h 則是用來定義要預先編譯的地方,不過我之前提到這是 VC 的使用方式,不見得每個編譯器都是這樣的用法,像 Matrowerks CodeWarrior 則是使用任意的文字檔當作 precompile header source,這個文字檔 include 那些常用又很少變更的 header,然後以:
#pragma precompile_target filename.pch
來指明預先編譯後的產物檔,然後在 CodeWarrior IDE 裡設定要使用這個 .pch 檔。
(2)
至於 VC 設定,我不清楚你使用哪一版的 VC,以我使用的 VC7.0(VC.NET) 為例,在 [C/C++ Precompiled Headers] page 裡可以設定 "Create/Use Precompiled Headers" 為 Use Precompiled Header,"Create/Use PCH Through File" 設定為 StdAfx.h(VC project template 就是習慣這樣搞),然後在 "Precompiled Header File" 這一欄設定預先編譯後的產物(.pch)。
(3)
你可以把常用到的 <iostream> 之類的 include 到 stdafx.h 裡,不過記得最後要留一個空行。使用到 iostream 的 cpp 檔可以不再明白 include <iostream>。
#include   "stdafx.h"是在当前目录找stdafx.h文件。  
   改为  
   #include   <stdafx.h>是在VC指定目录找stdafx.h文件。

(台胞的方法   .....         by: -铁骨)

-------------------------------------------------------------------------------------------
Cannot open include file: 'stdafx.h': No such file or directory Error executing cl.exe.
I tray change Project<Settings<C/C++<Precompiled headers and It do not success.
Thank you
---------------------
Hi,
Could you please try doing "Rebuild All" in Build menu.

---------------------
you must save all files in the same folder
and
use the same name of the header file as followes:
Sum.h // when you named your header file and saved it ,
we must call it as :
#include "Sum.H" in the .cpp file and main.cpp

(老外的)
---------------------------------------------

附:
1、fatal error C1010: unexpected end of file while looking for precompiled header directive。
寻找预编译头文件路径时遇到了不该遇到的文件尾。(一般是没有#include "stdafx.h")
2、fatal error C1083: Cannot open include file: 'R…….h': No such file or directory
不能打开包含文件“R…….h”:没有这样的文件或目录。
3、error C2011: 'C……': 'class' type redefinition
类“C……”重定义。
4、error C2018: unknown character '0xa3'
不认识的字符'0xa3'。(一般是汉字或中文标点符号)
5、error C2057: expected constant expression
希望是常量表达式。(一般出现在switch语句的case分支中)
6、error C2065: 'IDD_MYDIALOG' : undeclared identifier
“IDD_MYDIALOG”:未声明过的标识符。
7、error C2082: redefinition of formal parameter 'bReset'
函数参数“bReset”在函数体中重定义。
8、error C2143: syntax error: missing ':' before '{'
句法错误:“{”前缺少“;”。
9、error C2146: syntax error : missing ';' before identifier 'dc'
句法错误:在“dc”前丢了“;”。
10、error C2196: case value '69' already used
值69已经用过。(一般出现在switch语句的case分支中)
11、error C2509: 'OnTimer' : member function not declared in 'CHelloView'
成员函数“OnTimer”没有在“CHelloView”中声明。
12、error C2511: 'reset': overloaded member function 'void (int)' not found in 'B'
重载的函数“void reset(int)”在类“B”中找不到。
13、error C2555: 'B::f1': overriding virtual function differs from 'A::f1' only by return type or calling convention
类B对类A中同名函数f1的重载仅根据返回值或调用约定上的区别。
14、error C2660: 'SetTimer' : function does not take 2 parameters
“SetTimer”函数不传递2个参数。
15、warning C4035: 'f……': no return value
“f……”的return语句没有返回值。
16、warning C4553: '= =' : operator has no effect; did you intend '='?
没有效果的运算符“= =”;是否改为“=”?
17、warning C4700: local variable 'bReset' used without having been initialized
局部变量“bReset”没有初始化就使用。
18、error C4716: 'CMyApp::InitInstance' : must return a value
“CMyApp::InitInstance”函数必须返回一个值。
19、LINK : fatal error LNK1168: cannot open Debug/P1.exe for writing
连接错误:不能打开P1.exe文件,以改写内容。(一般是P1.Exe还在运行,未关闭)
20、error LNK2001: unresolved external symbol "public: virtual _ _thiscall C……::~C……(void)"
连接时发现没有实现的外部符号(变量、函数等)。
(最常见得20个错误)
---------------------------------------------------------------------------

抱歉!评论已关闭.