First, shell realpath
and the GNU make realpath
function are different. Shell realpath
will return a path even if the file doesn't exist:
/home/me$ rm -f blahblah
/home/me$ realpath blahblah
/home/me/blahblah
But, GNU make's realpath
will return the empty string if the file doesn't exist.
So, why does the file not exist? Because make will expand all lines of the recipe before running any line of the recipe.
That means that the make functions like $(realpath ...)
and $(shell ...)
are expanded first, before the first line of the recipe (touch test
) is run... and so at the time they are expanded the test
file doesn't exist.
In general you never want to use make's $(shell ...)
function in a recipe and you can't use make constructs to "interact" with operations that happen inside your recipe. You should use shell functions for that:
test :
touch test;
echo $$(realpath test)
rm test
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…