在 Makefile 中使用 bash 变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29500434/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using bash variables in Makefile
提问by Lily
I want to use the bash timing variables in my makefile for example in my terminal I can do this and it works
我想在我的 makefile 中使用 bash 计时变量,例如在我的终端中我可以这样做并且它可以工作
MY_TIME=$SECONDS
echo $MY_TIME
but when I write this on my makefile it does not work
但是当我在我的 makefile 上写这个时它不起作用
how can I use these two lines in my make file?
如何在我的 make 文件中使用这两行?
this is what I'm doing
这就是我正在做的
.PHONY: myProg
myProg:
MY_TIME=$SECONDS
echo $MY_TIME
After Etan Reisner' answer
在 Etan Reisner 的回答之后
This is what I have now
这就是我现在所拥有的
.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
但是我的 echo 的结果是一个空行,它看起来不像是在存储日期
采纳答案by Etan Reisner
By default make uses /bin/sh
as the shell which executes recipe lines.
默认情况下,make/bin/sh
用作执行配方行的 shell。
Presumably /bin/sh
doesn't support the SECONDS
variable.
大概/bin/sh
不支持该SECONDS
变量。
You can tell make to use a different shell by assigning a value to the SHELL
variable (i.e. SHELL := /bin/bash
).
您可以通过为SHELL
变量(即SHELL := /bin/bash
)分配一个值来告诉 make 使用不同的 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.
这样做将使SECONDS
可用但仍然不允许您在配方行之间携带变量值,因为每个配方行都在其自己的 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
or
或者
.PHONY: myProg
myProg:
MY_TIME=$SECONDS; \
echo $MY_TIME
That being said you would almost certainly be better off notdoing this and instead using something like date
invoked at the start/end of the recipe or time
invoked onthe command to be timed directly instead.
话虽如此,您几乎肯定最好不要这样做,而是使用类似date
在配方开始/结束时time
调用或在命令上调用以直接计时的东西。
.PHONY: myProg
myProg:
date
# Do something
date
or
或者
.PHONY: myProg
myProg:
time some_command
回答by Cavaz
the dollar sign ($MY_TIME
) refers to make variables, which are not the same as bash variables.
美元符号 ( $MY_TIME
) 指的是make 变量,它与bash 变量不同。
To access a bash variableyou must escape the dollar using the double dollar notation ($$MY_TIME
).
要访问bash 变量,您必须使用双美元符号 ( $$MY_TIME
)转义美元。
.PHONY: myProg
myProg:
MY_TIME=$$SECONDS ; echo $$MY_TIME
As already mentioned in Etan answeryou can't split the code into multiple lines (unless you are using the backslash) since each command executes in a different subshell, making variables inaccessible to other lines.
正如在Etan answer 中已经提到的,您不能将代码分成多行(除非您使用反斜杠),因为每个命令都在不同的子 shell 中执行,使得其他行无法访问变量。
In the following example the value of SECONDS
will be always 0
, since it get reset by the spawn of the shell for the second line.
在以下示例中, 的值SECONDS
将始终为0
,因为它会在第二行的 shell 生成时重置。
.PHONY: myProg
myProg: # WRONG
MY_TIME=$$SECONDS
echo $$MY_TIME
回答by mrflash818
PROGRAM_NAME = myLittleProgram
...
$(PROGRAM_NAME) : myLittleProgram.o
I know the above works, as it is in my own makefile (program names and object names changed to protect the innocent).
我知道上述工作,因为它在我自己的 makefile 中(程序名称和对象名称已更改以保护无辜者)。
"Variable references can be used in any context: targets, dependencies, commands, most directives, and new variable values. Here is an example of a common case, where a variable holds the names of all the object files in a program:"
“变量引用可用于任何上下文:目标、依赖项、命令、大多数指令和新变量值。这是一个常见情况的示例,其中变量保存程序中所有目标文件的名称:”
objects = program.o foo.o utils.o
program : $(objects)
cc -o program $(objects)
$(objects) : defs.h
http://web.mit.edu/gnu/doc/html/make_6.html
http://web.mit.edu/gnu/doc/html/make_6.html