在 Makefile 中使用 bash 变量 [英] Using bash variables in Makefile

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

问题描述

我想在我的 makefile 中使用 bash 计时变量例如,在我的终端中,我可以做到这一点并且它可以工作

I want to use the bash timing variables in my makefile for example in my terminal I can do this and it works

 MY_TIME=$SECONDS 
 echo $MY_TIME

但是当我在我的 makefile 上写这个时它不起作用

but when I write this on my makefile it does not work

如何在我的 make 文件中使用这两行?

how can I use these two lines in my make file?

这就是我正在做的事情

.PHONY: myProg
myProg:
      MY_TIME=$SECONDS 
      echo $MY_TIME

在 Etan Reisner 的回答之后

After Etan Reisner' answer

这就是我现在拥有的

.PHONY: myProg
 myProg:
        MY_TIME= date; echo $MY_TIME

但是我的回显结果是一个空行,它看起来不像是在存储日期

but the result of my echo is an empty line, it does not look like it is storing the date

推荐答案

默认情况下 make 使用 /bin/sh 作为 shell 来执行配方行.

By default make uses /bin/sh as the shell which executes recipe lines.

大概 /bin/sh 不支持 SECONDS 变量.

Presumably /bin/sh doesn't support the SECONDS variable.

您可以通过为 SHELL 变量(即 SHELL :=/bin/bash)赋值来告诉 make 使用不同的 shell.

You can tell make to use a different shell by assigning a value to the SHELL variable (i.e. SHELL := /bin/bash).

这样做将使 SECONDS 可用,但仍然不允许您在配方行之间携带变量值,因为每个配方行都在其自己的 shell 中运行.

Doing that will make SECONDS available but will still not allow you to carry a variable value between recipe lines as each recipe line is run in its own shell.

因此,要执行您想要的操作,您需要将这两行写在一行上,或者在换行符上继续该行.

So to do what you want you would need to write both of those lines on one line or continue the line over the newline.

.PHONY: myProg
myProg:
      MY_TIME=$SECONDS; echo $MY_TIME

.PHONY: myProg
myProg:
      MY_TIME=$SECONDS; 
      echo $MY_TIME

话虽如此,这样做几乎肯定会更好,而是使用在配方开始/结束或时间调用的date之类的东西 on 调用了要直接计时的命令.

That being said you would almost certainly be better off not doing this and instead using something like date invoked at the start/end of the recipe or time invoked on the command to be timed directly instead.

.PHONY: myProg
myProg:
      date
      # Do something
      date

.PHONY: myProg
myProg:
      time some_command

这篇关于在 Makefile 中使用 bash 变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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