提示: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個錯誤)