C++ 如何在 Visual Studio 2008 中包含头文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4702302/
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
How to include header files in Visual Studio 2008?
提问by Sergio
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. THe problem is that I see the file included in the Header's folder and in the code I have written:
我目前正在尝试编译一个包含两个头文件的简单程序。我在解决方案资源管理器中看到它们,在那里我通过“包含现有文件”将它们包含在内。但是,当我运行我的程序时,它会出现以下错误。致命错误 C1083:无法打开包含文件:“FileWrite.h”:没有这样的文件或目录。问题是我看到包含在 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 文件?
回答by Bojan Komazec
If you write in your code something like #include "FileWrite.h"
you need to make sure compiler can find that file. There are three options:
如果您在代码中编写类似的内容#include "FileWrite.h"
,则需要确保编译器可以找到该文件。共有三个选项:
- FileWrite.h should either be in the same directory as your source code file (.cpp) or
- Path to that header file should should be listed in project's Properties (in C/C++ -> General -> Additional Include Directories) or
- Path could be set in your VisualStudio - add it to Include Filesin Tools->Options->Projects and Solutions->VC++ Directories
- FileWrite.h 应该与源代码文件 (.cpp) 位于同一目录中,或者
- 该头文件的路径应该列在项目的属性中(在C/C++ -> General -> Additional Include 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).
使用这些选项 shell 中的哪一个取决于该标题是来自该项目(第一个选项)还是其他项目(其他两个选项中的任何一个)。
回答by DumbCoder
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,因为在与您不同的系统上编译项目时,很难确定项目的依赖关系。
回答by Motti
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. If FileWrite.h
isn't in the same directory as your source file check the additional included directories.
当包含文件时,编译器首先在当前目录(包含源.cpp
文件的目录)中查找,然后在其他包含目录中查找。如果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
。
回答by Edward Strange
You said the file is in the "headers" folder. This could either mean the headers filteror 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:
您说该文件位于“headers”文件夹中。这可能意味着标头过滤器或文件系统上的实际标头目录。当包含您自己的项目中的文件时,您需要指定要包含到的文件的路径。所以,如果你有这样的事情:
src/main.cpp
include/my_object.h
You would use #include "../include/my_object.h"
in main.cpp.
您将#include "../include/my_object.h"
在 main.cpp 中使用。
That's for directories. The folders you see in your project are called filtersand 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 资源管理器中的结构是什么样的,以确定在包含语句中使用的路径。