Simple and easy to understand Makefile entry (03)-target file search (the difference and use of VPATH and vpath), implicit rules

1. Target file search (VPATH and vpath)

If the file is needed is present in a different path (ie, the source file and Makefilethe file is not under the same path), at compile time to use a Makefilefunctional search for files in a directory for us.

There are mainly two common search methods:

  • VPATH It is a variable, you need to specify the path of the file when you use it, and it is all the files under the search path;
  • vpathIt is a keyword, search according to the pattern, it can also be said to be a selective search. When searching, not only need to add the path of the file, but also need to add the corresponding restrictive conditions;

1.1 VPATH

In Makefilecan write:

VPATH := dir

We can understand the dirassignment value to the variable VPATH, so the execution maketime will be from dirfind files we need directory.

When there are multiple paths, we can write:

VPATH := dir dir2

or

VPATH := dir:dir2

Use spaces or colons to separate multiple paths, which means searching for files in multiple paths. The order of the search order for us to write, you should search dirthe file directory, and then search for dir2files in a directory.

Note: No matter how many paths are defined, makewhen executed will first search for files in the current directory, no file we are looking for the current directory, just go to VPATHthe path to find. If we want to use the file in the current directory, it makewill use the files in our current directory.

The code directory structure is as follows:

wohu@ubuntu:~/cpp/demo$ tree
.
├── header
│   └── name.h
├── Makefile
└── source
    ├── main.cpp
    └── name.cpp

2 directories, 4 files
wohu@ubuntu:~/cpp/demo$

MakefileOn headerand sourcecatalog the same directory. Written Makefileas follows:

main: main.o name.o
	g++ main.o name.o -o main

Execution makewill be given:

make: *** No rule to make target 'main.o', needed by 'main'.  Stop.

The ultimate goal of rebuilding the file mainwhen we need to main.ofile, but did not find main.othe file, which is the root cause of the error.

Plus path search

VPATH = source header
main: main.o name.o
	g++ main.o name.o -o main

Enter make to run normally

wohu@ubuntu:~/cpp/demo$ make
g++    -c -o main.o source/main.cpp
g++    -c -o name.o source/name.cpp
g++ main.o name.o -o main
wohu@ubuntu:~/cpp/demo$ 

3.2 vpath

Specific usage:

vpath PATTERN DIRECTORIES 
vpath PATTERN
vpath

among them:

  • PATTERN: To find the conditions;
  • DIRECTORIES: The path to find;

The first is usage one, the command format is as follows:

vpath main.cpp dir

It can be understood, in the dirsearch for files under the path main.cpp. The multi-path writing rules are as follows:

vpath main.cpp dir dir2         或者是         vpath main.cpp dir : dir2

In fact, the use of multi-path and VPATHare similar with a space or a colon separated, is the first order of the search path of dirthe directory, then the dir2directory.

Followed by usage two, the command format is as follows:

vpath main.cpp

Usage of Two means clear specific file main.cppsearch directories.

The last is usage three, the command format is as follows:

vpath

vpath Separately means to clear all the file search paths that have been set.

Also the use of vpathtime, may be included in the search condition character mode %, this symbol is the role of a match or a plurality of characters, such as %.cppindicating that all the search path .cppfiles ending. If it is not included in the search criteria %, the searched file is the specific file name.

Use VPATHcases are less in the path before the file, or the file search can not use wildcard notation used in these cases VPATHthe best.

If there is a special multi-path file or you can use wildcards when indicated, is not recommended to use VPATHthis method, why?

Because VPATHat the time to search for the file no restrictions, so it went back to retrieve all files in this directory, each file will compare, search and directory name the same file that we not only can be slow, but is inefficient.

We can use in this case vpathsearch, which includes limit the search conditions when the search will only search target from the conditions we set, the filter does not conform to the requirements document, of course, find time will be relatively fast.

Example of use:

vpath %.cpp source
main: main.o name.o
	g++ main.o name.o -o main

2. Implied rules

The so-called implicit rule is that the system automatically completes some actions. In some cases, there is actually no need to give the command to rebuild the target file, and some even do not need to give the rules.
E.g:

vpath %.cpp source
main: main.o name.o
	g++ main.o name.o -o main

After executing make, the output result:

wohu@ubuntu:~/cpp/demo$ make
g++    -c -o main.o source/main.cpp
g++    -c -o name.o source/name.cpp
g++ main.o name.o -o main
wohu@ubuntu:~/cpp/demo$ 

The system automatically executed the following two lines of statements

g++    -c -o main.o source/main.cpp
g++    -c -o name.o source/name.cpp

Note: Implied conditions can only omit the commands and rules for the intermediate target file reconstruction, but the commands and rules for the final target cannot be omitted.

The following makefileis equivalent to above.

vpath %.cpp source
main: main.o name.o
	g++ main.o name.o -o main
main.o: main.cpp
	g++ -c ./source/main.cpp -o main.o 
name.o: name.cpp 
	g++ -c ./source/name.cpp -o name.o

Execution make, the output results:

wohu@ubuntu:~/cpp/demo$ make 
g++ -c ./source/main.cpp -o main.o 
g++ -c ./source/name.cpp -o name.o
g++ main.o name.o -o main
wohu@ubuntu:~/cpp/demo$ 

Note that: by VPATH/vpathinforming file search path is informed that makethis is conducive to file search when it implicitly deduced, not informed gcc, they still have to pass -Ispecified gccwhen the precompiled header file search path.

2.1 Implicit rules for command variables

Writing Makefilecan be directly written shellwith these variables. Here are just some of the C/C++related:

variable name meaning
RM rm -f
WITH With
CC cc
CXX g++

Example:

all:
    @echo $(RM)
    @echo $(AR)
    @echo $(CC)
    @echo $(CXX)

Execute make, display the value of each variable

wohu@ubuntu:~/cpp/func$ make 
rm -f
ar
cc
g++
wohu@ubuntu:~/cpp/func$ 

2.2 Command parameter variables

variable name meaning
ARFLAGS AR command parameters
CFLAGS C language compiler parameters
CXXFLAGS C++ language compiler parameters

Example: Below CXXFLAGSan example to show

#include <iostream>

int main(int argc, char *argv[])
{
    
    
    std::cout << "hello,world" << std::endl;
    return 0;
}

Makefile content

CXXFLAGS += -Wall
main: demo.o
	$(CXX) demo.o -o main

carried out make

wohu@ubuntu:~/cpp/func$ make
g++ -Wall   -c -o demo.o demo.cpp   <-- 这个是隐式规则自动推导的
g++ demo.o -o main
wohu@ubuntu:~/cpp/func$ 

Guess you like

Origin blog.csdn.net/wohu1104/article/details/110942995