C++ #include 指令:相对于哪里?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/645165/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
#include directive: relative to where?
提问by rlbond
I have looked in The C++ Programming Languageto 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?
我查看了The C++ Programming Language试图找到答案。当我#include "my_dir/my_header.hpp"
在标题中时,它在哪里查找此文件?它是相对于标题,相对于包含它的源文件,还是其他什么?
采纳答案by aib
Implementation defined. See what is the difference between #include <filename> and #include “filename”.
回答by Ignacio Vazquez-Abrams
It is relative to both the current source file and to any search paths given (-I for gcc).
它相对于当前源文件和给定的任何搜索路径(-I 表示 gcc)。
回答by Peter McG
It depends on what syntax you use in the #include directive:
这取决于您在 #include 指令中使用的语法:
#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.
引用形式:此形式指示预处理器在包含 #include 语句的文件的同一目录中查找包含文件,然后在包含 (#include) 该文件的任何文件的目录中查找。然后预处理器沿着 /I 编译器选项指定的路径搜索,然后沿着 INCLUDE 环境变量指定的路径搜索。
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.
角括号形式:这种形式指示预处理器首先沿 /I 编译器选项指定的路径搜索包含文件,然后在从命令行编译时,沿 INCLUDE 环境变量指定的路径搜索。
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.
path-spec 是一个文件名,可选地以目录规范开头。文件名必须命名现有文件。path-spec 的语法取决于编译程序的操作系统。
This information should be in the documentation for your specific C++ Preprocessor Reference, the above is taken from this article on MSDNwhich has more on the subject.
回答by noup
The complete search path may depend on the compiler. For Visual Studio, the documentationstates that it:
完整的搜索路径可能取决于编译器。对于 Visual Studio,文档指出它:
(...) 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.
(...) 指示预处理器在包含 #include 语句的文件的同一目录中查找包含文件,然后在包含 (#include) 该文件的任何文件的目录中查找。然后预处理器沿着 /I 编译器选项指定的路径搜索,然后沿着 INCLUDE 环境变量指定的路径搜索。
回答by Tom
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
它的实现定义。文件(例如 foo.h)上的那些 #include"my_dir/xxy.hpp" 是相对于文件的(foo.h 和 my_dir 在目录层次结构中处于同一级别)。对于某些(大多数?)编译器,您可以使用标志来使用这些 < > (#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.
我知道 gcc / g++ 提供了 -I 标志。因此,您可以使用 g++ -I /home [...] 指示 xxy.hpp 文件位于 /home/my_dir/ 目录中。我已经有一段时间没有使用任何其他 C/C++ 编译器了。
回答by Denis Ros
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.
对于 GCC 版本 <= 3.0,尖括号形式不会在包含文件和包含文件之间生成依赖关系。因此,如果您希望 makefile 自动生成依赖项,则必须对应包含在依赖项树中的文件使用带引号的形式。