多个包含在C++中的#include

Qua*_*sar 0 c++ include c++11

我对#includeC++中的预处理程序指令有疑问.

在我的程序中,我想包含另一个目录中的头文件.为此,我使用了完整路径,例如:

#include "full/path/to/my/header.hpp"
Run Code Online (Sandbox Code Playgroud)

现在事实证明它header.hpp本身有一个包含(比方说#include "otherheader.hpp".在编译期间,编译器抱怨它找不到这个其他标题.

考虑到我也不想为每个头文件编写完整路径这一事实,尤其是那些只需要"进一步向下"的文件,这是处理这个问题的最佳方法是什么?

Ron*_*eau 7

您应该使用编译器的-I选项.

示例g++:

g++ -I full/path/to/my/
Run Code Online (Sandbox Code Playgroud)

然后在你的代码中你可以简单地把:

#include "header.hpp"
Run Code Online (Sandbox Code Playgroud)

有关搜索路径的更多信息.

  • @ZeeshanAkhter这如何提高可读性? (2认同)