Eclipse C++ 包括来自我的源文件夹的头文件

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

Eclipse C++ including header file from my source folder

c++eclipseheaderincludeinclude-path

提问by Joseph Little

I'm pretty new to C++ and Eclipse in general so I apologise if I'm missing something fairly obvious.

总的来说,我对 C++ 和 Eclipse 还很陌生,所以如果我遗漏了一些相当明显的东西,我深表歉意。

The problem I'm having is that I'm trying to include a header file in one of my source files but they're in different folders in my project directory. I have no idea how I should be including them. I've uploaded an image showing my problem with the header file I want to include highlighted.

我遇到的问题是我试图在我的一个源文件中包含一个头文件,但它们位于我的项目目录中的不同文件夹中。我不知道我应该如何将它们包括在内。我上传了一张图片,显示了我想突出显示的头文件的问题。

enter image description here

在此处输入图片说明

If someone could tell me what '#include' statement I should be using them that would be brilliant.

如果有人能告诉我什么 '#include' 语句我应该使用它们,那将是非常棒的。

Thanks!

谢谢!

回答by Praetorian

There are a couple of different options to make this work. Simplest is to change the #includeto

有几个不同的选项可以使这项工作发挥作用。最简单的是改变#include

#include "../Statistics/Statistics.h"

This will work without any other modifications. However, if you move either file, or somehow change the relative path between the two, this will break.

这将无需任何其他修改即可工作。但是,如果您移动任一文件,或以某种方式更改两者之间的相对路径,这将中断。

Alternately, you can add the path to the Statisticsfolder to your compiler's include file search path. Right click on the project name, select Properties -> C/C++ Build -> Settingsand then find the includes files path option for your compiler. For g++, it is -I<path/to/include/folder>. Adding this will make the #includestatement work as you currently have it.

或者,您可以将Statistics文件夹的路径添加到编译器的包含文件搜索路径中。右键单击项目名称,选择Properties -> C/C++ Build -> Settings,然后找到编译器的包含文件路径选项。对于 g++,它是-I<path/to/include/folder>. 添加这将使#include语句像您当前拥有的那样工作。

A very similar option to the second one is to add the path to the srcfolder (instead of the Statisticsfolder) to the includes search path. In this case, you'll have to change the statement to

与第二个非常相似的选项是将src文件夹(而不是Statistics文件夹)的路径添加到包含搜索路径。在这种情况下,您必须将语句更改为

#include "Statistics/Statistics.h"

回答by Dunes

When you create subfolders in your src folder then each cpp file is compiled in that folder it is located in. Thus, any ""includes need to specify the relative path to get from that folder to another.

当您在 src 文件夹中创建子文件夹时,每个 cpp 文件都会在它所在的文件夹中编译。因此,任何""包含都需要指定从该文件夹到另一个文件夹的相对路径。

In your case, to get from inside the FileInOut folder you need to go back one level and then into the Statistics folder

在您的情况下,要从 FileInOut 文件夹中获取,您需要返回一级,然后进入 Statistics 文件夹

eg

例如

#include "../Statistics/Statistics.h"

Another alternative is, if you are keeping your includes in your src directory, to add the src directory to the include path. Now when you include you need only specify the path from the src root.

另一种选择是,如果您将包含保留在 src 目录中,则将 src 目录添加到包含路径中。现在,当您包含时,您只需要指定来自 src 根目录的路径。

eg.

例如。

#include "Statistics/Statistics.h"