I have the following gnu make script:
for hdrfile in $(_PUBLIC_HEADERS) ; do \
echo $(dir $$hdrfile) ; \
done
The _PUBLIC_HEADERS variable has a list of relative paths, like so:
./subdir/myheader1.h
./subdir/myheader2.h
The output I get from the for loop above is:
./
./
I expect to see:
./subdir/
./subdir/
What am I doing wrong? Note that if I change the code to:
echo $(dir ./subdir/myheader1.h)
it works in this case. I think maybe it has something to do with the for loop but I’m not sure.
You are confusing
makevariables (or functions) with shell variables when executing thefor-loop. Note that$(dir ...)is amakeconstruct that gets expanded bymakebefore the command is executed by the shell. However, you want the shell to execute that command inside the loop.What you could do is replace
$(dir)with the corresponding commanddirnamewhich gets executed by the shell. So it becomes:This should give the desired result.