分配时计算Makefile变量 [英] Computing Makefile variable on assignment

查看:70
本文介绍了分配时计算Makefile变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Makefile中,我试图将shell命令的结果分配给变量:

In a Makefile, I'm trying to assign the result of a shell command to a variable:

TMP=`mktemp -d /tmp/.XXXXX`

all:
    echo $(TMP)
    echo $(TMP)

但是

$ make Makefile all

回显2个不同的值,例如:

is echoing 2 different values, eg:

/tmp/.gLpm1T
/tmp/.aR4cDi

在变量分配中计算mktemp的语法是什么?

What is the syntax for mktemp to be computed on variable assignment?

谢谢.

推荐答案

这取决于make的风格.使用GNU Make,您可以使用:=代替=

It depends on the flavour of make. With GNU Make, you can use := instead of = as in

TMP:=$(shell mktemp -d /tmp/.XXXXX)

编辑正如Novelocrat指出的那样,=赋值与:=赋值的不同之处在于,使用=赋值的值将在替换期间求值(因此,每次变量),而:=分配的变量将只对它们的值进行一次评估(在分配期间),因此,此后这些值是固定的.有关详细说明,请参见GNU Make的文档.

Edit As pointed out by Novelocrat, the = assignment differs from := assignment in that values assigned using = will be evaluated during substitution (and thus, each time, the variable is used), whereas := assigned variables will have their values evaluated only once (during assignment), and hence, the values are fixed after that. See the documentation of GNU Make for a more detailed explanation.

但是,为了使该值在赋值后真正地为常数,它不应包含外壳程序可能特有的任何部分(make调用以实际运行更新规则等).特别是,最好避免反引号.相反,请使用GNU make的内置shell函数以及类似的函数来实现您的目标.

In order for the value to be truly constant after assignment, though, it should not contain any parts, which might be special to the shell (which make calls in order to actually run the update rules, etc.) In particular, backticks are best avoided. Instead, use GNU make's built-in shell function and similar to achieve your goals.

这篇关于分配时计算Makefile变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆