C++ 无法使用 Visual Studio 打开包含文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19415521/
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 22:47:23  来源:igfitidea点击:

Cannot open include file with Visual Studio

c++visual-studiovisual-studio-2012includecodeblocks

提问by user2853108

I have recently gone from Code::Blocksto Visual Studio, and in Code::Blocks one could just add a class and then include it straight away. However, whenever I do the same in Visual Studio with the following statement:

我最近从Code::Blocks转到了 Visual Studio,在 Code::Blocks 中可以添加一个类,然后立即包含它。但是,每当我使用以下语句在 Visual Studio 中执行相同操作时:

#include "includedFile.h"

or

或者

#include "include/includedFile.h"

It doesn't work and instead I get the error:

它不起作用,而是出现错误:

cannot open include file: 'includedFile.h'; no such file or directory.

无法打开包含文件:'includedFile.h'; 无此文件或目录。

Is there some box or setting that I have to tick? Or do I have to add each header as a dependency manually?

是否有一些我必须勾选的框或设置?还是我必须手动将每个标头添加为依赖项?

Here is the code for the class in question:

这是相关类的代码:

Public.h:

公共.h:

    #pragma once
    class Public
    {
        public:
            static const int SCREEN_WIDTH=1000;
            static const int SCREEN_HEIGHT=1250;
            Public(void);
            ~Public(void);
    };

Public.cpp:

公共.cpp:

    #include "Public.h"


    Public::Public(void)
    {
    }


    Public::~Public(void)
    {
    }

How it is being included:

它是如何被包括在内的:

    #include "Public.h"

回答by Yablargo

I had this same issue going from e.g gcc to visual studio for C programming. Make sure your include file is actually in the directory-- not just shown in the VS project tree. For me in other languages copying into a folder in the project tree would indeed move the file in. With Visual Studio 2010, pasting into "Header Files" was NOT putting the .h file there.

我遇到了同样的问题,例如从 gcc 到 Visual Studio 进行 C 编程。确保您的包含文件确实在目录中——而不仅仅是显示在 VS 项目树中。对于我在其他语言中复制到项目树中的文件夹确实会将文件移入。使用 Visual Studio 2010,粘贴到“头文件”中并没有将 .h 文件放在那里。

Please check your actual directory for the presence of the include file. Putting it into the "header files" folder in project/solution explorer was not enough.

请检查您的实际目录是否存在包含文件。将其放入项目/解决方案资源管理器中的“头文件”文件夹是不够的。

回答by Radu Ruse

Go to your Project properties (Project -> Properties -> Configuration Properties -> C/C++ -> General) and in the field Additional Include Directories add the path to your .h file.

转到您的项目属性(项目 -> 属性 -> 配置属性 -> C/C++ -> 常规)并在字段附加包含目录中添加到您的 .h 文件的路径。

And be sure that your Configuration and Platform are the active ones. Example: Configuration: Active(Debug) Platform: Active(Win32).

并确保您的配置和平台是活动的。示例:配置:活动(调试)平台:活动(Win32)。

回答by Chris Olsen

You need to set the path for the preprocessor to search for these include files, if they are not in the project folder.

您需要设置预处理器的路径以搜索这些包含文件,如果它们不在项目文件夹中。

You can set the path in VC++ Directories, or in Additional Include Directories. Both are found in project settings.

您可以在VC++ DirectoryAdditional Include Directories 中设置路径。两者都可以在项目设置中找到。

回答by tomi.lee.jones

By default, Visual Studio searches for headers in the folder where your project is ($ProjectDir) and in the default standard libraries directories. If you need to include something that is not placed in your project directory, you need to add the path to the folder to include:

默认情况下,Visual Studio 在项目所在的文件夹 ($ProjectDir) 和默认标准库目录中搜索标头。如果你需要包含一些没有放在你的项目目录中的东西,你需要添加文件夹的路径来包含:

Go to your Project properties (Project -> Properties -> Configuration Properties -> C/C++ -> General) and in the field Additional Include Directoriesadd the path to your .h file.

转到您的项目属性(项目 -> 属性 -> 配置属性 -> C/C++ -> 常规)并在字段中Additional Include Directories添加 .h 文件的路径。

You can, also, as suggested by Chris Olen, add the path to VC++ Directories field.

您也可以按照 Chris Olen 的建议,将路径添加到 VC++ 目录字段。

回答by Joe

I found this post because I was having the same error in Microsoft Visual C++. (Though it seems it's cause was a little different, than the above posted question.)

我发现这篇文章是因为我在 Microsoft Visual C++ 中遇到了同样的错误。(虽然看起来它的原因与上面发布的问题有点不同。)

I had placed the file, I was trying to include, in the same directory, but it still could not be found.

我已将文件放置在同一目录中,我试图将其包含在内,但仍然找不到。

My include looked like this: #include <ftdi.h>

我的包含看起来像这样: #include <ftdi.h>

But When I changed it to this: #include "ftdi.h"then it found it.

但是当我把它改成这样: #include "ftdi.h"然后它就找到了。

回答by mkohler

For me, it helped to link the projects current directory as such:

对我来说,它有助于链接项目当前目录:

In the properties -> C++ -> General window, instead of linking the path to the file in "additional include directories". Put "." and uncheck "inheret from parent or project defaults".

在属性 -> C++ -> 常规窗口中,而不是将路径链接到“附加包含目录”中的文件。放 ”。” 并取消选中“从父级或项目默认值继承”。

Hope this helps.

希望这可以帮助。

回答by Patapoom

If your problem is still there it's certainly because you are trying to compile a different version from your current settings.

如果您的问题仍然存在,那肯定是因为您正在尝试编译与当前设置不同的版本。

For example if you set your Additional Include Directoriesin Debug x64, be sure that you are compiling with the same configuration.

例如,如果您Additional Include DirectoriesDebug x64 中设置,请确保您使用相同的配置进行编译。

Check this: Build > Configuration Manager... >There is problably something like this in your active solution configuration: Debug x86 (Win32) platform.

检查这个:Build > Configuration Manager... >在您的活动解决方案配置中可能有这样的东西:Debug x86 (Win32) platform

回答by cowlinator

If you've tried the other answers and your include file still can't be found, here are some additional debugging steps and sanity-checks:

如果您尝试了其他答案,但仍然找不到包含文件,这里有一些额外的调试步骤和健全性检查:

  • Ensure that you are building to a platform that is supported by your code. (If not, consider removing this platform as a target)
  • Verify that the filename/path is correct. Modify your source code to #includethe whole absolute path of the header file instead, and see if the file can be found now. If not, copy-paste the path from your source code into a command line to validate that the file exists at that full path with no typos. Open the header file to ensure you have read access. (Change the source code back when done.)
  • If you've already added the path to Additional Include Directories, try clicking the drop-down combo box for Additional Include Directories, and select <Edit...>. This will show you evaluated valuesof paths. (If it does not show the correct evaluated values, variables in your path might not be set. Click Macros>>to see variables.) Copy-paste the evaluated path into windows explorer to validate that the path exists.
  • Create a new empty C++ "Windows Console Application" project. Set just the one Include Directory, and #include just the one file in your main.cpp, and see if that builds.
  • 确保您正在构建一个受您的代码支持的平台。(如果没有,请考虑删除此平台作为目标)
  • 验证文件名/路径是否正确。将你的源代码修改#include为头文件的整个绝对路径,看看现在是否可以找到该文件。如果没有,请将源代码中的路径复制粘贴到命令行中,以验证该文件是否存在于该完整路径中且没有拼写错误。打开头文件以确保您具有读取权限。(完成后将源代码改回。)
  • 如果您已将路径添加到Additional Include Directories,请尝试单击 的下拉组合框Additional Include Directories,然后选择<Edit...>。这将向您显示evaluated values路径。(如果它没有显示正确的评估值,则您的路径中的变量可能未设置。单击Macros>>以查看变量。)将评估的路径复制粘贴到 Windows 资源管理器中以验证该路径是否存在。
  • 创建一个新的空 C++“Windows 控制台应用程序”项目。在 main.cpp 中只设置一个包含目录,#include 只设置一个文件,然后看看是否可以构建。