makefile特殊符号介绍
http://blog.chinaunix.net/uid-20564848-id-217918.html
makefile下$(wildcard $^),$^,$@,$?,$<,$(@D),$(@F)代表的不同含义
$(filter-out $(PHONY) $(wildcard $^),$^)
常用用法为$(wildcard *.c)
表示列举当前目录下的所有.c文件
这里$^因为会包含依赖的文件名,如果包含的该文件存在,那么将返回其含路径的文件名
所以$(wildcard $^)就是用来过滤$^包含的所有文件并且该文件确实在本地存在.
自动化变量$?代表依赖文件列表中被改变过的所有文件。
自动化变量$^代表所有通过目录搜索得到的依赖文件的完整路径名(目录 + 一般文件名)列表。
自动化变量$@代表规则的目标。
自动化变量$<代表规则中通过目录搜索得到的依赖文件列表的第一个依赖文件。
自动化变量$(@D)
The directory part of the file name of the target,
with the trailing slash removed. If the value of ‘$@’ is dir/foo.o
then ‘$(@D)’ is dir. This value is . if ‘$@’ does not contain a slash.
http://www.gnu.org/software/make/manual/make.html
自动化变量$(@F)
The file-within-directory part of the file name of
the target. If the value of ‘$@’ is dir/foo.o then ‘$(@F)’ is foo.o.
‘$(@F)’ is equivalent to ‘$(notdir $@)’.
4.12 静态模式
静态模式规则是这样一个规则:
规则存在多个目标,
并且不同的目标可以根据目标
文件的名字来自动构造出依赖文件。
静态模式规则比多目标规则更通用,
它不需要多个
目标具有相同的依赖。
但是静态模式规则中的依赖文件必须是相类似的而不是完全相同
的。
4.12.1
静态模式规则的语法
首先,我们来看一下静态模式规则的基本语法:
TARGETS ...: TARGET-PATTERN: PREREQ-PATTERNS ...
COMMANDS
...
“TAGETS”
列出了此规则的一系列目标文件。
像普通规则的目标一样可以包含通
配符。关于通配符的使用可参考 4.4 文件名使用通配符 一节
“TAGET-PATTERN”和“PREREQ-PATTERNS”说明了如何为每一个目标文件
生成依赖文件。从目标模式(TAGET-PATTERN)的目标名字中抽取一部分字符串(称
为“茎”。使用“茎”替代依赖模式(PREREQ-PATTERNS)中的相应部分来产生对
)
应目标的依赖文件。下边详细介绍这一替代的过程。
首 先 在目标模式和依赖模式中 ,一般需要包含模式字符“% ”
。在目标模式
(TAGET-PATTERN)中“%”可以匹配目标文件的任何部分,模式字符“%”匹配的
部分就是“茎”
。目标文件和目标模式的其余部分必须精确的匹配。看一个例子:目标
“foo.o”符合模式“%.o”
,其“茎”为“foo”
。而目标“foo.c”和“foo.out”就不符
合此目标模式。
每一个目标的依赖文件是使用此目标的“茎”代替依赖模式
(PREREQ-PATTERNS)中的模式字符“%”而得到。例如:上边的例子中依赖模式
(PREREQ-PATTERNS)为“%.c”
,那么使用“茎”
“foo”替代依赖模式中的“%”
得到的依赖文件就是“foo.c”
。需要明确的一点是:在模式规则的依赖列表中使用不包
含模式字符“%”也是合法的。代表这个文件是所有目标的依赖文件。
在模式规则中字符‘%’可以用前面加反斜杠“\”方法引用。引用“%”的反斜杠
也可以由更多的反斜杠引用。引用“%”“\”的反斜杠在和文件名比较或由“茎”代
、
替它之前会从模式中被删除。反斜杠不会因为引用“%”而混乱。如,模式
“the\%weird\\%pattern\\”是“the%weird\”+“%”+“pattern\\”构成。最后的两个
反斜杠由于没有任何转义引用“%”所以保持不变。
我们来看一个例子,它根据相应的.c 文件来编译生成“foo.o”和“bar.o”文件:
objects = foo.o bar.o
all: $(objects)
$(objects): %.o: %.c
$(CC) -c $(CFLAGS) $< -o $@
例子中,规则描述了所有的.o文件的依赖文件为对应的.c文件,对于目标“foo.o”
,取
其茎“foo”替代对应的依赖模式“%.c”中的模式字符“%”之后可得到目标的依赖文
件“foo.c”
。这就是目标“foo.o”的依赖关系“foo.o: foo.c”
,规则的命令行描述了如
何完成由“foo.c”编译生成目标“foo.o”
。命令行中“$<”和“$@”是自动化变量,
“$<”
表示规则中的第一个依赖文件,
“$@”
表示规则中的目标文件
(可参考 10.5.3 自
动化变量 一小节)
。上边的这个规则描述了以下两个具体的规则:
foo.o : foo.c
$(CC) -c $(CFLAGS) foo.c -o foo.o
bar.o : bar.c
$(CC) -c $(CFLAGS) bar.c -o bar.o
在使用静态模式规则时,指定的目标必须和目标模式相匹配,否则执行make时将
会得到一个错误提示。
如果存在一个文件列表,
其中一部分符合某一种模式而另外一部
分符合另外一种模式,这种情况下我们可以使用“filter”函数(可参考 第八章 make
的内嵌函数)来对这个文件列表进行分类,在分类之后对确定的某一类使用模式规则。
例如:
files = foo.elc bar.o lose.o
$(filter %.o,$(files)): %.o: %.c
$(CC) -c $(CFLAGS) $< -o $@
$(filter %.elc,$(files)): %.elc: %.el
emacs -f batch-byte-compile $<
其中;$(filter %.o,$(files))的结果为“bar.o lose.o”“filter”函数过滤不符合“%.o”
。
模式的文件名而返回所有符合此模式的文件列表。
第一条静态模式规则描述了这些目标
文件是通过编译对应的.c 源文件来重建的。同样第二条规则也是使用这种方式。
我们通过另外一个例子来看一下自动环变量“$*”在静态模式规则中的使用方法:
bigoutput littleoutput : %output : text.g
generate text.g -$* > $@
当执行此规则的命令时,
自动环变量
“$*”
被展开为
“茎” 在这里就是
。
“big” “little”
和
。
静态模式规则对一个较大工程的管理非常有用。
它可以对整个工程的同一类文件的
重建规则进行一次定义,而实现对整个工程中此类文件指定相同的重建规则。比如,可
以用来描述整个工程中所有的.o 文件的依赖规则和编译命令。通常的做法是将生成同
一类目标的模式定义在一个 make.rules 的文件中。在工程各个模块的 Makefile 中包含
此文件。
- 静态模式makefile中$(cobjs): $(obj)/%.o: $(src)/%.c
- http://www.gnu.org/software/make/manual/make.html
- 4.12.1 Syntax of Static Pattern Rules
- Here is the syntax of a static pattern rule:
- targets ...: target-pattern: prereq-patterns ...
- recipe
- ...
- The targets list specifies the targets that the rule applies to. The targets can contain wildcard characters, just like the targets of ordinary rules (see Using Wildcard Characters in File Names).
- The target-pattern and prereq-patterns say how to compute the prerequisites of each target.Each target is matched against the target-pattern to extract a part of the target name, called the stem. This stem is substituted into each of the prereq-patterns to make the prerequisite names(one from each prereq-pattern).
- Each pattern normally contains the character ‘%’ just once. When the target-pattern matches a target, the ‘%’ can match any part of the target name; this part is called the stem. The rest of the pattern must match exactly. For example, the target foo.o matches the pattern ‘%.o’, with ‘foo’ as the stem. The targets foo.c and foo.out do not match that pattern.
- The prerequisite names for each target are made by substituting the stem for the ‘%’ in eachprerequisite pattern. For example, if one prerequisite pattern is %.c, then substitution of the stem ‘foo’ gives the prerequisite name foo.c. It is legitimate to write a prerequisite pattern that doesnot contain ‘%’; then this prerequisite is the same for all targets.
- ‘%’ characters in pattern rules can be quoted with preceding backslashes (‘\’). Backslashes that would otherwise quote ‘%’ characters can be quoted with more backslashes. Backslashes that quote ‘%’ characters or other backslashes are removed from the pattern before it is compared tofile names or has a stem substituted into it. Backslashes that are not in danger of quoting ‘%’ characters go unmolested. For example, the pattern the\%weird\\%pattern\\ has ‘the%weird\’ preceding the operative ‘%’ character, and ‘pattern\\’ following it. The final two backslashes areleft alone because they cannot affect any ‘%’ character.
- Here is an example, which compiles each of foo.o and bar.o from the corresponding .c file:
- objects = foo.o bar.o
- all: $(objects)
- $(objects): %.o: %.c
- $(CC) -c $(CFLAGS) $< -o $@
- Here ‘$<’ is the automatic variable that holds the name of the prerequisite and ‘$@’ is the automatic variable that holds the name of the target; see Automatic Variables.
- Each target specified must match the target pattern; a warning is issued for each target that does not. If you have a list of files, only some of which will match the pattern, you can use the filterfunction to remove nonmatching file names (see Functions for String Substitution and Analysis):
- files = foo.elc bar.o lose.o
- $(filter %.o,$(files)): %.o: %.c
- $(CC) -c $(CFLAGS) $< -o $@
- $(filter %.elc,$(files)): %.elc: %.el
- emacs -f batch-byte-compile $<
- In this example the result of ‘$(filter %.o,$(files))’ is bar.o lose.o, and the first static pattern rule causes each of these object files to be updated by compiling the corresponding C source file.The result of ‘$(filter %.elc,$(files))’ is foo.elc, so that file is made from foo.el.
- Another example shows how to use $* in static pattern rules:
- bigoutput littleoutput : %output : text.g
- generate text.g -$* > $@
- When the generate command is run, $* will expand to the stem, either ‘big’ or ‘little’.
makefile特殊符号介绍的更多相关文章
- 很详细、很移动的Linux makefile教程:介绍,总述,书写规则,书写命令,使用变量,使用条件推断,使用函数,Make 的运行,隐含规则 使用make更新函数库文件 后序
很详细.很移动的Linux makefile 教程 内容如下: Makefile 介绍 Makefile 总述 书写规则 书写命令 使用变量 使用条件推断 使用函数 make 的运行 隐含规则 使用m ...
- makefile文件模板介绍
1 src : = $(shell ls *.c)2 objs : = $(patsubst %.c, %.o, $(src))3 test : $(objs)4 ...
- 【linux】-Makefile简要知识+一个通用Makefile
目录 Makefile Makefile规则与示例 为什么需要Makefile Makefile样式 先介绍Makefile的两个函数 完善Makefile 通用Makefile的使用 通用的Make ...
- Makefile规则③规则语法、依赖、通配符、目录搜寻、目标
规则语法 通常规则的语法格式如下: TARGETS : PREREQUISITES COMMAND ... 或者: TARGETS : PREREQUISITES ; COMMAND COMMAND ...
- Linux编程(3) MakeFile
1. 在Linux中,make工具可以维护程序模块关系和生成可执行程序.它可根据程序模块的修改情况重新编译链接生成中间代码或最终的可执行程序.执行make命令,需要一个名为Makefile的文本文件, ...
- makefile基础实例讲解 分类: C/C++ 2015-03-16 10:11 66人阅读 评论(0) 收藏
一.makefile简介 定义:makefile定义了软件开发过程中,项目工程编译链.接接的方法和规则. 产生:由IDE自动生成或者开发者手动书写. 作用:Unix(MAC OS.Solars)和Li ...
- Linux ARM kernel Makefile and Kconfig
kernel build:顶层Makefile:-->1. include build/main.mk -->2. include build/kernel.mk k ...
- Linux Kconfig及Makefile学习
内核源码树的目录下都有两个文档Kconfig (2.4版本是Config.in)和Makefile.分布到各目录的Kconfig构成了一个分布式的内核配置数据库,每个Kconfig分别描述了所属目录源 ...
- 【高速接口-RapidIO】2、RapidIO串行物理层的包与控制符号
一.RapidIO串行物理层背景介绍 上篇博文提到RapidIO的物理层支持串行物理层与并行物理层两种,由于Xilinx 部分FPGA内部已经集成了串行高速收发器,所以用FPGA实现RapidIO大多 ...
随机推荐
- yum报错Segmentation fault
yum install 安装一个包,提示 Segmentation fault,可以确定的是这个源肯定是可用的. 经查询,是 libz 这个库存在多个版本,导致冲突. # ldconfig -v | ...
- 单点登录(九)-----遇到问题-----FileNotFoundException: class path resource-UsernamePasswordWrapperAuthenticatio
运行cas server 项目时 报错 FileNotFoundException: class path resource-UsernamePasswordWrapperAuthenticatio ...
- 解题:USACO12OPEN Bookshelf
题面 从零开始的DP学习之肆 当DP方程中的一部分具有某种单调性时可以用数据结构或者预处理维护来降低复杂度 一开始没有看懂题,尴尬,后来发现题目可以简化成这个样子: 将一个序列划分为若干段,每段长度不 ...
- BZOJ 1497 [NOI2006]最大获利
1497: [NOI2006]最大获利 Description 新的技术正冲击着手机通讯市场,对于各大运营商来说,这既是机遇,更是挑战.THU集团旗下的CS&T通讯公司在新一代通讯技术血战的前 ...
- 【trie树】【P4551】 最长异或路径
Description 给定 \(n\) 个点的带边权树,求一条异或和最大的简单路径 Input 第一行是点数 \(n\) 下面 \(n - 1\) 行每行三个整数描述这棵树 Output 输出一个数 ...
- 微信开发使用 frp 实现本地测试
前提条件: 1.有公网服务器(如阿里云) 2.需要独立的 80 端口,也就是说,想要实现这个目标,服务器上不能跑 nginx 之类占用 80 端口的程序 3.有可以测试使用的域名,并解析到上面说的公网 ...
- PHP_EOL 写入字符串换行 , php获取毫秒 microtime
private function miclog($t1,$t2,$name){ $lasttime = ($t2 - $t1).'ms'; $content = date('Y-m-d H:i:s', ...
- MongoDB 安装及开启关闭
开启关闭的方式: 命令行 输入 net start mongodb 就打开mongo的服务了 输入 net stop mongodb 关闭服务 验证是否成功的方式: 在浏览器中输入 http://lo ...
- NATS_09:NATS常见问题说明
1. Request() 和 Publish() 之间的不同 Publish()发送一条消息到 gnatsd 服务,是使用它的地址作为一个 主题(subject),而 gnatsd 交付消息给所有注册 ...
- 根据Bool值挑选数组中元素
根据Bool值挑选数组中元素 觉得有用的话,欢迎一起讨论相互学习~Follow Me 使用Boolean类型的数组挑选一维数组中的值 使用一维Boolean数组选取数组中的特定元素,对应位置为True ...