使用变量
三、变量高级用法
# 把变量“var” 中所有以“a”字符串“结尾”的“a”替换成“b”字符串
$( var: a = b)
或
${ var: a = b}
foo : = a. o b. o
bar : = $( foo: . o = . c)
#现在bar = a. b b. c
或者
foo : = a. o b. o
bar : = $( foo: % . o = % . c) #这种比较好用
x = y
y = z
z = u
a : = $( $( $( x) ) )
x = $( y)
y = z
z = HELLO
a : = $( $( x) ) # $( $( x) )
#1. $( x) = $( y)
#2 , $( z)
#3. HELLO
x = variable1
variable2 : = HELLO
y = $( subst 1 , 2 , $( x) )
z = y
a : = $( $( $( z) ) ) # = $( $( y) )
# = $( $( subst 1 , 2 , $( x) ) )
# = $( $( subst 1 , 2 , variable1)
# subst函数令variable1的1换成2
# = $( variable2)
# = HELLO
first_second = HELLO
a = first
b = second
all = $( $a_$b) # $( first_second)
# all 输出为 HELLO
a_objects : = a. o b. o c. o
1 _objects : = 1. o 2. o 3. o
sources : = $( $( a1) _objects: . o = . c)
# a1=a ,则 sources := a.c b.c c.c
# a1=1 ,则 sources := 1.c 2.c 3.c
ifdef do_sort #若定义了do_sort,则foo: = $( sort a d b g q c)
func : = sort # $( foo) 的值为 a b c d g q
else #若没定义do_sort,则foo: = $( sort a d b g q c)
func : = strip #调用的就是strip函数
endif
bar : = a d b g q c
foo : = $( $( func) $( bar) )
# 定义了三个变量:dir ,foo_sources, foo_print
dir = foo
$(dir)_sources := $(wildcard $(dir)/*.c)
define $(dir)_print
Ipr $($(dir)_sources)
endif
四、追加变量值
使用“ += ” 追加符 为了防止“=”带来的变量的递补归定义,make会自动修补
# 最好使用“: = ”, 而不是“= ”
# " += " 表示如果变量之前没有定义过,那么“+ = ”会自动变成“: = ”或“= ”
# 如果有定义过就会继承前次操作符的赋值符
objects : = main. o. foo. o bar. o utils. o
objects + = another. o
等价于
objects : = $( objects) another. o