Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
731 views
in Technique[技术] by (71.8m points)

makefile - realpath not working on newly created files

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

When I run make, $(realpath test) returns the empty string, whereas $(shell realpath test) returns the exptected result. Why is this? I've tried using .ONESHELL but it doesn't make a difference.

question from:https://stackoverflow.com/questions/65837227/realpath-not-working-on-newly-created-files

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share

2.1m questions

2.1m answers

63 comments

56.6k users

...