简体   繁体   English

如何在Visual Studio 2008中包含头文件?

[英]How to include header files in Visual Studio 2008?

I am currently trying to compile a simple program that includes two header files. 我目前正在尝试编译一个包含两个头文件的简单程序。 I see them in the Solution Explorer, where I included them through "include existing files". 我在解决方案资源管理器中看到它们,我通过“包含现有文件”将它们包括在内。 However, when I run my program it get the following error. 但是,当我运行我的程序时,它会收到以下错误。 fatal error C1083: Cannot open include file: 'FileWrite.h': No such file or directory. 致命错误C1083:无法打开包含文件:'FileWrite.h':没有这样的文件或目录。 THe problem is that I see the file included in the Header's folder and in the code I have written: 问题是我看到Header的文件夹和我编写的代码中包含的文件:

#include "FileWrite.h"

and then the rest of the program code. 然后是程序代码的其余部分。 Is there something else needed to do so that the compiler can see the header file and link it to the .cpp file I'm trying to compile? 是否有其他需要这样做,编译器可以看到头文件并将其链接到我正在尝试编译的.cpp文件?

If you write in your code something like #include "FileWrite.h" you need to make sure compiler can find that file. 如果您在代码中写入类似#include "FileWrite.h" ,则需要确保编译器可以找到该文件。 There are three options: 有三种选择:

  • FileWrite.h should either be in the same directory as your source code file (.cpp) or FileWrite.h应与源代码文件(.cpp)位于同一目录中
  • Path to that header file should should be listed in project's Properties (in C/C++ -> General -> Additional Include Directories ) or 应该在项目的属性中列出该头文件的路径(在C / C ++ - >常规 - >其他包含目录中 )或
  • Path could be set in your VisualStudio - add it to Include Files in Tools->Options->Projects and Solutions->VC++ Directories 可以在VisualStudio中设置路径 - 将其添加到工具中的 包含文件 - >选项 - >项目和解决方案 - > VC ++目录

Which of these options shell be used depends on whether that header originates from this project (1st option) or some other project (any of other two options). 使用以下哪个选项取决于该标头是来自此项目(第一个选项)还是某个其他项目(任何其他两个选项)。

There are two ways to do this. 有两种方法可以做到这一点。

1) Only for the current project 1)仅适用于当前项目

Select your project -> properties -> C/C++ -> General -> Additional Include Directories -
Include your header file directory.

2) For all projects 2)对于所有项目

Tools -> Options -> VC++ Directories -> Include files - Add the header file directory.

Refrain from using 2, as it would be difficult to figure out dependencies for a project when compiling it on a system different than yours. 避免使用2,因为在不同于您的系统上编译项目时很难找出项目的依赖关系。

When including files the compiler first looks in the current directory (the directory which contains the source .cpp file) then it looks in the additional include directories. 当包含文件时,编译器首先查找当前目录(包含源.cpp文件的目录),然后查找其他包含目录。 If FileWrite.h isn't in the same directory as your source file check the additional included directories. 如果FileWrite.h与源文件不在同一目录中,请检查其他包含的目录。

In the project's property page look at the additional include directories and see if they include the folder in which FileWrite.h is in. 在项目的属性页面中,查看其他包含目录,并查看它们是否包含FileWrite.h所在的文件夹。

You said the file is in the "headers" folder. 你说文件在“headers”文件夹中。 This could either mean the headers filter or an actual headers directory on the filesystem. 这可能意味着头文件过滤器或文件系统上的实际头文件目录。 When including a file from your own project you need to specify the path from the file you're including into. 从您自己的项目中包含文件时,您需要指定您所包含文件的路径。 So, if you had something like so: 所以,如果你有这样的事情:

src/main.cpp
include/my_object.h

You would use #include "../include/my_object.h" in main.cpp. 你可以在main.cpp中使用#include "../include/my_object.h"

That's for directories. 这是目录。 The folders you see in your project are called filters and have absolutely no relation to the directory structure of your project unless you force it to. 您在项目中看到的文件夹称为过滤器 ,除非您强制使用,否则与项目的目录结构完全无关。 You need to be paying attention to what the structure looks like in windows explorer to ascertain what path to use in an include statement. 您需要注意Windows资源管理器中的结构,以确定在include语句中使用的路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何强制Visual Studio 2008警告缺少头文件 - How to force Visual Studio 2008 to warn about missing header files 如何在Visual Studio中的所有cpp文件中自动包含标头? - How to include an header automatically in all cpp files in visual studio? 不能在visual studio 2008中包含std头文件 - Cannot include std header file in visual studio 2008 如何在Visual Studio中包含头文件? - How to include a header file in Visual Studio? 如何将cpp文件添加到Visual Studio 2008? - How to add cpp files to Visual Studio 2008? 如何在Visual Studio 2008中使用.a lib文件? - How to use .a lib files with Visual Studio 2008? Visual Studio 在其他包含文件夹中找不到头文件 - Visual studio cannot find the header files in Additional Include folders 如何将带有头文件的外部文件夹(包含文件夹)添加到 Visual Studio 项目? - How to add an external folder with header files (include folder) to a Visual Studio project? 如何使 Visual Studio 2008 IDE 查看 header 文件的正确位置。 编译 Qt/C++ 程序 - How to make the visual studio 2008 IDE look at the right place for header files. Compiling Qt/C++ program 如何在Visual Studio 2008中使用inl文件创建静态库? - How to create a static library in Visual Studio 2008 with inl files?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM