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

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

问题描述

我要使用bash的时间变量的makefile我
例如在我的终端,我可以做到这一点,它的工作原理

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

我怎么能在我的化妆文件中使用这两行?

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

这是我在做什么。

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

在伊坦赖斯纳的回答

这是我现在有

.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

推荐答案

在默认情况下让使用 / bin / sh的作为执行配方行外壳。

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

presumably / bin / sh的不支持变量。

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

您可以告诉make通过分配一个值 SHELL 变量(即 SHELL使用不同的外壳:= /斌/庆典)。

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

这样做,这将使可用秒,但仍然无法让每个食谱线在其自己的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

话虽这么说,你几乎肯定会更好的不调用的这样做的,而不是使用像日期在的开始/结束配方或时间调用的的命令直接而不是定时。

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天全站免登陆