I tried the following code tree. If I put the header file hello.h into the “inc” archive, and use the MM option my system reported that hello.h cannot be found even I add a vpath command in my makefile. If I move the hello.h back to the same directory of hello.c, gcc -MM works fine and listed the dependence file successfully.
How can I make the gcc know where to find the header file automatically? The following is the code tree, and “hello.h” is placed in archive “inc”
total 12
-rw-r--r-- 1 root root 101 Jun 22 14:13 hello.c
drwxr-xr-x 2 root root 4096 Jun 22 14:14 inc
-rw-r--r-- 1 root root 139 Jun 22 14:18 makefile
The following is my makefile content:
vpath %.h /home/tempcode/inc
hello: hello.o
gcc -o hello hello.o
hello.o: hello.c hello.h
gcc -c hello.c
debug:
gcc -MM hello.c
It makes me puzzled that if I run gcc directly like this, it works. does this mean VPATH or vpath do not help gcc finding the include path ? If so, I guess vpath variable only helps ‘make’ with finding the header file but not helping gcc, am I right?
[root@localhost tempcode]# gcc -MM -I/home/tempcode/inc hello.c
hello.o: hello.c /home/tempcode/inc/hello.h
That is correct. You should set the
CFLAGSvariable to do what you want. Make automatically includesCFLAGSwhen compiling C files. (CXXFLAGSfor C++.) In your case, you’ll need to add it to make.dfiles. I usually do something like this: