C++ Visual Studio 无法“看到”我包含的头文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12490015/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 16:19:51  来源:igfitidea点击:

Visual Studio can't 'see' my included header files

c++visual-studio-2008include

提问by Nabijon

I created an empty 'Demo' project in Visual Studio 2008 and added some existing projects to my solution. Included "MyHeader.h" (other project's header) in main.cpp file which is in 'Demo'. Also added header files' path in "Tools/Option/VC++ Directories/Include files" section. But intellisense says: "File 'MyHeader.h' not found in current source file's directory or in build system paths..."

我在 Visual Studio 2008 中创建了一个空的“演示”项目,并将一些现有项目添加到我的解决方案中。在“演示”中的 main.cpp 文件中包含“MyHeader.h”(其他项目的标题)。还在“工具/选项/VC++ 目录/包含文件”部分添加了头文件的路径。但是智能感知说:“在当前源文件的目录或构建系统路径中找不到文件‘MyHeader.h’……”

How the problem can be fixed? Thanks.

如何解决问题?谢谢。

采纳答案by TimothyHeyden

If you choose Projectand then All Filesin the menu, all files should be displayed in the Solution Explorer that are physically in your project map, but not (yet) included in your project. If you right click on the file you want to add in the Solution Explorer, you can include it.

如果您选择Project然后All Files在菜单中,所有文件都应该显示在解决方案资源管理器中,这些文件物理上位于您的项目地图中,但(尚未)包含在您的项目中。如果在解决方案资源管理器中右键单击要添加的文件,则可以将其包含在内。

回答by aguazales

Delete the .sdf file that is in your solution directory. It's just the Intellisense database, and Visual Studio will recreate it the next time you open that solution. This db can get corrupted and cause the IDE to not be able to find things, and since the compiler generates this information for itself on the fly, it wouldn't be affected.

删除解决方案目录中的 .sdf 文件。它只是 Intellisense 数据库,Visual Studio 将在您下次打开该解决方案时重新创建它。此数据库可能会损坏并导致 IDE 无法找到内容,而且由于编译器会即时为自己生成此信息,因此不会受到影响。

回答by Elliot

This happened to me just now, after shutting down and restarting the computer. Eventually I realised that the architecture had somehow been changed to ARM from x64.

刚刚关闭并重新启动计算机后,这发生在我身上。最终我意识到架构不知何故从 x64 更改为 ARM。

回答by MultiVAC

If it is the case that only the IDE indicates that it cannot find included files, but compiling is successful, the issue is simply that IntelliSense is not fully up to date with recent changes. This can happen specifically when including existing projects, in my own experience. Deleting the .sdf file (= IntelliSense database) that is generated in your solution directory forces Visual Studio to regenerate it, so that it is up to date again. Just doing a "clean" will probably do the same thing, but takes more time since everything will be generated again then.

如果只有 IDE 指示它找不到包含文件,但编译成功,则问题只是 IntelliSense 没有完全更新最近的更改。根据我自己的经验,当包括现有项目时,这可能会发生。删除在解决方案目录中生成的 .sdf 文件(= IntelliSense 数据库)会强制 Visual Studio 重新生成它,以便它再次更新。只是做一个“清理”可能会做同样的事情,但需要更多的时间,因为一切都会再次生成。

回答by goug

I know this is an older question, but none of the above answers worked for me. In my case, the issue turned out to be that I had absolute include paths but without drive letters. Compilation was fine, but Visual Studio couldn't find an include file when I right-clicked and tried to open it. Adding the drive letters to my include paths corrected the problem.

我知道这是一个较旧的问题,但上述答案都不适合我。就我而言,问题是我有绝对包含路径但没有驱动器号。编译很好,但是当我右键单击并尝试打开它时,Visual Studio 找不到包含文件。将驱动器号添加到我的包含路径纠正了问题。

I would never recommend hard-coding drive letters in any aspect of your project files; either use relative paths, macros, environment variables, or some mix of the tree for any permanent situation. However, in this case, I'm working in some temporary projects where absolute paths were necessary in the short term. Not being able to right-click to open the files was extremely frustrating, and hopefully this will help others.

我永远不会在项目文件的任何方面推荐硬编码驱动器号;对于任何永久情况,要么使用相对路径、宏、环境变量或树的某种组合。但是,在这种情况下,我正在一些临时项目中工作,短期内需要绝对路径。无法右键单击打开文件非常令人沮丧,希望这会对其他人有所帮助。

回答by john elemans

In my experience, with VS2010, when include files can't be found at compile time, doing a clean, then build usually fixes the problem. It's not that rare for the editor to be able to open an include file and then the compiler to announce that it can't find that very file, even when it is open on the screen!

根据我的经验,在 VS2010 中,当在编译时找不到包含文件时,进行清理,然后构建通常可以解决问题。编辑器能够打开一个包含文件然后编译器宣布它找不到那个文件的情况并不少见,即使它在屏幕上打开也是如此!

回答by Sam Liu

If the visual studio says that you miss some file in the current source file folder, there is one solution that i used. Just right click the file you want to add and choose Open Document, if it really doesn't exist, then you should see something like cannot find file in the source file path = "somewhere in your computer", then what you could do is the add your source file into that path first and see if it works.

如果 Visual Studio 说您错过了当前源文件夹中的某个文件,我使用了一种解决方案。只需右键单击您要添加的文件并选择Open Document,如果它确实不存在,那么您应该看到类似的内容cannot find file in the source file path = "somewhere in your computer",然后您可以先将源文件添加到该路径中,看看它是否有效。

回答by Ivaylo Strandjev

Try adding the header file to your project's files. (right click on project -> add existing file).

尝试将头文件添加到您的项目文件中。(右键单击项目-> 添加现有文件)。

回答by abe312

Here's how I solved this problem.

这是我解决这个问题的方法。

  • Go to Project --> Show All Files.
  • 转到项目 --> 显示所有文件。

enter image description here

在此处输入图片说明

  • Right click all the files in Solutions Explorer and Click on Include in Project in all the files you want to include.
  • 右键单击解决方案资源管理器中的所有文件,然后在要包含的所有文件中单击包含在项目中。

enter image description here

在此处输入图片说明

Done :)

完毕 :)

回答by Darren

I encountered this issue, but the solutions provided didn't directly help me, so I'm sharing how I got myself into a similar situation and temporarily resolved it.

我遇到了这个问题,但是提供的解决方案并没有直接帮助我,所以我分享了我如何让自己陷入类似情况并暂时解决它。

I created a new project within an existing solution and copy & pasted the Header and CPP file from another project within that solution that I needed to include in my new project through the IDE. Intellisense displayed an error suggesting it could not resolve the reference to the header file and compiling the code failed with the same error too.

我在现有解决方案中创建了一个新项目,并从该解决方案中的另一个项目中复制并粘贴了 Header 和 CPP 文件,我需要通过 IDE 将其包含在我的新项目中。Intellisense 显示一个错误,表明它无法解析对头文件的引用,并且编译代码也失败并出现相同的错误。

After reading the posts here, I checked the project folder with Windows File Explorer and only the main.cppfile was found. For some reason, my copy and paste of the header file and CPP file were just a reference? (I assume) and did not physically copy the file into the new project file.

阅读这里的帖子后,我使用 Windows 文件资源管理器检查了项目文件夹,只找到了main.cpp文件。出于某种原因,我对头文件和 CPP 文件的复制和粘贴只是参考?(我假设)并且没有将文件物理复制到新的项目文件中。

I deleted the files from the Project view within Visual Studio and I used File Explorer to copy the files that I needed to the project folder/directory. I then referenced the other solutions posted here to "include files in project"by showing all files and this resolved the problem.

我从 Visual Studio 的项目视图中删除了文件,并使用文件资源管理器将我需要的文件复制到项目文件夹/目录中。然后我通过显示所有文件引用了此处发布的其他解决方案以“在项目中包含文件”,这解决了问题。

It boiled down to the files not being physically in the Project folder/directory even though they were shown correctly within the IDE.

它归结为文件不在项目文件夹/目录中,即使它们在 IDE 中正确显示。

Please NoteI understand duplicating code is not best practice and my situation is purely a learning/hobby project. It's probably in my best interest and anyone else who ended up in a similar situation to use the IDE/project/Solution setup correctly when reusing code from other projects - I'm still learning and I'll figure this out one day!

请注意,我明白复制代码不是最佳实践,我的情况纯粹是一个学习/爱好项目。在重用来自其他项目的代码时,正确使用 IDE/项目/解决方案设置可能符合我的最大利益和其他任何最终处于类似情况的人 - 我仍在学习,有一天我会解决这个问题!