Thanks for the help. I don't have c/c.h in my computer. Actually, this problem still existed even I changed my file name. According to your suggestions, I finally found the cause. It is that my CPLUS_INCLUDE_PATH variable is set to CPLUS_INCLUDE_PATH=some_path: The tail ':' of the variable CPLUS_INCLUDE_PATH caused this issue. If I remove the tail and make it to CPLUS_INCLUDE_PATH=some_path All results are normal (as you said, g++ reports error). On Sun, Aug 20, 2017 at 3:39 AM, Xi Ruoyao <ryxi@xxxxxxxxxxxxxxxxx> wrote: > On 2017-08-19 02:05 -0700, chran wang wrote: >> Hello Ruoyao, >> >> For (B), I'm sorry that I forgot to say that I changed '#include >> "c/c.h" ' to '#include "c.h" ' in that case. For others, I did not >> encounter any errors on my machine as you showed. >> >> I'm using ubuntu 16.04. At first, I tried with g++ 5.4.0 since it is >> shipped with ubuntu. Then I compiled a gcc 7.1 from its source code. I >> got the same result under g++ 7.1 (and no error output). >> > For (A) there should be error output. > > Try g++ -E -v c/c.cpp in root. Then you can see which file "c/c.h" really > is. > > Note that you may have a "c/c.h" in system header directories (-isystem). > -MM doesn't mention system headers. By default your include directive can > not find your root/c/c.h. So maybe you have "-isystem root" or likewise, > or maybe "/usr/include/c/c.h" (weird name collision). > > Check you environment CPLUS_INCLUDE_PATH since it may add system header > directories unintentionally. >> >> On Sat, Aug 19, 2017 at 1:32 AM, Xi Ruoyao <ryxi@xxxxxxxxxxxxxxxxx> wrote: >> > On 2017-08-18 15:52 -0700, chran wang wrote: >> > > Hello, >> > > >> > > I observed some weird results about the dependency generation. >> > > >> > > Suppose the source code structure is as follows: >> > > >> > > root >> > > |-- c >> > > | |-- c.cpp >> > > | |-- c.h >> > > >> > > 'c.cpp' includes the header file 'c.h' by >> > > #include "c/c.h" >> > > >> > > Now I try to generate the dependencies for c/c.cpp. >> > > >> > > (A) If I'm now in 'root' folder and run command >> > > g++ -MM -MT c/c.o c/c.cpp >> > > the compiler outputs >> > > c/c.o : c/c.cpp # which is wrong because it lacks c/c.h >> > >> > Your #include directive is bad. The compiler gives: >> > >> > LANG= g++ -MM -MT c/c.o c/c.cpp >> > c/c.cpp:1:10: fatal error: c/c.h: No such file or directory >> > #include "c/c.h" >> > ^~~~~~~ >> > compilation terminated. >> > >> > > (B) If I'm in folder 'c' and run command >> > > g++ -MM -MT c.o c.cpp >> > > the compiler can ouput the correct results >> > > c.o : c.cpp c.h >> > >> > Ditto. >> > >> > > (C) If I'm in the parent folder of folder 'root' and run command >> > > g++ -I./root -MM -MT root/c/c.o root/c/c.cpp >> > > the results is also correct: >> > > root/c/c,o : root/c/c.cpp root/c/c.h >> > >> > With -I./root the include directive is correct. >> > >> > > (D) Now, if I'm in folder 'root' again and include the head file 'c.h' >> > > for 'c.cpp' by >> > > #include "c.h" instead of #include "c/c.h" >> > > the result is also correct. >> > >> > Ditto. >> > >> > > I'm wondering why (A) cannot give a correct dependency output but (B), >> > > (C) and (D) can. >> > >> > Which version of GCC are you using? It seems like a preprocessor bug >> > and I can't reproduce it with GCC 4.6, 4.7, 5.x, 6.x and 7.x. >> > -- >> > Xi Ruoyao <ryxi@xxxxxxxxxxxxxxxxx> >> > School of Aerospace Science and Technology, Xidian University > -- > Xi Ruoyao <ryxi@xxxxxxxxxxxxxxxxx> > School of Aerospace Science and Technology, Xidian University