繁体   English   中英

包含包含标题的目录时出现编译错误

[英]compilation error when including directory containing headers

我有一个目录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> ,因此出现错误。

我如何解决它。

您可以将include路径更改为-I..或将#include <Dense>更改为#include <Dense>

请稍等,如果maths与源文件位于同一目录中,并且这是当前目录,则可以将include路径更改为-I. 或您要包含的内容以#include "Dense"

数学与.cpp文件位于同一目录(即我的主目录)

您的包含路径为-I ../maths 您需要-I ./maths或更简单的-I maths因为maths当前目录的子目录,而不是父目录的子目录。 对?

然后在您的C ++文件中,使用#include <Dense> 如果要使用#include <maths/Dense> ,则需要调整include路径。 但是,使用-I. 可能会导致大问题1 ,我强烈建议您要这样做。

相反,通常的做法是包含一个include子目录。 因此,您的文件夹结构最好如下所示:

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

然后使用-I include ,在您的C ++文件中, #include <maths/Dense>


1)考虑如果您有一个文件./map.cpp从中生成一个名为./map的可执行文件,该怎么./map 在代码中的任何位置使用#include <map> ,这将尝试包含./map而不是map标准标头。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM