如何在 Makefile 中使用长变量名?

Zen*_*Zen 1 make variable

今天我正在学习make命令,我发现它似乎可以bash通过读取Makefile当前目录来执行任何命令。

但是,我遇到了一个问题。使用变量时,系统似乎只会读取变量的第一个字符。

以下是我的文件和运行结果:

# FILE CONTENT
Z="zen_on_the_moon"
now=$(date)

fun:
    touch $Z
    echo $now
    echo "file created on" $now >> $Z

# RUNNING IT
=>make fun
touch "zen_on_the_moon"
echo ow
ow
echo "file created on" ow >> "zen_on_the_moon"
Run Code Online (Sandbox Code Playgroud)

我应该如何使用变量nowMakefile以下fun项目?

cuo*_*glm 6

在 中Makefile,您使用语法来引用变量$(var_name)。使用$var_name引起除美元符号$、左括号(或左括号以外的第一个字符{被视为变量名。

在 中$now,您实际上获得了变量的内容,$n然后是文字字符串ow

所以你需要:

$(now)
Run Code Online (Sandbox Code Playgroud)

获取名为 的变量的内容now

另请注意,now=$(date)获取 named 变量的内容date而不是 command 的结果date。您需要使用shell 函数

now=$(shell date)
Run Code Online (Sandbox Code Playgroud)