makefile特殊符号介绍

http://blog.chinaunix.net/uid-20564848-id-217918.html

makefile下$(wildcard $^),$^,[email protected],$?,$<,$(@D),$(@F)代表的不同含义

$(filter-out $(PHONY) $(wildcard $^),$^)
常用用法为$(wildcard *.c)
表示列举当前目录下的所有.c文件
这里$^因为会包含依赖的文件名,如果包含的该文件存在,那么将返回其含路径的文件名
所以$(wildcard $^)就是用来过滤$^包含的所有文件并且该文件确实在本地存在.

自动化变量$?代表依赖文件列表中被改变过的所有文件。
自动化变量$^代表所有通过目录搜索得到的依赖文件的完整路径名(目录 + 一般文件名)列表。
自动化变量[email protected]代表规则的目标。
自动化变量$<代表规则中通过目录搜索得到的依赖文件列表的第一个依赖文件。
自动化变量$(@D) 
The directory part of the file name of the target, 
with the trailing slash removed. If the value of ‘[email protected]’ is dir/foo.o 
then ‘$(@D)’ is dir. This value is . if ‘[email protected]’ 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 ‘[email protected]’ is dir/foo.o then ‘$(@F)’ is foo.o. 
‘$(@F)’ is equivalent to ‘$(notdir [email protected])’.

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 [email protected]
例子中,规则描述了所有的.o文件的依赖文件为对应的.c文件,对于目标“foo.o”
,取
其茎“foo”替代对应的依赖模式“%.c”中的模式字符“%”之后可得到目标的依赖文
件“foo.c”
。这就是目标“foo.o”的依赖关系“foo.o: foo.c”
,规则的命令行描述了如
何完成由“foo.c”编译生成目标“foo.o”
。命令行中“$<”和“[email protected]”是自动化变量,
“$<”
表示规则中的第一个依赖文件,
“[email protected]”
表示规则中的目标文件
(可参考 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 [email protected]
$(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 -$* > [email protected]
当执行此规则的命令时,
自动环变量
“$*”
被展开为
“茎” 在这里就是

“big” “little”


静态模式规则对一个较大工程的管理非常有用。
它可以对整个工程的同一类文件的
重建规则进行一次定义,而实现对整个工程中此类文件指定相同的重建规则。比如,可
以用来描述整个工程中所有的.o 文件的依赖规则和编译命令。通常的做法是将生成同
一类目标的模式定义在一个 make.rules 的文件中。在工程各个模块的 Makefile 中包含
此文件。

  1. 静态模式makefile中$(cobjs): $(obj)/%.o: $(src)/%.c
  2. http://www.gnu.org/software/make/manual/make.html
  3. 4.12.1 Syntax of Static Pattern Rules
  4. Here is the syntax of a static pattern rule:
  5. targets ...: target-pattern: prereq-patterns ...
  6. recipe
  7. ...
  8. 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).
  9. 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).
  10. 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.
  11. 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.
  12. ‘%’ 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.
  13. Here is an example, which compiles each of foo.o and bar.o from the corresponding .c file:
  14. objects = foo.o bar.o
  15. all: $(objects)
  16. $(objects): %.o: %.c
  17. $(CC) -c $(CFLAGS) $< -o [email protected]
  18. Here ‘$<’ is the automatic variable that holds the name of the prerequisite and ‘[email protected]’ is the automatic variable that holds the name of the target; see Automatic Variables.
  19. 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):
  20. files = foo.elc bar.o lose.o
  21. $(filter %.o,$(files)): %.o: %.c
  22. $(CC) -c $(CFLAGS) $< -o [email protected]
  23. $(filter %.elc,$(files)): %.elc: %.el
  24. emacs -f batch-byte-compile $<
  25. 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.
  26. Another example shows how to use $* in static pattern rules:
  27. bigoutput littleoutput : %output : text.g
  28. generate text.g -$* > [email protected]
  29. When the generate command is run, $* will expand to the stem, either ‘big’ or ‘little’.
时间: 2024-08-24 16:36:59

makefile特殊符号介绍的相关文章

linux shell中的比较符号与特殊符号介绍

shell字符串比较.判断是否为数字  二元比较操作符,比较变量或者比较数字.注意数字与字符串的区别.  整数比较  -eq 等于,如:if [ "$a" -eq "$b" ]  -ne  不等于,如:if [ "$a" -ne "$b" ]  -gt 大于,如:if [ "$a" -gt "$b" ]  -ge 大于等于,如:if [  "$a" -ge "

很详细、很移动的Linux makefile教程:介绍,总述,书写规则,书写命令,使用变量,使用条件推断,使用函数,Make 的运行,隐含规则 使用make更新函数库文件 后序

很详细.很移动的Linux makefile 教程 内容如下: Makefile 介绍 Makefile 总述 书写规则 书写命令 使用变量 使用条件推断 使用函数 make 的运行 隐含规则 使用make更新函数库文件 后序 近期在学习Linux下的C编程,买了一本叫<Linux环境下的C编程指南>读到makefile就越看越迷糊,可能是我的理解能不行. 于是google到了以下这篇文章.通俗易懂.然后把它贴出来,方便学习. 后记,看完发现这篇文章和<Linux环境下的C编程指南>

linux的cut、sort_wc_uniq、tee_tr_split命令及一些特殊符号介绍

shell特殊符号cut命令: 1.* 任意个任意字符(这是一个通配符) 2.? 任意一个字符 3. # 注释字符 4. \ 脱义字符 5. | 管道符 几个和管道有关的命令: 1.cut 分割,-d 分隔符  -f 指定段号   -c 指定第几个字符, 示例: [[email protected] ~]# cat /etc/passwd |head root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x

系统基本符号和正则符号介绍

一.系统基本符号($,!,|,#,' ' , " ", ,>, >> ,2>, 2>>,<,<<,., ..,&& ,||,;) 1.美元符号:$ a.用于取出变量中的内容b.用于取出指定列的信息(awk)c.表示用户命令提示符号(普通用户为$)d.表示一行的结尾 2.感叹号符号:! a.用于表示取反b.命令行中表示取出最近命令c.用于表示强制操作处理 3.竖线符管道号:| 管道前面命令执行完,交给管道后面执行:经常

makefile编译文件介绍

通常在C语言程序开发中,尤其是大型项目的构建,我们不可能一个文件一个文件去编译,通常会编写Makefile文件使用make命令完成项目的编译构建: 如下:math项目是简单的计算那个数字大,哪个数字小,以及两个数字和的小程序.由max.c,min.c , sum.c 和main.c构成,分别如下: max.c #include <stdio.h> int max(int a,int b) {     if(a>b){         return a;      }else{      

Makefile.am 文件介绍

Makefile.am 是一种比 Makefile 更高层次的规则.只需要指定要生成什么目标,它由什么源文件生成,要安装到什么目录 等构成 下表列出了可执行文件.静态库.头文件和数据文件,四种书写 Makefile.am 文件的一般格式. 对于可执行文件和静态库类型,如果只想编译,不想安装到系统中,可以用 noinst_PROGRAMS 代替 bin_PROGRAMS, noinst_LIBRARIES 代替 lib_LIBRARIES. Makefile.am 文件还提供一些全局变量供所有的目

Makefile语法基础介绍

在Linux下,make是一个命令工具,是一个解释Makefile中指令的命令工具.make命令执行时,需要一个Makefile文件,以告诉make命令需要怎么样去编译和链接程序. make如何工作:在默认的方式下,只输入make命令,那么: (1).make会在当前目录下找名字叫"Makefile"或"makefile"的文件: (2).如果找到,它会找文件中的第一个目标文件(target),并把这个文件作为最终的目标文件: (3).如果目标文件不存在,或是目标文

shell 特殊符号介绍

8.10 shell特殊符号&cut命令 特殊符号 "*" 代表零个或多个字符 "?" 代表一个字符 "#" 注释符号 "\" 脱意符号 "|" 管道符 "$" 该符号与"!"合用"!$"表示上一条命令中的最后一个变量 ":" 分隔符,在一行中运行两个及两个以上的命令时使用 "~" 用户的家目录(ro

简单介绍Makefile

makefile定义了一系列的规则来指定,哪些文件需要先编译,哪些文件需要后编译,哪些文件需要重新编译,甚至可以在makefile中执行shell脚本.makefile带来的好处就是——“自动化编译”,一旦写好,只需要一个make命令,整个工程完全自动编译,极大的提高了软件开发的效率. makefile简介 1 一个工程中的源文件不计数,其按类型.功能.模块分别放在若干个目录中,makefile定义了一系列的规则来指定,哪些文件需要先编译,哪些文件需 要后编译,哪些文件需要重新编译,甚至于进行更