当前位置:网站首页>Gun make (5) variables in makefile
Gun make (5) variables in makefile
2022-06-26 01:55:00 【Chen_ Hulk】
makefile Variables in have the following characteristics :
- makefile The expansion of variables and functions in make Read Makefile File .
- The variable name does not include : # = Leading blank Tail blank Any string of .
- Variable names are case sensitive .
1. References to variables
The expansion of variable references is a strict text substitution process , And C The macro expansion process in the language is the same .
How variables are referenced :
$(VAR)
${VAR}
$X // Single character only
2. Two variable definitions
2.1 Recursive expansion
Such variables are defined by = perhaps define Definition , The definition will not be expanded , Expand at the referenced place and , Its referenced variables are replaced and expanded together .
foo=$(bar)
bar=$(ugh)
ugh=hun?
all:
echo $(foo)
When executed make all When , Output hun?
advantage :
This type of variable is defined , You can reference variables that have not been defined before .
CFLAGS=$(include_dir) -O
include_dir = -lfoo -lbar //include_dir Define after reference shortcoming :
- Since there is a definition, it can be after the reference , So it will lead to infinite variable expansion .
CFLAGS=$(CFLAGS) -O- If you define a function in this style , The functions contained in the value of the variable are always executed where the variable is referenced .
2.1 Direct expansion
This style variable uses := Define , It is expanded at the time of definition .
x:=foo
y:=$(x) bar
x:=later
Equivalent to :
y:=foo bar
x:=later shortcoming :
Because variables are expanded when they are defined , Therefore, it is impossible to implement a reference to a variable defined after it .
3. Define a space
The leading space character in the leading value of a general variable is in Variable references and Function call Be discarded when .
Using direct extended variables, we can define a leading space in the variable value .
nullstr:=
space:=$(nullstr) #end of line
But trailing spaces are not ignored :
dir:=/fool/bar #end line
Variable dir The value is “/foo/bar ”
4. ?= The operator
This operator represents This variable has no previous assignment To assign a value to this variable .
FOO ?= bar
5. Alternative references to variables
Replace reference refers to replacing the suffix character in the value with the specified character .
Method 1:
$(VAR:A=B) perhaps ${VAR:A=B}) It means that you will :
VAR variable A Characters end with B Replace with characters .
The ending meaning is before the space .
foo := a.o b.o c.o
bar := $(foo:.o=.c)
After replacement bar The value is a.c b.c c.cMethod 2:
$(patsubst A,B $(VAR)) among ,A,B You need to include pattern characters %
$(patsubst %.o,%.c $(a.o b.o c.o))
6. Append variable value
Use += To implement the append operation of a variable value .
If the variable to which the value is appended has not been previously defined , be += Degenerate to =
objects = main.o foo.o
objects += another.o
After addition ,objects Turn into main.o foo.o another.o
direct / The additional difference between recursive expansion variables :
hypothesis include The variable is empty :
Recursive expansion :
CFLAGS = $(include)
CFLAGS += -pg
be CFLAGS When adding , Do not expand immediately , The resulting $(include) -pg
Direct expansion :
CFLAGS := $(include)
CFLAGS += -pg
be CFLAGS When adding , Immediately expand to get an empty , The resulting -pg
7. override Indicator
In execution makefile when , If you define a variable from the command line , Then replace in Makefile Variable definitions with the same name that appear in .
If you do not want the variable value specified on the command line to be replaced in Makefile Variable definition in , So we need to Makefile Use instructions in override To declare this variable .
override VAR = VALUE
override VAR := VALUE
override VAR += VALUE # Variables are defined using override, You also need override, Otherwise, no valid will be appended .
8. Target specified variable
The target specified variable is For the same variable, specify different values according to different targets .
The target specified variable is only valid in the target context in which it is specified , No impact on other goals , It's local .
grammar :
TARGET...:VAR-ASSIGN
TARGET...:override VAR-ASSIGN
among ,VAR-ASSIGN have access to = += := ?= Assignment method .Use :
foo:foo.c
foo:CFLAGS += -O2
bar:bar.c
bar:CFLAGS += -g
On it, the compiler foo when , Use optimization options -O2, But don't use -g
Compiling bar When the -g, But no -O2
9. The schema specifies the variables
When specifying variables using targets , This variable is defined on a specific target and the target of the rule caused by it .
The schema specifies that the variable definition is to assign a variable value to On all targets that conform to this pattern .
PATTERN...:VAR-ASSIGN
PATTERN...:override VAR-ASSIGN
among PATTERN Represents one or more schema targets , Contains mode characters %
such as :
For all .o File specifies the variable CFLAGS value :
%.o:CFLAGS += -O
边栏推荐
猜你喜欢

轻轻松松理解指针

二造实务案例答题技巧和举例汇总,满满都是精髓

Show spirit chenzitong was invited to be the chief experience officer of the global finals of the sixth season perfect children's model

丨EGFR FISH 探针解决方案

如何为政企移动办公加上一道“安全锁”?

Tengwenze, a hot-blooded boy, was invited to serve as the image ambassador of the global finals of the sixth season perfect children's model

分布式系统(二)分布式事务的理解

Cross validation -- a story that cannot be explained clearly

通俗易懂C語言關鍵字static

通俗易懂C语言关键字static
随机推荐
Talking about interface test (I)
Easy to understand C language keyword static
Eight principles of element positioning
Web Testing
The answer skills and examples of practical cases of the second construction company are full of essence
LeetCode 41 ~ 50
Log4j2 vulnerability
APP测试(一)
cyclegan:unpaired image-to-image translation using cycle-consistent adversarial network
Several methods of JQ obtaining objects
xargs 的作用详解
JQ user defined attribute value
Exploring temporary information for dynamic network embedding
Differences and functions of export set env in makefile
如何为政企移动办公加上一道“安全锁”?
Principle of voice wake-up
蒟蒻初学单片机的一丢丢笔记
Postman接口测试之断言
Reverse output an integer
readv & writev