C++ 如何告诉 g++ 编译器在哪里搜索包含文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15478005/
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
How to tell g++ compiler where to search for include files?
提问by Roman
In a "working directory" I have a lot of *.cpp and *.h files that #include
each other and files from subdirectories.
在“工作目录”中,我有很多 *.cpp 和 *.h 文件#include
以及来自子目录的文件。
For example:
例如:
#include "first.h"
#include "second.h"
#include "dir1/third.h"
#include "dir2/fourth.h"
In my own directory (that is different from the "working" directory) I would like to create a new *.cpp and *.h file that includes one of the files from the "working" directory. For example:
在我自己的目录(与“工作”目录不同)中,我想创建一个新的 *.cpp 和 *.h 文件,其中包含“工作”目录中的文件之一。例如:
#include "/root/workingdirectory/first.h"
However, it does not work. Because "first.h" might include "second.h" and "second.h" is not located in my directory. Is there a way to tell the compiler that it needs to search included files not in the current but in the working directory: /root/workingdirectory/
?
但是,它不起作用。因为“first.h”可能包含“second.h”而“second.h”不在我的目录中。有没有办法告诉编译器它需要搜索不在当前目录中而是在工作目录中的包含文件:/root/workingdirectory/
?
To make it even more complex, dir1
and dir2
are not located in my working directory. They are located in /root/workingdirectory2/
. So, my second question is if it is possible to resolve this problem by letting compiler know that subdirectories are located somewhere else?
使其更加复杂,dir1
并且dir2
不在我的工作目录中。它们位于/root/workingdirectory2/
. 所以,我的第二个问题是是否可以通过让编译器知道子目录位于其他地方来解决这个问题?
I also need to add, that I do not use any environment for development and compile from the command line (using g++
).
我还需要补充一点,我不使用任何环境进行开发和从命令行编译(使用g++
)。
回答by rubenvb
It's there for everyone to read. You even have a choice of what to use (I'd go with the first):
它可供所有人阅读。您甚至可以选择使用什么(我会选择第一个):
-Idir
Add the directory dir to the head of the list of directories to be searched for header files. This can be used to override a system header file, substituting your own version, since these directories are searched before the system header file directories. However, you should not use this option to add directories that contain vendor-supplied system header files (use
-isystem
for that). If you use more than one-I
option, the directories are scanned in left-to-right order; the standard system directories come after.If a standard system include directory, or a directory specified with
-isystem
, is also specified with-I
, the-I
option is ignored. The directory is still searched but as a system directory at its normal position in the system include chain. This is to ensure that GCC's procedure to fix buggy system headers and the ordering for theinclude_next
directive are not inadvertently changed. If you really need to change the search order for system directories, use the-nostdinc
and/or-isystem
options.
将目录 dir 添加到要搜索头文件的目录列表的头部。这可用于覆盖系统头文件,替换您自己的版本,因为在系统头文件目录之前搜索这些目录。但是,您不应使用此选项添加包含供应商提供的系统头文件的目录(
-isystem
用于此目的)。如果您使用多个-I
选项,则按从左到右的顺序扫描目录;标准系统目录紧随其后。如果标准系统包含目录或指定的目录
-isystem
也指定为-I
,-I
则忽略该选项。该目录仍然被搜索,但作为系统目录在系统包含链中的正常位置。这是为了确保 GCC 修复错误系统标头的程序和include_next
指令的顺序不会被无意中更改。如果您确实需要更改系统目录的搜索顺序,请使用-nostdinc
和/或-isystem
选项。
-iquotedir
Add the directory dir to the head of the list of directories to be searched for header files only for the case of
#include "file"
; they are not searched for#include <file>
, otherwise just like-I
.
仅针对以下情况将目录dir添加到要搜索头文件的目录列表的头部
#include "file"
;他们不会被搜索#include <file>
,否则就像-I
.
回答by Useless
As you've already been told, it's useful to read the manual- specifically this chapter- and even more specifically right here.
正如您已经被告知的那样,阅读手册很有用- 特别是本章- 更具体地说是在这里。
Specifically, you want
具体来说,你想要
g++ -I/root/workingdirectory -I/root/workingdirectory2
Note also the documentation on #include
directive syntax, described hereas:
还要注意#include
指令语法的文档,这里描述为:
2.1 Include Syntax
Both user and system header files are included using the preprocessing directive
#include
. It has two variants:#include <file>
This variant is used for system header files. It searches for a file named file in a standard list of system directories. You can prepend directories to this list with the -I option (see Invocation).
#include "file"
This variant is used for header files of your own program. It searches for a file named file first in the directory containing the current file, then in the quote directories and then the same directories used for
<file>
. You can prepend directories to the list of quote directories with the -iquote option. The argument of#include
, whether delimited with quote marks or angle brackets, behaves like a string constant in that comments are not recognized, and macro names are not expanded. Thus,#include <x/*y>
specifies inclusion of a system header file named x/*y.However, if backslashes occur within file, they are considered ordinary text characters, not escape characters. None of the character escape sequences appropriate to string constants in C are processed. Thus,
#include "x\n\\y"
specifies a filename containing three backslashes. (Some systems interpret\
as a pathname separator. All of these also interpret/
the same way. It is most portable to use only/
.)It is an error if there is anything (other than comments) on the line after the file name.
2.1 包含语法
使用预处理指令包含用户和系统头文件
#include
。它有两种变体:#include <file>
此变体用于系统头文件。它在系统目录的标准列表中搜索名为 file 的文件。您可以使用 -I 选项将目录添加到此列表中(请参阅调用)。
#include "file"
此变体用于您自己程序的头文件。它首先在包含当前文件的目录中搜索名为 file 的文件,然后在引用目录中搜索,然后在用于
<file>
. 您可以使用 -iquote 选项将目录添加到引用目录列表中。的参数#include
,无论是用引号还是尖括号分隔,其行为都类似于字符串常量,因为无法识别注释,并且不会扩展宏名称。因此,#include <x/*y>
指定包含名为 x/*y 的系统头文件。但是,如果文件中出现反斜杠,则它们被视为普通文本字符,而不是转义字符。不处理适用于 C 中字符串常量的字符转义序列。因此,
#include "x\n\\y"
指定包含三个反斜杠的文件名。(某些系统解释\
为路径名分隔符。所有这些也以/
相同的方式解释。仅使用最便携/
。)如果文件名后的行中有任何内容(注释除外),则这是一个错误。
So for example
所以例如
#include "first.h"
will startlooking in the same directory as the .cpp file containing this directive (or take a relative path as relative to this directory).
将开始在与包含此指令的 .cpp 文件相同的目录中查找(或采用相对于该目录的相对路径)。
If you want to use the include path (specified by -I
) you should use
如果要使用包含路径(由 指定-I
),则应使用
#include <dir1/third.h>
Usual practice is to use the #include "local.h"
form for headers inside a library/package/module (however you've chosen to organize that), and the #include <external.h>
form for headers from external/3rd-party or system libraries.
通常的做法是使用#include "local.h"
库/包/模块中的头文件的形式(无论你选择如何组织它),以及#include <external.h>
来自外部/第 3 方或系统库的头文件的形式。
回答by Andrew White
For gcc it is the -I
option for header includes. For the .cpp files you just need those to appear as an argument to the gcc command.
对于 gcc,它是header 包含的-I
选项。对于 .cpp 文件,您只需要它们作为 gcc 命令的参数出现。