1. Target file search (VPATH and vpath)
If the file is needed is present in a different path (ie, the source file and Makefile
the file is not under the same path), at compile time to use a Makefile
functional 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;vpath
It 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 Makefile
can write:
VPATH := dir
We can understand the dir
assignment value to the variable VPATH
, so the execution make
time will be from dir
find 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 dir
the file directory, and then search for dir2
files in a directory.
Note: No matter how many paths are defined,
make
when executed will first search for files in the current directory, no file we are looking for the current directory, just go toVPATH
the path to find. If we want to use the file in the current directory, itmake
will 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$
Makefile
On header
and source
catalog the same directory. Written Makefile
as follows:
main: main.o name.o
g++ main.o name.o -o main
Execution make
will be given:
make: *** No rule to make target 'main.o', needed by 'main'. Stop.
The ultimate goal of rebuilding the file main
when we need to main.o
file, but did not find main.o
the 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 dir
search 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 VPATH
are similar with a space or a colon separated, is the first order of the search path of dir
the directory, then the dir2
directory.
Followed by usage two, the command format is as follows:
vpath main.cpp
Usage of Two means clear specific file main.cpp
search 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 vpath
time, may be included in the search condition character mode %
, this symbol is the role of a match or a plurality of characters, such as %.cpp
indicating that all the search path .cpp
files ending. If it is not included in the search criteria %
, the searched file is the specific file name.
Use VPATH
cases are less in the path before the file, or the file search can not use wildcard notation used in these cases VPATH
the best.
If there is a special multi-path file or you can use wildcards when indicated, is not recommended to use VPATH
this method, why?
Because VPATH
at 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 vpath
search, 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 makefile
is 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/vpath
informing file search path is informed thatmake
this is conducive to file search when it implicitly deduced, not informedgcc
, they still have to pass-I
specifiedgcc
when the precompiled header file search path.
2.1 Implicit rules for command variables
Writing Makefile
can be directly written shell
with 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 CXXFLAGS
an 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$