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