问题描述
总的来说,我对 C++ 和 Eclipse 还很陌生,所以如果我遗漏了一些相当明显的东西,我深表歉意.
I'm pretty new to C++ and Eclipse in general so I apologise if I'm missing something fairly obvious.
我遇到的问题是我试图在我的一个源文件中包含一个头文件,但它们位于我的项目目录中的不同文件夹中.我不知道我应该如何包括他们.我上传了一张图片,显示了我要突出显示的头文件的问题.
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.
如果有人能告诉我应该使用什么#include"语句,那就太好了.
If someone could tell me what '#include' statement I should be using them that would be brilliant.
谢谢!
推荐答案
有几个不同的选项可以完成这项工作.最简单的是将 #include
改为
There are a couple of different options to make this work. Simplest is to change the #include
to
#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.
或者,您可以将 Statistics
文件夹的路径添加到编译器的包含文件搜索路径中.右键单击项目名称,选择 Properties -> C/C++ Build -> Settings,然后找到编译器的包含文件路径选项.对于 g++,它是 -I
.添加这将使 #include
语句像您当前拥有的那样工作.
Alternately, you can add the path to the Statistics
folder to your compiler's include file search path. Right click on the project name, select Properties -> C/C++ Build -> Settings and then find the includes files path option for your compiler. For g++, it is -I<path/to/include/folder>
. Adding this will make the #include
statement work as you currently have it.
与第二个非常相似的选项是将 src
文件夹的路径(而不是 Statistics
文件夹)添加到包含搜索路径.在这种情况下,您必须将语句更改为
A very similar option to the second one is to add the path to the src
folder (instead of the Statistics
folder) to the includes search path. In this case, you'll have to change the statement to
#include "Statistics/Statistics.h"
这篇关于Eclipse C++ 包括来自我的源文件夹的头文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!