Chinaunix首页 | 论坛 | 博客
  • 博客访问: 113671
  • 博文数量: 39
  • 博客积分: 1680
  • 博客等级: 上尉
  • 技术积分: 395
  • 用 户 组: 普通用户
  • 注册时间: 2009-10-10 12:42
文章分类

全部博文(39)

文章存档

2011年(1)

2010年(8)

2009年(30)

我的朋友

分类: C/C++

2010-03-02 19:00:01

   vc的stdafx文件和C1083、C1010错误  
     
  今天在改一个旧的vc6工程(为了节省空间和方便备份,以前收尾时把中间文件都删了),整理了一下文件,归了归类,结果不知怎么的,工程不能编译了,编译什么文件都出现fatal   error   C1083:   Cannot   open   precompiled   header   file:   'Debug/xxx.pch':   No   such   file   or   directory,可是我也没删什么文件,改程序内容,就是把文件挪了挪,看来是vc的工程有点问题,只好查查怎么回事。  
     
  用vc新建的工程一般都有这两个文件(空工程除外),其实如果不用mfc,这两个根本没用  
  StdAfx.cpp   就一句#include   "stdafx.h"  
  StdAfx.h 包含了一些头文件,如果没用mfc就一句有用的#include    
  要是你把哪个源文件的#include   "stdafx.h"这行删除了,或者从其他工程考过来一个文件没有这行的,编译时一律是fatal   error   C1010:   unexpected   end   of   file   while   looking   for   precompiled   header   directive,这时你只要给该文件开头加个#include   "stdafx.h"就了事。  
  这两个文件除了这点之外还有一个问题,就是这两个文件不能从工程里删除,不然你编译任何一个.cpp/.c的文件都会报fatal   error   C1083:   Cannot   open   precompiled   header   file:   'Debug/xxx.pch':   No   such   file   or   directory,你build也不行,vc会给每一个源文件报这样一个错误。  
     
  你再把这两个文件加入工程,一样不行,不论你把他们加在哪,怎么编译就是fatal   error   C1083,找不到.pch文件。只有新建一个工程,然后把原来的源文件都加过去,要是工程里有很多源文件,进行了很多设置的话,可就麻烦了。  
     
  于是,我新建了一个工程就叫test把,看看怎么会出现fatal   error   C1083的。在新建一个工程后,vc在工程目录里生成了几个文件,包括test.dsp(工程文件)、StdAfx.h、StdAfx.cpp。和主程序源文件test.cpp等。如果你build工程,会在相应的编译目录下(一般为debug或release)产生test.pch、vc60.idb、vc60.pdb、StdAfx.obj和源程序的obj、pdb文件和一个test.exe程序,fatal   error   C1083就是说没有这个.pch文件,他也不能产生这个文件。那到底vc是怎么产生这个文件的,又是作了什么改动使他不能再产生这个文件的呢?我们将debug目录删除,然后编译test.cpp文件,结果j:\test\test.cpp(4)   :   fatal   error   C1083:   Cannot   open   precompiled   header   file:   'Debug/test.pch':   No   such   file   or   directory;不行,我build:  
  --------------------Configuration:   test   -   Win32   Debug--------------------  
  Compiling...  
  StdAfx.cpp  
  Compiling...  
  test.cpp  
  Linking...  
  test.exe   -   0   error(s),   0   warning(s)  
     
  看来是编译StdAfx.cpp产生了test.pch,然后再编译test.cpp的把;再把debug目录删了,编译StdAfx.cpp就ok,看看debug目录:test.pch、StdAfx.obj、vc60.idb、vc60.pdb。原来是编译StdAfx.cpp产生的pch文件,那从工程里删除这个文件然后在添加这个文件为什么不行呢?肯定是在在删除/添加StdAfx.cpp文件过程中,工程文件不一样了。先关闭工程,把test.dsp备份,然后打开工程,删除StdAfx.cpp文件,在添加,编译,错误C1083,然后关闭工程(保存工程)。fc比较一下刚才备份的dsp和现在的工程文件,发现原来正常的那个工程文件里在SOURCE=.\StdAfx.cpp之后多这么一行#   ADD   CPP   /Yc"stdafx.h",而删了StdAfx.cpp再添加的工程就没有,其他的就完全一样。  
   
  正常的dsp文件包含StdAfx.cpp的这两行是这样的:  
  SOURCE=.\StdAfx.cpp  
  #   ADD   CPP   /Yc"stdafx.h"  
  #   End   Source   File  
   
  后记:  
  vc真是奇怪呀,这行#   ADD   CPP   /Yc"stdafx.h"在新建工程时有,从工程里删除了StdAfx.cpp就没有了,再在工程里加上这个StdAfx.cpp就没有加上。看似删除再添加好像没有动工程,谁知道他来这么一手,艾,要是懂编译懂vc的还好,要是遇上我这样的菜鸟,艾,只有和他奋斗半天才把他搞得稀里糊涂。把他写出来,免得和我一样的菜鸟再费半天劲。  
     
  其实这个pch文件是预编译头(PreCompiled   Header),下边是msdn里关于PreCompiled   Header和/Yc选项的解释。一直都是用集成编译环境的,先是tc,然后是vc,没用过make,没写过makefile,对编译、连接这些东西还不是很懂,高手见笑了。这个选项是可以在工程的设置里设的,具体可以看下边的msdn帮助。  
     
  /Yc    (Create   Precompiled   Header   File)  
  Home   | Overview   | How   Do   I   | Compiler   Options  
  This   option   instructs   the   compiler   to   create   a   precompiled   header   (.PCH)   file   that   represents   the   state   of   compilation   at   a   certain   point.   (To   find   this   option   in   the   development   environment,   click   Settings   on   the   Project   menu.   Then   click   the   C/C++   tab,   and   click   Precompiled   Headers   in   the   Category   box.)    
  Command   Line   Project   Settings   Description    
  /Yc   Create   Precompiled   Header   File   The   compiler   compiles   all   code   up   to   the   end   of   the   base   source   file,   or   to   the   point   in   the   base   file   where   #pragma   hdrstop   occurs.   
  /Ycfilename   Through   Header   The   compiler   compiles   all   code   up   to   and   including   the   .H   file   specified   in   the   Through   Header   text   box   (filename).   
   
  The   precompiled   code   is   saved   in   a   file   with   a   name   created   from   the   base   name   of   the   file   specified   with   the   /Yc   option   and   a   .PCH   extension.   You   can   also   use   the   /Fp   option   to   specify   a   name   for   the   precompiled   header   file.  
  If   you   use   /Ycfilename   (Through   Header),   the   compiler   compiles   all   code   up   to   and   including   the   specified   file   for   subsequent   use   with   the   /Yu   option.    
  Note    If   the   options   /Ycfilename   and   /Yufilename   occur   on   the   same   command   line   and   both   reference,   or   imply,   the   same   file   name,   /Ycfilename,   takes   precedence.   This   feature   simplifies   the   writing   of   makefiles.  
  Example  
  Consider   the   following   code:  
  #include       //   Include   header   for   class   library  
  #include   "resource.h"   //   Include   resource   definitions  
  #include   "myapp.h"  //   Include   information   specific   to   this   app  
  ...  
  When   this   code   is   compiled   with   the   command  
  CL   /YcMYAPP.H   PROG.CPP  
  the   compiler   saves   all   the   preprocessing   for   AFXWIN.H,   RESOURCE.H,   and   MYAPP.H   in   a   precompiled   header   file   called   MYAPP.PCH.  
  See   Also    Creating   Precompiled   Header   Files  
阅读(1876) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~