自学MakeFile笔记___aBout Pattern Rule.

作者:Easiny

 

笔者初次学习MakeFile 看的是GNU的makefile Manual 英文,其中所有的翻译均是自己理解,

可能和网上的有所不同,仅供参考。

一:什么是Pattern Rule

 

10.5.1 Introduction to Pattern Rules(范式规则)

A pattern rule contains the character `%

' (exactly one of them) in the target; otherwise, it looks exactly like an ordinary rule.

//Pattern Rule 和普通Rule的区别就是其中有一个“%”符号。


The target is a pattern for matching file names; the `

%

' matches any nonempty substring, while other characters match only themselves.

For example, `%.c ' as a pattern matches any file name that ends in `.c '. `s.%.c

' as a pattern matches any file name that starts with `s. ', ends in `.c

' and is at least five characters long.

(There must be at least one character to match the `% '.) The substring that the `% ' matches is called the stem .

 

//简单的说就是 用“%”符号去匹配目录下的文件,由make自己去搜索,不过在Targets中要列出足够的搜索信息足以找到

//准确的依赖文件。

 

 

`% ' in a prerequisite of a pattern rule stands for the same stem that was matched by the `%

' in the target.//“%”的使用在依赖文件列表中也是起相同的作用。

In order for the pattern rule to apply, its target pattern must match the file name under consideration

and all of its prerequisites (after pattern substitution) must name files that exist or can be made.

These files become prerequisites of the target. Thus, a rule of the form

 %.o : %.c ; command

...



==================================华丽的分割线============================================





10.5.3 Automatic Variables(自动变量)

Suppose you are writing a pattern rule to compile a `.c ' file into a `.o

' file:how do you write the `cc ' command so that it operates on the right source file name?

You cannot write the name in the command, because the name is different each time the implicit rule is applied.

 

What you do is use a special feature of make , the automatic variables .

 

These variables have values computed afresh for each rule that is executed, based on the target and prerequisites of the rule. In this example, you would use `$@ ' for the object file name and `$< ' for the source file name.

 

It's very important that you recognize the limited scope in which automatic variable values are available:

they only have values within the command script .//自动变量只能在Makefile的命令行中使用

In particular, you cannot use them anywhere within the target list of a rule; they have no value there and will expand to the empty string.

Also, they cannot be accessed directly within the prerequisite list of a rule.//不能在Targerts和依赖文件中使用

A common mistake is attempting to use $@ within the prerequisites list; this will not work.

However, there is a special feature of GNU make , secondary expansion (see Secondary Expansion ), which will allow automatic variable values to be used in prerequisite lists.

Here is a table of automatic variables:

$@    //列出目标文件,如果是归档文件,只列出归档文件名
The file name of the target of the rule . If the target is an archive member, then ` $@ ' is the name of the archive file. In a pattern rule that has multiple targets (see Introduction to Pattern Rules ), ` $@ ' is the name of whichever target caused the rule's commands to be run.

$%    //当目标文件是一个归档文件的时候,列出归档文件的成员文件名。否则为空。
The target member name, when the target is an archive member. See Archives . For example, if the target is foo.a(bar.o) then ` $% ' is bar.o and ` $@ ' is foo.a . ` $% ' is empty when the target is not an archive member.

$<    //列出第一个依赖文件
The name of the first prerequisite . If the target got its commands from an implicit rule, this will be the first prerequisite added by the implicit rule (see Implicit Rules ).

===================以上三个都是列出一个文件,下面的一般可能会列出多个文件==========

$?    //列出所有更新的文件
The names of all the prerequisites that are newer than the target, with spaces between them. For prerequisites which are archive members, only the member named is used (see Archives ).
$^    //列出所有的依赖文件,重复文件只列出一次
The names of all the prerequisites , with spaces between them. For prerequisites which are archive members, only the member named is used (see Archives ). A target has only one prerequisite on each other file it depends on, no matter how many times each file is listed as a prerequisite. So if you list a prerequisite more than once for a target, the value of $^ contains just one copy of the name. This list does not contain any of the order-only prerequisites; for those see the ` $| ' variable, below.
$+    //列出所有依赖文件,重复文件按顺序列出
T hi s is like `$^ ', but prerequisites listed more than once are duplicated in the order they were listed in the makefile. This is primarily useful for use in linking commands where it is meaningful to repeat library file names in a particular order.

$|   //列出所有order-Only文件名。
T he n ames of all the order-only prerequisites, with spaces between them.

$*
The stem with which an implicit rule matches (see How Patterns Match ). If the target is dir/a.foo.b and the target pattern is a.%.b then the stem is dir/foo . The stem is useful for constructing names of related files. In a static pattern rule, the stem is part of the file name that matched the ` % ' in the target pattern.

In an explicit rule, there is no stem; so `$* ' cannot be determined in that way. Instead, if the target name ends with a recognized suffix (see Old-Fashioned Suffix Rules ), `$* ' is set to the target name minus the suffix. For example, if the target name is `foo.c ', then `$* ' is set to `foo ', since `.c ' is a suffix. GNU make does this bizarre thing only for compatibility with other implementations of make . You should generally avoid using `$* ' except in implicit rules or static pattern rules.

If the target name in an explicit rule does not end with a recognized suffix, `$* ' is set to the empty string for that rule.

 

=========$?的一个例子==============

$? ' is useful even in explicit rules when you wish to operate on only the prerequisites that have changed. For example, suppose that an archive named lib is supposed to contain copies of several object files. This rule copies just the changed object files into the archive:

            lib: foo.o bar.o lose.o win.o ar r lib $?

Of the variables listed above, four have values that are single file names, and three have values that are lists of file names. These seven have variants that get just the file's directory name or just the file name within the directory. The variant variables' names are formed by appending `D ' or `F ', respectively. These variants are semi-obsolete in GNU make since the functions dir and notdir can be used to get a similar effect (see Functions for File Names ). Note, however, that the `D ' variants all omit the trailing slash which always appears in the output of the dir function. Here is a table of the variants:

` $(@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.

` $(@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 $@) '.

` $(*D) '
` $(*F) '
The directory part and the file-within-directory part of the stem; dir and foo in this example.

` $(%D) '
` $(%F) '
The directory part and the file-within-directory part of the target archive member name. This makes sense only for archive member targets of the form archive ( member ) and is useful only when member may contain a directory name. (See Archive Members as Targets .)

` $(<D) '
` $(<F) '
The directory part and the file-within-directory part of the first prerequisite.

` $(^D) '
` $(^F) '
Lists of the directory parts and the file-within-directory parts of all prerequisites.

` $(+D) '
` $(+F) '
Lists of the directory parts and the file-within-directory parts of all prerequisites, including multiple instances of duplicated prerequisites.

` $(?D) '
` $(?F) '
Lists of the directory parts and the file-within-directory parts of all prerequisites that are newer than the target.

Note that we use a special stylistic convention when we talk about these automatic variables; we write “the value of `$< '”, rather than “the variable < ” as we would write for ordinary variables such as objects and CFLAGS . We think this convention looks more natural in this special case. Please do not assume it has a deep significance; `$< ' refers to the variable named < just as `$(CFLAGS) ' refers to the variable named CFLAGS . You could just as well use `$(<) ' in place of `$< '.



 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值