包含包含标头的目录时出现编译错误
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文件位于同一目录(即我的主目录)中,我也在我家运行编译行。
程序 1.cpp 和程序 2.cpp 具有以下标头 分别在第 4 行和第 6 行#include<maths/Dense>
,因此我收到错误。
我该如何解决它。
您可以将
包含路径更改为-I..
或将包含更改为#include <Dense>
等等,如果maths
与源文件位于同一目录中,并且是当前目录,则可以将包含路径更改为-I.
或将包含更改为#include "Dense"
maths 与.cpp文件位于同一目录(即我的主目录)中
您的包含路径为 -I ../maths
。您需要-I ./maths
- 或者更简单,-I maths
因为maths
是当前目录的子目录,而不是父目录的子目录。右?
然后在C++文件中,使用 #include <Dense>
.如果要使用#include <maths/Dense>
则需要调整包含路径。但是,使用-I.
可能会导致大量问题1,我强烈建议不要这样做。
相反,通常的做法是包含一个包含include
子目录。因此,您的文件夹结构最好如下所示:
./
+ include/
| + maths/
| + Dense
|
+ your_file.cpp
然后使用 -I include
,并在C++文件中#include <maths/Dense>
。
1)考虑如果你有一个文件./map.cpp
,从中生成一个名为./map
的可执行文件会发生什么。一旦您在代码中的任何位置使用 #include <map>
,这将尝试包含 ./map
而不是map
标准标头。
相关文章:
- std::is_base_of表示ctor编译错误
- Qt5:使用QCommandLineParser类时出现奇怪的编译错误
- 如何修复sfml c++代码编译错误
- 使用 MATLAB 编码器生成C++代码:编译错误"undefined reference to `rgb2gray_tbb_real64'"
- 使用外部SDK工具链文件在VisualStudio上生成项目编译错误
- vscode下的Arduino代码出现意外编译错误
- 第三方 API 中的编译错误 - Visual Studio
- std::cout输出int时出现编译错误
- 奇怪的代码抛出编译错误模板< J,int aSize=10> C2143:语法错误:在"<"之前缺少";"
- 提升图广度优先搜索前置编译错误
- C++ 中的编译错误:未定义对"主"的引用 collect2:错误:ld 返回 1 个退出状态
- 使用带有 ref 参数的成员函数创建线程时出现编译错误
- 我在C++中遇到了这个奇怪的编译错误
- 在C++中使用 Catch 测试框架编译错误"error: expected ';' at end of declaration list"
- 使用 std::enable_if 限制派生类的模板参数时出现编译错误
- 现代OpenGL和GLEW Libraray的编译错误
- C++ 编译错误:意外的类型名称"字符串":预期的表达式
- C ++程序编译错误,找不到/访问文件
- 使用直接大括号初始化时,C++ 编译错误"声明末尾的预期";"
- 为什么传递非静态成员函数会导致编译错误?