如何让#include<> 在标准系统目录的子文件夹中查找?

How do I get #include<> to look in a subfolder of the standard system directory?

我把原来的 post 写在下面,但认为这个问题可以更笼统。

我安装了一个名为 ibex 的 C++ 库。它包含一个文件ibex.h和一个子文件夹ibex,子文件夹包含更多被ibex.h调用的头文件。 ibex.h 和子文件夹都位于 /usr/local/include 中。我已经确认这是在我的标准系统目录中。

我正在尝试 运行 下面 ibex 网站的示例代码 foo.cpp

#include <ibex.h>
#include <iostream>

using namespace std;
using namespace ibex;

int main(int argc, char** argv) {
  Interval x(0,1);
  cout << "My first interval:" << x << endl;
}

但出现错误

In file included from (filelocationremoved)/foo.cpp:1:
/usr/local/include/ibex.h:6:10: fatal error: 'ibex_Setting.h' file not found

所以基本上它可以找到 ibex.h 但不会在子文件夹中查找 ibex_Setting.h(或子文件夹中的任何其他文件)。有没有办法让它查看该子文件夹中的所有文件,就好像它们在 /usr/local/include 文件夹中一样,而不用将它们全部移入?

谢谢!

原文post:

我是一个新手 c++ 用户(我主要使用 Matlab 和一点 Python)但需要为 ibex 库使用 c++ (http://www.ibex-lib.org)。我正在尝试编译他们的基本示例,但遇到了问题。我希望这里的一些有经验的用户可以提供帮助,因为我没有取得任何进展。

备注:

我正在尝试 运行 他们的文档 foo.cpp

中的以下示例代码
#include <ibex.h>
#include <iostream>

using namespace std;
using namespace ibex;

int main(int argc, char** argv) {
  Interval x(0,1);
  cout << "My first interval:" << x << endl;
}

但出现错误

In file included from (filelocationremoved)/foo.cpp:1:
/usr/local/include/ibex.h:6:10: fatal error: 'ibex_Setting.h' file not found

ibex_Setting.h 文件是 ibex.h 调用的众多文件之一。我在 /usr/local/include 中有 ibex.h 文件,它似乎能够找到它。我在同一个 /usr/local/include 文件夹中还有一个 ibex 文件夹的别名(所有相关子文件都位于该文件夹中)。但它不会在该子文件夹中查找。该子文件夹是由 brew link ibex 创建的,我认为应该可以找到它。如果我将 ibex_Setting.h 移动到 /usr/local/include 文件夹中,它会找到它,但会移动到它找不到的下一个文件(也位于 /usr/local/include/ibex 中)。该文件夹有足够的文件,我不想手动将它们全部移动到 /usr/local/include。此外,需要这样做似乎很愚蠢。

在 ibex 文档中,它还说要这样做: export DYLD_LIBRARY_PATH=[prefix]/lib,其中前缀是文件夹位置。我的 libibex.dylib 位于我使用的 /usr/local/lib 中。这没有帮助。我确定我没有提供足够的信息,但我又是 c++ 的新手,所以甚至不知道还包括什么。任何帮助将不胜感激!

谢谢

/usr/local/include 是 ibex.h 所在的位置。 尝试在其他包含目录中添加 ibex_Setting.h 的子目录路径。或者,如果您没有找到其他包含目录设置,请使用 ../../../Path 找到您的包含文件。

我不熟悉 XCode 和 Sublime Text 但我可以给你看一个演示 问题的根源,以及如何在编译器级别解决它。你可以如何工作 在 IDE.

的编译器设置中应用它

这是程序项目文件夹的内容:

$ ls -R
.:
header.h  main.cpp  subfolder

./subfolder:
subheader.h

这里是源文件和 headers:

$ cat main.cpp
#include <header.h>

int main()
{
    return foo();
}

$ cat header.h
#ifndef HEADER_H
#define HEADER_H

#include <subheader.h>

inline int foo() {
    return bar();
}

#endif

$ cat subfolder/subheader.h 
#ifndef SUBHEADER_H
#define SUBHEADER_H

inline int bar() {
    return 42;
}

#endif

我首先尝试这样编译 prog

$ g++ -Wall -o prog main.cpp
main.cpp:1:10: fatal error: header.h: No such file or directory
    1 | #include <header.h>
      |          ^~~~~~~~~~
compilation terminated.

这是行不通的,因为预处理器不知道去哪里找 对于 系统 header <header.h>。 (在这种情况下,系统 header 只是 表示 <...> 中指定的 header 文件,而不是 local header, 将在 "..." 中指定,并将在当前搜索 编译器运行时的目录)。

我可以通过添加一个 -I dir 预处理器选项来解决这个问题,它告诉预处理器 在目录 dir 中搜索 header 文件。由于 header.h 在当前 在此演示中的目录,我将只使用 . 作为目录名称,而不是 输入完整的目录名称。

$ g++ -Wall -I. -o prog main.cpp
In file included from main.cpp:1:
./header.h:4:10: fatal error: subheader.h: No such file or directory
    4 | #include <subheader.h>
      |          ^~~~~~~~~~~~~
compilation terminated.

该修复已启用 header.h 被发现,但随后在 header.h 中,有 <subheader.h>.

同样的问题

我用同样的方法解决了这个问题:

$ g++ -Wall -I. -I./subfolder -o prog main.cpp
$ ./prog
$ echo $?
42

现在一切正常。

所以对于第一个近似值,您需要做的是在您的 IDE 项目中进行设置以指定 -I/usr/local/include-I/usr/local/include/subfolder 被传递给 编译命令。但实际上你不需要第一个,因为 /usr/local/include 始终是 header 文件的默认编译器搜索目录。的答案 this question 可能对你有帮助,虽然它很旧。

是否有任何类似于 -I dir 的选项可以让预处理器 递归搜索 dir 的子文件夹中解析 #include <...> 指令?不,没有。

请注意,如果我将 header.h 编码为:

#include <subfolder/subheader.h>

而不是:

#include <subheader.h>

那么我可以在第二次尝试时编译我的程序,g++ -Wall -I. -o prog main.cpp

组织良好的包往往有他们的 #include <...> 指令编写 必要的路径元素,如 <subfolder/subheader.h>,以允许成功编译 最多有一个 -I base/path 选项,其中 base/path 是公共路径前缀 所有 #include <...> 指令。但是有些包裹没有 可能是一个很好的理由。