From reading Chenhao, "write Makefile with Me"
VPATH Variable
In some large projects, there are a large number of source files, we usually do this is to classify many of the source files, and stored in different directories. So, when make needs to find a dependency on a file, you can add a path to the file, but the best way is to give it a path, so it is automatically searched. The special variable "VPATH" in the Makefile file is the completion of this function, and if this variable is not specified, make will only find the dependent and target files in the current directory. If this variable is defined, make will search for the file in the specified directory when the current directory is not found.
VPATH = Dir1:dir2
The above definition specifies two directories, "Dir1" and "Dir2", and make will search in this order. The directory is delimited by a "colon". (Of course, the current directory is always the highest-priority search place)
How to use it?
Assume the following works, the number of directories is:
./├──bardir│├──bar.c│└──bar.h├──command.h├──foodir│├──foo.c│└──foo.h├──main.c├──makefile└──readme.md
The function in Bar.c foo.c is called in Main.c, the most straightforward makefile
OBJS = main.o foodir/foo.o bardir/bar.ocincludes =-i./foodir-i./bardircflags =-walltarget = test$ (TARGET): $ (OBJS) $ (CC) $ (CFLAGS) $^-o [email protected] $ (cincludes). PHONY:CLEANCLEAN:RM $ (OBJS) $ (TARGET)
View directory tree
./├──bardir│├──bar.c│├──bar.h│└──bar.o├──command.h├──foodir│├──foo.c│├──foo.h│└──foo.o├──main. C├──main.o├──makefile├──readme.md└──test
If the module directory is deep, then OBJS will follow a large pile, then the Vpath variable plays a role, the improved makefile
VPATH =./foodir:./bardir
Cincludes =-i./foodir-i./bardir
CFLAGS =-wall $ (cincludes)
TARGET = Test
$ (TARGET): $ (OBJS)
$ (CC) $ (CFLAGS) $^-o [email protected]
. Phony:clean
Clean
@-rm-f $ (TARGET) $ (OBJS)
To view the directory tree again:
./├──bardir│├──bar.c│└──bar.h├──bar.o├──command.h├──foodir│├──foo.c│└──foo.h├──foo.o├──main.c├──mai N.o├──makefile├──readme.md└──test
Compare the last time you can see that the. o files that were generated under the subdirectory are now generated in the makefile level directory. It is important to note that the file search path through Vpath is informed by make, which facilitates file searches when it is implicitly deduced, rather than notifying GCC, so it is still necessary to specify the GCC precompiled time header file search path through -I.
Vpath Keywords
。。。
The use of the Vpath and the Vpath of makefile