問題描述
總的來說,我對 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++ 包括來自我的源文件夾的頭文件的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!