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'.
Suppose we are in folder 'src' now. 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'. 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
#include "c/c.h".
However, the result is different if we are in folder 'c' and run the above command. 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).
Anyone knows the reason of why the first process misses the header dependency?
According to this GCC webpage , the
"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."
That means when it sees #include "c/ch"
, it checks for files in a hypothetical subdirectory named "c" from the current file's location.
When you replaced that with #include "ch"
, the preprocessor then checks the directory of the current file.
Another option is to add -I../
to the command line parameters for g++.
This GCC webpage provides the complete order in which the preprocessor searches directories for include files. The lookup order is as follows:
Note that the directory from which you ran g++ does not appear on the above list. This means, the preprocessor does not check the directory from which you ran g++ on the command line. The reason is so that you can run g++ from any directory and still get the same build results.
This weird output is caused by the variable 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. 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. Accordingly, if I set CPLUS_INCLUDE_PATH to
CPLUS_INCLUDE_PATH=some_path
then the problem is solved.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.