有没有办法打印递归变量的未扩展定义?我有一个复杂的构建系统,用户可以设置一些值。我想将用户定义回显到另一个文件,以供以后使用。
例如,
externals = $(HOME)/externals
all:
echo $(externals)
不起作用,因为它使用 HOME 的当前定义进行回显。我希望它能回响
文字字符串 $(HOME)/externals
而不扩展 $(HOME)
。
最佳答案
value
function可能就是您想要的。
8.8 The value Function
The value function provides a way for you to use the value of a variable without having it expanded. Please note that this does not undo expansions which have already occurred; for example if you create a simply expanded variable its value is expanded during the definition; in that case the value function will return the same result as using the variable directly.
The syntax of the value function is:
$(value variable)
Note that variable is the name of a variable, not a reference to that variable. Therefore you would not normally use a ‘$’ or parentheses when writing it. (You can, however, use a variable reference in the name if you want the name not to be a constant.)
The result of this function is a string containing the value of variable, without any expansion occurring. For example, in this makefile:
FOO = $PATH all: @echo $(FOO) @echo $(value FOO)
The first output line would be ATH, since the “$P” would be expanded as a make variable, while the second output line would be the current value of your $PATH environment variable, since the value function avoided the expansion.
The value function is most often used in conjunction with the eval function (see Eval Function).
除此之外,您还需要在 echo
行上使用单引号,否则 shell 会扩展您的内容。
$ cat Makefile
externals = $(HOME)/externals
all:
echo $(externals)
allv:
echo $(value externals)
allvq:
echo '$(value externals)'
$ make all
echo /home/user/externals
/home/user/externals
$ make allv
echo $(HOME)/externals
/bin/sh: HOME: command not found
/externals
$ make allvq
echo '$(HOME)/externals'
$(HOME)/externals
关于gnu-make - 在 gnu make 中打印未展开的递归变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27621130/