如何包含位于特定文件夹中的头文件?(C++)

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

How do I include a header file located in a specific folder? (C++)

c++fileheaderinclude

提问by Ben

I want to include a specific header file (MyHeader.h) in a C++ project. The solution for my project is located in the folder:

我想在 C++ 项目中包含一个特定的头文件 (MyHeader.h)。我的项目的解决方案位于文件夹中:

C:\Projects\MyProgram

The header file is located in the folder:

头文件位于以下文件夹中:

C:\Projects\MyProgram\Files

I tried the following line of code, but it doesn't work.

我尝试了以下代码行,但它不起作用。

#include <Files\MyHeader.h>

Is there an easy way to include the header file without adding the full path to "Include directories" in the configuration properties?

有没有一种简单的方法来包含头文件,而无需在配置属性中添加“包含目录”的完整路径?

Thanks in advance for any help. :)

在此先感谢您的帮助。:)

采纳答案by gldraphael

Try this

尝试这个

#include "files/myheader.h"

It will work if the header is in a filesfolder in the same directory as the current source.

如果标题files与当前源位于同一目录中的文件夹中,它将起作用。



If you're trying to include a 3rd party library and not your own header, I'd suggest you to save the library headers in a particular path (say C:\Library\headers). (If there are static libraries put them in some other path like C:\Library\lib).

如果您尝试包含第 3 方库而不是您自己的标头,我建议您将库标头保存在特定路径中(例如C:\Library\headers)。(如果有静态库,请将它们放在其他路径中,例如C:\Library\lib)。

  1. In your Visual Studio C++ Project, go to View > Other Windows > Property Manager.
  1. 在 Visual Studio C++ 项目中,转到View > Other Windows > Property Manager.

Property Manager Window

属性管理器窗口

  1. Double Click on the Project Name. You will see a dialog box like this:
  1. 双击项目名称。你会看到一个这样的对话框:

Property Page Editor

属性页编辑器

Make sure All Configurationsis chosen in the dropdown, if you want the change to be applied to both the Debug and the Release Configurations. Else just choose the Configuration you want the properties to be applied to.

All Configurations如果您希望更改同时应用于调试和发布配置,请确保在下拉列表中选择 。否则只需选择要应用属性的配置。

  1. Go to VC++ Directories on the left and choose Include Directories in the right, and enter the path(s) in the textbox separated by a ;.
  1. 转到左侧的 VC++ 目录并选择右侧的包含目录,然后在文本框中输入路径,以;.

You can also use the drop down and use the Dialog box to add the paths if you'd prefer to browse to each path separately

如果您希望分别浏览到每个路径,也可以使用下拉菜单并使用对话框添加路径

Edit1Edit2

编辑 1编辑2

  1. Add the library path the same way to Library Directories
  2. Save the changes using the Save button on the Property Manager Pane's toolbox.
  1. 以相同的方式添加库路径 Library Directories
  2. 使用属性管理器窗格工具箱上的保存按钮保存更改。


You will then be able to access the header file contained in the directory you added by something like:

然后,您将能够访问包含在您添加的目录中的头文件,例如:

#include <myheader.h>

This approach will help, because it won't matter where the headers saved. The header path is not hard-coded.

这种方法会有所帮助,因为标题保存在哪里无关紧要。标头路径不是硬编码的。



回答by CashCow

The current directory of the source file is always searched, although if you use angled brackets it is searched after your include path, whilst if you use quotes it will be the first directory searched.

始终搜索源文件的当前目录,但如果您使用尖括号,它将在您的包含路径之后搜索,而如果您使用引号,它将成为搜索的第一个目录。

The directory of your solution or makefile/project file is irrelevant, the local path is relative to the compilation unit, i.e. the cpp file.

您的解决方案或makefile/project 文件的目录无关紧要,本地路径是相对于编译单元的,即cpp 文件。

If that cpp file includes a header, that headers own includes are relative to itself, not the cpp file that included it. (It would be hell to manage if it were not).

如果该 cpp 文件包含一个头文件,则该头文件所包含的内容是相对于自身的,而不是包含它的 cpp 文件。(如果不是这样管理将是地狱)。

Ideally you should use forward slashes in paths too.

理想情况下,您也应该在路径中使用正斜杠。

Your actual correct setup here is to include the solution directory in your search path. If it is Visual Studio you can use a macro for this, $(SolutionDir)I think.

此处实际正确的设置是在搜索路径中包含解决方案目录。$(SolutionDir)我认为,如果是 Visual Studio,您可以为此使用宏。

That means that if anyone else is going to build your solution, they can put it in a directory they choose and as long as the structure underneath is the same, it will still work.

这意味着如果其他人要构建您的解决方案,他们可以将它放在他们选择的目录中,只要下面的结构相同,它仍然可以工作。

To use a relative path in your cpp file without any include directory settings, you might need something like:

要在没有任何包含目录设置的情况下在 cpp 文件中使用相对路径,您可能需要以下内容:

#include "../Files/MyHeader.h"

回答by Erwin R Panganiban

You just need to replace your brackets <>with double quotes ""like this:

你只需要像这样<>用双引号替换你的括号""

#include "Files\MyHeader.h"

Brackets is used when you want Visual Studio to find the path from your project settings and double quotes when you want to access the header from a specific path or relative to your project.

当您希望 Visual Studio 从您的项目设置中查找路径时使用方括号,当您希望从特定路径或相对于您的项目访问标头时使用双引号。