IntelliSense: 无法打开 源 文件 "stdafx.h"

来源:互联网 发布:股票数据接口 刷新 编辑:程序博客网 时间:2024/05/16 15:33

VS2010中包含以前的.h/.cpp文件于现在的工程中,出现IntelliSense: 无法打开 源 文件 "stdafx.h",搜寻到一些方法都不适用,比如:设置项目属性->配置属性->C/C++->预编译头->使用 (/Yu)/创建 (/Yc)/不使用预编译头三种方式都不行。虽然提示这样的错误,但是运行程序是成功的,初始化和编译运行好像使用不同的查找路径,在低版本的VS2005/VS2008开发而在高版本VS2010中打开时会遇到这样的问题。其解决办法是:

            项目属性->配置属性->C/C++->常规->附加包含目录->$(ProjectDir)


http://blog.sina.com.cn/s/blog_8216ada701017jcj.html


  • 关于 visual studio 2010 intellisense cannot open source file "stdafx.h" 的解决方法 - [编程]

    版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明
    http://www.blogbus.com/deepvoid-logs/262776518.html

    下午将近要离开实验室的时候,我尝试着使用 namespace 来把我自己的代码纳入统一的命名空间,以免和其它同名的全局函数产生冲突,另外还新建了一个 calib3d 的 .cpp,新建完之后发现自己新建立的 .h 和 .cpp 都在工程目录下,包括之前新建的 typedef 的 .cpp,于是想当然的在系统目录下新建了个新的专属自己代码库的文件夹,并把所有自己的 .cpp 和 .h 都挪了进去,这一挪可就出问题了,自己代码所有的 .cpp 文件中 #include "stdafx.h" 都被 IDE 的 IntelliSense 提示说 cannot open source file "stdafx.h",.cpp 里要是真不能 include stdafx.h 也就意味着基本上工程中定义的所有的 struct class 等都不能识别了,都是 undefined 的,但是令人困惑的是 compile 能顺利通过,仅仅是 IntelliSense 的各种提示功能不能正常工作,不能识别各种结构,本来想这样的话,干脆别深究,就将就着算去,但是后来看看那一道道提示 error 的红线感觉实在闹心,就决心一定要查个明白,google 了老半天才终于在下面这个 msdn 的 blog 中找到了答案:

    http://blogs.msdn.com/b/vcblog/archive/2010/02/19/improving-c-intellisense-performance-with-pch.aspx?PageIndex=2

    其中 Windows C++ Team 的 Ulzii Luvsanbat 这样解释道:

    The reason why build compiler succeeded when intellisense failed on #include "stdafx.h" is because you were still using PCH for the project and the new file inherited that property.  So, when build compiler sees #include "stdafx.h" it automatically loads the .pch file without doing any look-up for the actual header file.  Once PCH is loaded, all symbols resolve and build succeeds.  If you turn-off PCH for the project and do a Re-Build, you will see a compiler error in the output that's same as the Intellisense squiggle error.

    When you add #include "stdafx.h" to any file the compiler or intellisense parser will follow the lookup heuristic of searching current directory and then searching INCLUDE path.  In both cases stdafx.h will not found because it's in the $(ProjectDir) folder and your source is in a different folder. One easy way fix this in your project would be to add $(ProjectDir) to your Include Directories under "VC++ Directories" property.

    他的意思简单点说就是,当 .cpp 文件不在工程目录中时,IntelliSense Parser 就会在 .cpp 当前的文件夹中去找 .cpp 中所包含的 stdafx.h,结果发现找不到,然后接着又在 INCLUDE path 里面去找,发现也找不到,因为 stdafx.h 是在工程目录下的,当 .cpp 和  stdafx.h 同在工程目录中时,这就没有问题,但一旦不在一个目录 IntelliSense Parser 就会找不到 stdafx.h 了,所以当我把之前在工程目录中的 typedef.cpp 挪过来的时候会出现这样的问题,而之前都没有,解决方法就是在 "VC++ Directories" 的 Include Directories 中添加 $(ProjectDir),这个路径应该就是工程自己的路径了。经过自己一试发现问题果然没了,problem solved。

http://www.blogbus.com/deepvoid-logs/262776518.html



0 0
原创粉丝点击