2012-04-25 118 views
0

我有一個目錄maths這是一個僅由頭文件組成的庫。 我想在我的主目錄中運行以下命令來編譯我的程序:包含包含目錄的標題時出現編譯錯誤

g++ -I ../maths prog1.cpp prog2.cpp test.cpp -o et -lboost_date_time -lgsl -lgslcblas 

,但我得到以下編譯錯誤:

prog1.cpp:4:23: fatal error: maths/Dense: No such file or directory 
compilation terminated. 
prog2.cpp:6:23: fatal error: maths/Dense: No such file or directory 
compilation terminated. 

maths位於同一目錄(即我主目錄)作爲.cpp文件,而且我也在家中運行編譯行。

prog1.cpp和prog2.cpp分別在第4行和第6行分別具有以下標頭 #include<maths/Dense>,因此我收到錯誤消息。

我該如何解決它。

+0

你確定它不是Dense.h或Dense.hpp嗎? – 2012-04-25 21:56:09

+0

@chris,解決了它......謝謝! – user1155299 2012-04-25 21:59:46

回答

1

maths is located in the same directory(i.e. my home directory) as the .cpp files

include路徑給出-I ../maths。您需要-I ./maths - 或更簡單,-I maths,因爲maths是當前目錄的子目錄,而不是父目錄。對?

然後在您的C++文件中使用#include <Dense>。如果您想使用#include <maths/Dense>,則需要修改包含路徑。但是,使用-I.可能會導致大量問題,我強烈建議這一點。

取而代之的是,包含一個include子目錄是常見的做法。所以,你的文件夾結構最好如下所示:

./ 
+ include/ 
| + maths/ 
| + Dense 
| 
+ your_file.cpp 

然後使用-I include,並在你的C++文件,#include <maths/Dense>


1)考慮一下,如果你有一個文件./map.cpp從中生成一個名爲./map的可執行文件會發生什麼。只要在代碼中的任意位置使用#include <map>,就會嘗試包括./map而不是map標準標頭。

+0

感謝您對該示例的解釋。這有助於。 – user1155299 2012-04-26 00:00:08

2

您可以更改包括路徑-I..或您的包括到#include <Dense>

等待,如果maths是在同一個目錄作爲源文件,這是你的當前目錄下,你可以改變你的包括對路徑-I.或您的包括對#include "Dense"

相關問題