realpath 不适用于新创建的文件

realpath not working on newly created files

test :
    touch test
    echo $(realpath test)
    echo $(shell realpath test)
    rm test

当我运行make时,$(realpath test)returns是空字符串,而$(shell realpath test)returns是预期的结果。为什么是这样?我试过使用 .ONESHELL,但没有任何区别。

首先,shell realpath 和GNU make realpath 函数不同。 Shell realpath 将 return 一个路径,即使文件不存在也是如此:

/home/me$ rm -f blahblah
/home/me$ realpath blahblah
/home/me/blahblah

但是,如果文件不存在,GNU make 的 realpath 将 return 为空字符串。

那么,为什么文件不存在呢?因为 make 会在运行宁食谱的任何行之前扩展食谱的所有行

这意味着像$(realpath ...)$(shell ...)这样的make函数首先展开,在配方的第一行(touch test)是运行之前...因此在扩展它们时 test 文件不存在。

通常,您永远不想在配方中使用 make 的 $(shell ...) 函数,并且您不能使用 make 构造与配方中发生的操作“交互”。您应该为此使用 shell 函数:

test :
        touch test;
        echo $$(realpath test)
        rm test