In the makefile rule, the wildcard character is automatically expanded. However, when the variable is defined and the function is referenced, the wildcard character is invalidated. In this case, if a wildcard is required to be valid, the function "wildcard" is used, and its usage is: $ (wildcard PATTERN ...). In makefile, it is expanded to a list of all files that already exist, separated by spaces, that match this pattern. If there are no files that conform to this pattern, the function ignores the pattern character and returns NULL. It is important to note that in this case, the expansion of the wildcard character in the rule and the previous section match the difference of the wildcard character.
Generally we can use "$ (wildcard *.c)" To get a list of all the. c files under the working directory. Use "$ (patsubst%.c,%.o,$ (wildcard *.c)") to get a list of. c files under the working directory using the "wildcard" function, followed by the suffix of all filenames in the list. C is replaced by. O. This allows us to get a list of the. o files that can be generated in the current directory. Therefore, in one directory, you can use the following makefile to compile all the. c files under the working directory and finally connect them as an executable file:
#sample Makefile
Objects: = $ (Patsubst%.c,%.o,$ (wildcard *.c))
Foo: $ (objects)
Cc-o Foo $ (objects)
Here we use the implicit rules of make to compile the. C source file. The assignment of a variable also uses a special symbol (: =).
1. Wildcard: Extended wildcard character
2. Notdir: Remove Path
3. PATSUBST: Replace wildcard characters
Example:
Set up a test directory to create a subdirectory named sub in the test directory
$ mkdir Test
$ CD Test
$ mkdir Sub
Under test, create A.C and B.c2 files, and in sub directory, create SA.C and SB.C2 files
To build a simple makefile
src=$ (wildcard *.c./sub/*.c)
dir=$ (Notdir $ (src))
obj=$ (Patsubst%.c,%.o,$ (dir))
All
@echo $ (SRC)
@echo $ (dir)
@echo $ (obj)
@echo "End"
Execution Result Analysis:
First line output:
A.C B.C./sub/sa.c./SUB/SB.C
Wildcard all files that have the suffix C under the specified directory./and./sub/are expanded.
The second line of output:
A.C B.C sa.c SB.C
Notdir the expanded file to get rid of the path information
The third line of output:
A.O B.O SA.O SB.O
In $ (Patsubst%.c,%.o,$ (dir)), Patsubst replaces the variable in the $ (dir) with the suffix of. c. O,
Any output.
Or you can use
obj=$ (DIR:%.C=%.O)
The effect is the same.
Here is the replacement reference rule in Makefile, which replaces another variable with the variable you specify.
Its standard format is
$ (var:a=b) or ${var:a=b}
What it means is to replace each value in Var with b by the end of a
Introduction of wildcard in Makefile