如何告诉g ++编译器在哪里搜索包含文件? [英] How to tell g++ compiler where to search for include files?

查看:112
本文介绍了如何告诉g ++编译器在哪里搜索包含文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在工作目录中,我有很多* .cpp和* .h文件, #include 对方和子目录下的文件。

In a "working directory" I have a lot of *.cpp and *.h files that #include each other and files from subdirectories.

例如:

#include "first.h"
#include "second.h"
#include "dir1/third.h"
#include "dir2/fourth.h"

在我自己的目录(这与工作目录不同)中,我想创建一个新的* .cpp和* .h文件,其中包含working 目录。比如:

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:

#include "/root/workingdirectory/first.h"

但是,它不工作。因为first.h可能包括second.h和second.h不在我的目录中。有没有办法告诉编译器,它需要搜索包含的文件不在当前,但在工作目录: / root / workingdirectory /

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/?

为了使它更复杂, dir1 dir2 工作目录。它们位于 / root / workingdirectory2 / 中。所以,我的第二个问题是如果有可能解决这个问题,让编译器知道子目录位于其他地方?

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?

我还需要添加,我不使用任何环境进行开发并从命令行编译(使用 g ++ )。

I also need to add, that I do not use any environment for development and compile from the command line (using g++).

推荐答案

阅读Fine手册

这里有大家阅读。你甚至可以选择使用什么(我会用第一个):

It's there for everyone to read. You even have a choice of what to use (I'd go with the first):

-Idir




将目录dir添加到要搜索头文件的目录列表的头部。这可以用于覆盖系统头文件,替换您自己的版本,因为这些目录是在系统头文件目录之前搜索的。但是,您不应该使用此选项添加包含供应商提供的系统头文件的目录(使用 -isystem )。如果使用多个 -I 选项,将按照从左到右的顺序扫描目录;

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.

如果标准系统包含目录或使用 -isystem 指定的目录,也使用 -I 指定,则会忽略 -I 选项。该目录仍然被搜索,但作为系统目录在其正常位置在系统包含链。这是为了确保GCC的修复错误系统头的过程和 include_next 指令的顺序不会无意中改变。如果真的需要更改系统目录的搜索顺序,请使用 -nostdinc 和/或 -isystem 选项。

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 the include_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.



-iquotedir




将目录dir添加到要搜索头文件的目录列表的头部,仅适用于# includefile;他们不会搜索 #include< file> ,否则就像 -I

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.

这篇关于如何告诉g ++编译器在哪里搜索包含文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆