GNU make manual 翻译(八十四)

继续翻译

4.4.1 Wildcard Examples                        
-----------------------                        
                        
Wildcards can be used in the recipe of a rule, where they are expanded by the shell.  For example, here is a rule to delete all the object files: 
     clean:                        
             rm -f *.o                        
                        
   Wildcards are also useful in the prerequisites of a rule.  With the following rule in the makefile, `make print' will print all the `.c' files that have changed since the last time you printed them:                        
                        
     print: *.c                        
             lpr -p $?                        
             touch print                        
                        
This rule uses `print' as an empty target file; see *note Empty Target Files to Record Events: Empty Targets.  (The automatic variable `$?' is used to print only those files that have changed; see *note Automatic Variables::.) 
                        
   Wildcard expansion does not happen when you define a variable. 
Thus, if you write this:                        
                        
     objects = *.o                        
                        
then the value of the variable `objects' is the actual string `*.o'.However, if you use the value of `objects' in a target or prerequisite, wildcard expansion will take place there.  If you use the value of `objects' in a recipe, the shell may perform wildcard expansion when the recipe runs.  To set `objects' to the expansion, instead use:              
     objects := $(wildcard *.o) 
*Note Wildcard Function::.                        

4.4.1 通配符例子 
-----------------------
通配符可以被使用在规则的片段中,在此时它们被shell所解释/展开。例如,这里有一个删除所有目标文件的规则:
clean:
  rm -f *.o

通配符在一个规则的前提条件中也是很有用的。通过makefile的下列例子,

make print 会打印出所有自从上次打印后改变的.c 文件:

print: *.c
  lpr -p $?
  touch print

这个规则 使用 print 作为空的目的文件;参见 *note Empty Target Files to Record Events: Empty Targets (自动变量 $ 用来只打印出那些发生了改变的文件;参见 *note Automatic Variables::)

在你定义一个变量的时候,通配符扩展不会发生,所以,如果你这么写:

objects = *.o

那么变量 objects 的值会是真的字符串 '*.o'。但是如果你是在目的或者前提条件中使用 objects的值,通配符扩展还是会发生。如果你在片段里使用 objects 的值,当片段运行时,通配符扩展会发生。

如果想要把 objects 设置在扩展里,应当使用:

objects := $(wildcard *.o)

*Note Wildcard Function::.

后文待续

原文地址:https://www.cnblogs.com/gaojian/p/2693202.html