#include指令:相对于哪里?

发布于 2024-07-14 23:43:50 字数 128 浏览 4 评论 0原文

我查阅了《C++ 编程语言》,试图找到这个问题的答案。 当我在标头中 #include "my_dir/my_header.hpp" 时,它会在哪里查找此文件? 它是相对于标头、相对于包含它的源文件还是其他什么?

I have looked in The C++ Programming Language to try to find the answer to this. When I #include "my_dir/my_header.hpp" in a header, where does it look for this file? Is it relative to the header, relative to the source file that included it, or something else?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

傾旎 2024-07-21 23:43:50

它与当前源文件和给定的任何搜索路径相关(-I 表示 gcc)。

It is relative to both the current source file and to any search paths given (-I for gcc).

你在看孤独的风景 2024-07-21 23:43:50

这取决于您在 #include 指令中使用的语法:

#include "path-spec"
#include <path-spec>

带引号的形式:此形式指示预处理器在包含 #include 语句的文件的同一目录中查找包含文件,然后在包含 #include 语句的任何文件的目录中查找包含文件(#include) 该文件。 然后,预处理器沿着 /I 编译器选项指定的路径进行搜索,然后沿着 INCLUDE 环境变量指定的路径进行搜索。

尖括号形式:这种形式指示预处理器首先沿着 /I 编译器选项指定的路径搜索包含文件,然后,当从命令行编译时,沿着 INCLUDE 环境变量指定的路径搜索包含文件。

路径规范是一个文件名,前面可以选择目录规范。 文件名必须命名现有文件。 路径规范的语法取决于编译程序的操作系统。

此信息应位于特定 C++ 预处理器参考的文档中,以上内容取自 MSDN 上的这篇文章 其中有更多关于该主题的信息。

It depends on what syntax you use in the #include directive:

#include "path-spec"
#include <path-spec>

Quoted form : This form instructs the preprocessor to look for include files in the same directory of the file that contains the #include statement, and then in the directories of any files that include (#include) that file. The preprocessor then searches along the path specified by the /I compiler option, then along paths specified by the INCLUDE environment variable.

Angle-bracket form : This form instructs the preprocessor to search for include files first along the path specified by the /I compiler option, then, when compiling from the command line, along the path specified by the INCLUDE environment variable.

The path-spec is a filename optionally preceded by a directory specification. The filename must name an existing file. The syntax of the path-spec depends on the operating system on which the program is compiled.

This information should be in the documentation for your specific C++ Preprocessor Reference, the above is taken from this article on MSDN which has more on the subject.

我的奇迹 2024-07-21 23:43:50

完整的搜索路径可能取决于编译器。 对于 Visual Studio,文档指出它:

(...) 指示预处理器在包含 #include 语句的文件的同一目录中查找包含文件,然后在包含 (#include) 该文件的任何文件的目录中查找包含文件。 然后,预处理器沿着 /I 编译器选项指定的路径进行搜索,然后沿着 INCLUDE 环境变量指定的路径进行搜索。

The complete search path may depend on the compiler. For Visual Studio, the documentation states that it:

(...) instructs the preprocessor to look for include files in the same directory of the file that contains the #include statement, and then in the directories of any files that include (#include) that file. The preprocessor then searches along the path specified by the /I compiler option, then along paths specified by the INCLUDE environment variable.

半衬遮猫 2024-07-21 23:43:50

其实现已定义。 文件(例如 foo.h)上的那些 #include"my_dir/xxy.hpp" 是相对于该文件的(foo.h 和 my_dir 在目录层次结构中位于同一级别)。
对于某些(大多数?)编译器,您可以使用标志来使用这些 < > (#包括

我知道 gcc / g++ 提供了 -I 标志。 所以你可以使用 g++ -I /home [...]
表明xxy.hpp文件位于/home/my_dir/目录中。
我有一段时间没有使用任何其他 C/C++ 编译器了。

Its implementation defined. Those #include"my_dir/xxy.hpp" on a file (for example foo.h) are relative to the file (foo.h and my_dir would be on the same level at the directory hierarchy).
With some (most?) compilers, you can use a flag to use these < > (#include

I know that gcc / g++ provides the -I flag. So you could use g++ -I /home [...]
indicating that the xxy.hpp file is located in the /home/my_dir/ directory.
I havent used any other C/C++ compiler in a while now.

暖风昔人 2024-07-21 23:43:50

对于 GCC 版本 <= 3.0,尖括号形式不会在包含文件和包含文件之间生成依赖关系。
因此,如果您希望 makefile 自动生成依赖项,则必须对应包含在依赖项树中的文件使用带引号的形式。

for GCC version <= 3.0, the angle-bracket form does not generate a dependency between the included file and the including one.
So if you want that your makefile automatically generates dependencies, you must use the quoted form for the files that should be included in the dependency tree.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文