GNU make manual 翻译( 一百三十一)

本文介绍了Makefile中如何使用变量,并详细解释了变量和函数的引用规则。特别关注于make变量与shell变量的区别,以及如何正确地在Makefile的命令片段中使用这些变量。

继续翻译

5.1.2 Using Variables in Recipes
--------------------------------

The other way in which `make' processes recipes is by expanding any
variable references in them (*note Basics of Variable References:
Reference.).  This occurs after make has finished reading all the
makefiles and the target is determined to be out of date; so, the
recipes for targets which are not rebuilt are never expanded.

   Variable and function references in recipes have identical syntax and
semantics to references elsewhere in the makefile.  They also have the
same quoting rules: if you want a dollar sign to appear in your recipe,
you must double it (`$$').  For shells like the default shell, that use
dollar signs to introduce variables, it's important to keep clear in
your mind whether the variable you want to reference is a `make'
variable (use a single dollar sign) or a shell variable (use two dollar
signs).  For example:

     LIST = one two three
     all:
             for i in $(LIST); do \
                 echo $$i; \
             done

results in the following command being passed to the shell:

     for i in one two three; do \
         echo $i; \
     done

which generates the expected result:

     one
     two
     three

5.1.2 在片段中使用变量
--------------------------------

make 处理片段的另一个方式是通过扩展任何的片段中的变量参照(*note Basics of Variable References: Reference.) 。这发生在当make 读取完毕所有的makefile并且目的被认为是过期的时候;因此,未被重新构建的目的的片段不会被展开。

makefile中的片段的变量和函数参照有着相同的语法和语义。它们也有同样的引用规则。如果你想要你的片段中出现一个美元符号,你必须使用$$。

对于缺省shell那样的shell, 使用美元符号来引入变量,保持头脑清醒地了解你所要使用的变量是否是 make 的变量(使用一个美元符)还是说是shell 的变量(使用两个美元符),是很重要的。例如:

LIST = one two three
all:
for i in $(LIST); do \
echo $$i; \
done

形成如下传递给shell的命令:

for i in one two three; do \
echo $i; \
done

得到如下的结果:

one
two
three

后文待续

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值