当前位置:网站首页>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
边栏推荐
猜你喜欢

Exploring temporary information for dynamic network embedding

cyclegan:unpaired image-to-image translation using cycle-consistent adversarial network

浅谈接口测试(一)

Talking about interface test (2)

tos cos dscp 区别和作用

recvmsg & sendmsg

论文阅读 Exploring Temporal Information for Dynamic Network Embedding

Viwi interface

How to add a "security lock" to the mobile office of government and enterprises?

Sweet cool girl jinshuyi was invited to be the spokesperson for the global finals of the sixth season perfect children's model
随机推荐
热血男孩滕文泽 受邀担任第六季完美童模全球总决赛形象大使
字节序问题
前置++,后置++与前置--与后置--(++a,a++与--a,a--)
元气少女王钰洁 受邀担任第六季完美童模全球总决赛代言人
关于VS scanf出现‘scanf‘: This function or variable may be unsafe. Consider usi问题的解决方法
Several methods of JQ obtaining objects
Redis的使用
regular expression
PTA class a simulated first bomb: 1132-1135
Abnova丨抗GBA单克隆抗体解决方案
App test (I)
17.11 std::atomic续谈、std::async深入谈
求n的乘阶
判定积分给业务带来价值的两个指标
Textcnn paper Interpretation -- revolutionary neural networks for sense classification
代码覆盖率测试(一)
cyclegan:unpaired image-to-image translation using cycle-consistent adversarial network
GUN make (2) 总述
Data analysis - similarities and differences between C-end and b-end data analysis
UN make (6) makefile的条件执行