简体   繁体   English

g ++生成依赖文件缺少用户定义的标头

[英]g++ generate dependency file miss user defined headers

The structure of the folder of my source code is defined as follows: 我的源代码文件夹的结构定义如下:

src
 |--c
    |--c.h
    |--c.cpp

'ch' declares a class called 'B' and 'c.cpp' defined the class 'B'. “ ch”声明一个名为“ B”的类,“ c.cpp”定义该类“ B”。

Suppose we are in folder 'src' now. 假设我们现在位于文件夹“ src”中。 I run 我跑

   g++ -I./ -MM -MT c/c.o -MF c/c.d c/c.cpp

to generate the dependency file 'c/cd' for 'c/c.cpp'. 为“ c / c.cpp”生成依赖文件“ c / cd”。 The content of the file 'c/c.d', however, does not contain 'c/ch' even if I have included 'c/ch' in 'c/c.cpp' by 但是,即使我已经在“ c / c.cpp”中的“ c / ch”中包含了“ c / ch”,文件“ c / c.d”的内容也不包含“ c / ch”。

   #include "c/c.h".

However, the result is different if we are in folder 'c' and run the above command. 但是,如果我们在文件夹“ c”中并运行上面的命令,结果将有所不同。 By replacing 'c/ch' with 'ch' in the above process, I can get a correct dependency file (meaning that 'ch' is in the dependency file). 通过在上述过程中将“ c / ch”替换为“ ch”,我可以获得正确的依赖文件(意味着“ ch”位于依赖文件中)。

Anyone knows the reason of why the first process misses the header dependency? 任何人都知道为什么第一个进程错过标头依赖性的原因吗?

According to this GCC webpage , the 根据此GCC网页

"preprocessor looks for header files included by the quote form of the directive #include "file" first relative to the directory of the current file , and then in a preconfigured list of standard system directories." “预处理器首先相对于当前文件的目录 ,然后在标准系统目录的预配置列表中查找指令#include "file"的引号形式包含的头文件。”

That means when it sees #include "c/ch" , it checks for files in a hypothetical subdirectory named "c" from the current file's location. 这意味着当它看到#include "c/ch" ,它将从当前文件的位置检查名为“ c”的假设子目录中的文件。

When you replaced that with #include "ch" , the preprocessor then checks the directory of the current file. 当您用#include "ch"替换它时,预处理器将检查当前文件的目录。

Another option is to add -I../ to the command line parameters for g++. 另一种选择是将-I../添加到g ++的命令行参数中。

This GCC webpage provides the complete order in which the preprocessor searches directories for include files. 此GCC网页提供了预处理器在目录中搜索包含文件的完整顺序。 The lookup order is as follows: 查找顺序如下:

  1. For the quote form of the include directive, the directory of the current file is searched first. 对于include指令的引号形式,将首先搜索当前文件的目录。
  2. For the quote form of the include directive, the directories specified by -iquote options are searched in left-to-right order, as they appear on the command line. 对于include指令的引号形式,由-iquote选项指定的目录按从左到右的顺序搜索,就像它们出现在命令行上一样。
  3. Directories specified with -I options are scanned in left-to-right order. 用-I选项指定的目录以从左到右的顺序扫描。
  4. Directories specified with -isystem options are scanned in left-to-right order. 用-isystem选项指定的目录以从左到右的顺序扫描。
  5. Standard system directories are scanned. 扫描标准系统目录。
  6. Directories specified with -idirafter options are scanned in left-to-right order. 用-idirafter选项指定的目录以从左到右的顺序扫描。

Note that the directory from which you ran g++ does not appear on the above list. 请注意,运行g ++的目录没有出现在上面的列表中。 This means, the preprocessor does not check the directory from which you ran g++ on the command line. 这意味着,预处理器不会检查从您在命令行中运行G ++的目录。 The reason is so that you can run g++ from any directory and still get the same build results. 原因是您可以从任何目录运行g ++并仍然获得相同的构建结果。

This weird output is caused by the variable CPLUS_INCLUDE_PATH. 此奇怪的输出是由变量CPLUS_INCLUDE_PATH引起的。 I have set it to some value in the following: 我将其设置为以下值:

CPLUS_INCLUDE_PATH=some_path:

This tail ':' of the variable CPLUS_INCLUDE_PATH is the cause of my problem. 变量CPLUS_INCLUDE_PATH的尾部':'是造成我问题的原因。 With ':', the compiler considers './' as the system folder, so it automatically removes the headers related to './', such as 'c/c.h', from the dependency list. 使用“:”,编译器将“ ./”视为系统文件夹,因此它会自动从依赖项列表中删除与“ ./”相关的标头,例如“ c / c.h”。 Accordingly, if I set CPLUS_INCLUDE_PATH to 因此,如果我将CPLUS_INCLUDE_PATH设置为

CPLUS_INCLUDE_PATH=some_path

then the problem is solved. 那么问题就解决了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM