Makefile 中 wildcard的用法1
wildcard即通配符,通常包括?
以及*
。
在Makefile规则中,通配符会被自动展开。但在变量的定义和函数引用时,通配符将失效。这种情况下如果需要通配符有效,就需要使用函数wildcard
。语法是:$(wildcard PATTERN...)
在Makefile中,它被展开为已经存在的、使用空格分开的、匹配此模式的所有文件列表。如果不存在任何符合此模式的文件,函数会忽略模式字符并返回空。
其他命令
- notdir: 去除路径名
- patsubst: 替换通配符(pattern substitute)
Makefile 中 addprefix的用法
给每个string添加前缀,string之间用空格隔开
$(addprefix [str], [string1 string2 ...])
[] 使用时需要去除,如:
$(addprefix chapters/, docx, pdf, jpg)
最终会得到
chapters/docx
chapters/pdf
chapters/jpg
当然,参数也可以使用变量$(...)
来代替。
用法示例
src=main
chap-dir=chapters
tmp-suffix = aux log out toc
tmp-files = $(addprefix ${src}., ${tmp-suffix})
tmp-files += $(wildcard $(addprefix ${chap-dir}/*, ${tmp-suffix}))
前三句定义了三个变量。
倒数第二句将main.
作为前缀分别添加到aux log out toc
,于是展开得到main.aux main.log main.out main.toc
最后一句将chapters/*
添加到aux log out toc
,展开后得到chapters/*aux chapters/*log chapters/*out chapters/*toc
。之后wildcard
函数将得到匹配此模式的所有文件列表,即得到chapters目录下上述四种文件类型的文件名。