Path search in make (12)
Do we have all the source files and header files in the same folder in the actual project? If it is a relatively small project, of course it is possible. But if there are thousands of source files, of course you have to store them separately. The commonly used source code management methods are as follows
So can the following makefile be compiled successfully?
Let's give it a try. The compilation results are as follows
Then the result must be like this, because we did not specify a path in makefile, it did not find the source file in the current directory. Next we have to introduce a special predefined variable VPATH (all uppercase). The value of the VAPTH variable is used to tell make how to find files, different folders can appear at the same time as the value of VPATH, and the names of folders need to be distinguished by delimiters. The format is as follows
Make for the VAPTH value of the way: 1, the current folder can not find the required files, VAPTH will be used; 2, make will be specified in the VAPTH folder to search for files; 3, when multiple folders have files of the same name, select the first search for the file. Note: 1, VAPTH can only determine the search path of make, but not the search path of the command; 2, for specific compilation commands (gcc), you need to specify the compilation search path independently. As follows
Let's take the code as an example and rewrite it on the basis of the makefile above.
OBJS: = func.o main.oINC: = incSRC: = srcVPATH: = $(INC) $(SRC) hello.out: $(OBJS) @ gcc-o $@ $^ @ echo "Target File = > $@" $(OBJS):% .o:% .c func.h @ gcc-o $@-c $^
Let's compile and try.
Do you see the compilation or the error? the reason is that when we gcc, the dependent file also contains func.h. Let's change the $^ of the last line to $.