GNU make manual 翻译( 一百五十)

本文介绍了如何在Makefile中使用define指令定义一系列重复利用的命令序列,即片段包,并提供了一个具体的例子来展示如何定义和使用这些命令序列。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

继续翻译

5.8 Defining Canned Recipes
===========================

When the same sequence of commands is useful in making various targets,
you can define it as a canned sequence with the `define' directive, and
refer to the canned sequence from the recipes for those targets.  The
canned sequence is actually a variable, so the name must not conflict
with other variable names.

   Here is an example of defining a canned recipe:

     define run-yacc =
     yacc $(firstword $^)
     mv y.tab.c $@
     endef
   
Here `run-yacc' is the name of the variable being defined; `endef'
marks the end of the definition; the lines in between are the commands.
The `define' directive does not expand variable references and function
calls in the canned sequence; the `$' characters, parentheses, variable
names, and so on, all become part of the value of the variable you are
defining.  *Note Defining Multi-Line Variables: Multi-Line, for a
complete explanation of `define'.

   The first command in this example runs Yacc on the first
prerequisite of whichever rule uses the canned sequence.  The output
file from Yacc is always named `y.tab.c'.  The second command moves the
output to the rule's target file name.

5.8 定义片段包
===========================

当同一个序列的命令在构建各种不同的目的时有用的时候,你可以通过 define 指令把它们定义为一个片段包,参照此包来为那些目的服务。片段包实际上是一个变量,因此名字必须不和其他变量名冲突。

这里有一个定义片段包的例子:

define run-yacc =
yacc $(firstword $^)
mv y.tab.c $@
endef

这里 'run-yacc' 是被定义的变量的名字; 'endef ' 标记了定义的结束;
两者之间是指令。'define' 指令并不扩展片段包中的变量和函数参照。'$' 符号, 括号, 变量名等等, 都成为你所定义的变量的值的一部份。如果要找完整的说明,请参考 *Note Defining Multi-Line Variables: Multi-Line。

在这个例子中的第一个命令在某个运用此片段包的规则的前提条件里,运行Yacc。从Yacc来的输出文件名总是 y.tab.c。第二个命令删除规则目的文件名的输出。

后文待续

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值