当前位置:网站首页>Gun make (3) Rules for makefile
Gun make (3) Rules for makefile
2022-06-26 01:55:00 【Chen_ Hulk】
1. The type of dependency
GUN make There are two types of dependencies in :
Routine dependence : Any one of the dependent files is newer than the target file , It is considered that the goal of the rule is expired and needs to be rebuilt .
order-only rely on : When order-only When a file in a dependency is updated , If the target does not exist , Generate target ; If the target exists , Do not update the target .
routine 、order-only Rely on writing rules :
TARGETS:NORMAL-PREREQUISITES | ORDER-ONLY-PREREQUISITES
Use “ | ” Separate general dependencies from order-only rely on , If a file appears on both sides at the same time , It is considered as a regular dependency .
2. File name wildcard
2.1 Wildcard type
makefile Wildcard characters can be used for file names in , Yes * , ? , [...]
Wildcards can be used in the following situations :
- Used in the target of the rule , Dependency , make In the reading makefile It is automatically matched when ( Wildcard expansion ).
- Can appear in the command of the rule , The general configuration processing is performed in shell Completed when executing this command .
- In other contexts besides the above two cases , Wildcards cannot be used directly .
2.2 wildcard function
In the rules , Wildcards are automatically expanded , But in variable definitions and function references , Wildcards will be invalidated .
If the wildcard is required to take effect , Use wildcard function :
$(wildcard PATTERN)
stay makefile in , It is expanded to an existing , Separated by spaces , List of all files matching the pattern .
such as :
$(wildcard *.c) To get all the... In the working directory .c File list .
3. Directory search
3.1 General search (VPATH)
Through the variable VPATH You can specify the search path for dependent files , Use Space Or in the The colon : Separate multiple directory files to be searched , The search order is in the order of the directory .
When When the dependent file of the rule does not exist in the current directory ,make These dependent files will be found in the directory specified by this variable .
such as :
VPATH = src:../headers
It specifies src and ../headers Two search directories , about foo:foo.c , If foo.c Exist in src Under the table of contents , This rule is equivalent to foo:src:/foo.c
3.2 Selective search (vpath)
Selective search can specify different search directories for different types of files .
vpath Medium PATTERN You need to include pattern characters %, It means matching one or more characters . Represents a class of files with the same characteristics .
DIRECTORIES Specifies the directory to search for such files .
Three ways to use :
- vpath PATTERN DIRECTORIES
For all conforming patterns PATTERN The file specifies the search directory DIRECTORIES, Multiple directories use spaces perhaps The colon : Separate .
- vpath PATTERN
Before clearing, it is in compliance mode PATTERN The search path for the file settings of .
- vpath Clear all set file search paths .
such as :
vpath %.h ../headers
Express makefile What happened in .h file , If it cannot be found in the current directory , Then go to the catalog ../headers Look for .
Be careful , The specified path here is limited to makefile Appears in the contents of the file .h file , You cannot specify the path where the header file contained in the source file is located . stay .c The header file path contained in the source file needs to use gcc Of -I To specify the .
3.3 The mechanism of directory search
make In parsing Makefile The algorithm for saving or discarding file paths when executing rules for files is as follows :
- If the rules are Target file stay makefile The directory where the file is located does not exist , Then perform a directory search .
- If the directory search is successful , This target's... Exists in the specified directory The rules , Then the full pathname searched will be saved as a temporary target file .
- For all of the rules Dependency file Use the same method to deal with .
- After the third step of dependency processing ,make The program can then determine whether the goal of the rule needs to be rebuilt , There are two situations :
- The goal of the rule does not need to be rebuilt , The full pathnames of all files in the rule are valid , The directory of the existing target file will not be changed .
- The goal of the rule needs to be rebuilt , The target file of the rule will be rebuilt in the working directory , Instead of the directory obtained during directory search .
- If you use GPATH instead of VPATH Specify search directory , Then the target is rebuilt , It will be rebuilt in the directory obtained during directory search .
3.4 Library files and search directories
makefile have access to -INAME Yes Static library .a Dynamic library .so Do a directory search .
The file name of the dependent library searched is determined by .LIBPATTERNS Appoint , It is usually multiple characters that contain patterns % Word , Multiple words are separated by spaces .
according to .LIBPATTERNS The order in which words appear , use NAME Pattern characters that replace words one by one , Get library file name , Search under the search directory according to the library file name .
By default , .LIBPATTERNS The value is lib%.so lib%.a , So usually search first libNAME.so library , Re search libNAME.a library .
4. Makefile Target in
4.1 Fake target
Pseudo targets use :
.PHONY:clean // Use .PHONY Declare false targets clean
clean:
rm *.o temp
Pseudo target advantages :
- When the working directory does not exist clean This file , Output make clean when , The corresponding rule must be executed . But if the current working directory exists clean When this document , When the input make clean, Because this rule does not depend on files , So the target is considered to be up-to-date without executing the commands defined by the rules .
- When a target is declared as a pseudo target ,make When this rule is executed, no attempt is made to find an implicit rule to create it .
4.2 Force the target
The target of the mandatory target is a nonexistent file name , And there are no commands or dependencies .
When executing this mandatory goal , Goals are always considered up to date . therefore , If you force the target as a dependency , Cause the rule to be executed .
such as :
clean:FORCE // Force the target as a dependency
rm $(objects)
FORCE: // Force the target ( No dependencies or commands )
4.3 Multiple goals
The multi-target rule means that all targets have the same dependent files .
Multi objective usage :
aaa bbb:text.g
generate text.g -$(subst output,[email protected]) > [email protected]
Equivalent to :
aaa:text.g
generate text.g -$(subst output,[email protected]) > aaa
bbb:text.g
generate text.g -$(subst output,[email protected]) > bbb
4.4 Multi rule target
For a multi rule target , The command to rebuild this target can only appear in one rule .
If multiple rules give the command to rebuild this target at the same time ,make The command defined in the last rule will be used .
4.5 Static mode
Static mode rules :
Rules have multiple goals , And different targets can automatically construct dependent files according to the name of the target file . And the dependent files must be similar, not identical .
4.5.1 grammar :
TARGETS...:TARGET-PATTERN:PREREQ-PATTERN...
COMMANDS
...TARGETS Lists a series of target files for this rule .
TARGET-PATTERN and PREREQ-PATTERNS Describes how to generate dependency files for each target file . From target mode TARGET-PATTERN Extract a part of the string from the target name of ( be called stem ), Use stems instead of dependency patterns PREREQ-PATTERNS To generate the dependent file corresponding to the target .
such as :
objects=foo.o bar.o
all:$(objects)
$(objects):%o:%c
$(CC) -c $(CFLAGS) $< -o [email protected]In the rules %o:%c Describes all .o The dependent file of the file is the corresponding .c file .
To the goal foo.o Take its stem foo Instead of the corresponding dependency pattern %.c Mode characters in % Then you can get the dependent files of the target foo.c.
The above rule describes the following two specific rules :
foo.o:foo.c
$(CC) -c $(CFLAGS) foo.c -o foo.o
bar.o:bar.c
$(CC) -c $(CFLAGS) bar.c -o bar.o
4.5.2 The difference between static patterns and implicit rules
- Implicit rules can be used in whatever On the target that matches it . When there are multiple implicit rules matching the target pattern , Execute only one of the rules , Which one to execute depends on the order in which the rules are defined .
- Static mode rules can only be used in rules clear The reconstruction process of those files pointed out . If there are two rules for a target , be make The execution will prompt an error .
4.6 Automatically generate dependencies
gcc adopt -M Option to automatically find the header file contained in the source file , And generate file dependencies .
When you do not need to consider standard library header files in dependencies , Use -MM Parameters .
such as ,main.c Only header files are included in def.h, be :
gcc -M main.c
Its output is :
main.o:main.c def.h
边栏推荐
- 通俗易懂C語言關鍵字static
- Perfdog
- Perfdog
- Data analysis - similarities and differences between C-end and b-end data analysis
- Byte order problem
- Summary of knowledge points of catboost
- Dataframe converts the data type of a column
- The 19th - 22nd week of scientific research training - about tnet and memnet
- Accumulation of some knowledge points in machine learning
- Accumulation and summary of activation function
猜你喜欢
随机推荐
Distributed systems (II) understanding of distributed transactions
GUN make (2) 总述
PTA class a simulated ninth bullet: 1114-1117
PTA class a simulated 11th bomb: 1124-1131
Redis7.0 installation steps
OTA trigger
甜酷少女金书伊 受邀担任第六季完美童模全球总决赛代言人
Summary of informer's paper
Summary of in-depth learning optimization techniques
Some summary of sequence model
字节序问题
27. template match
recvmsg & sendmsg
Mot - clé C facile à comprendre statique
PTA class a simulation bomb 10: 1119-1123
程序 编译的步骤 预编译 编译 汇编 连接
前置++,后置++与前置--与后置--(++a,a++与--a,a--)
PTA class a simulated sixth bomb: 1156-1159
Finding the sum of N multiplications
The role of xargs








