Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Makefile: why command substitution can't work in $(shell) function?

Tags:

makefile

I have a variable in Makefile:

JAVABIN = $(shell dirname $(which java))

And when I echo the JAVA_HOME variable in Makefile, the variable definition complains:

dirname: missing operand
Try 'dirname --help' for more information.

When I quote the $(which java), the JAVABIN is ., so the result is wrong. And I didn't understand how make reads a Makefile, maybe it is the cause. Thank you very much.

like image 557
zhenguoli Avatar asked Jun 06 '16 04:06

zhenguoli


1 Answers

You need to escape the dollar:

JAVABIN = $(shell dirname $$(which java))

See 6.1 Basics of Variable References.


The specific error message you received was caused by the fact that the $(which java) piece expanded to the empty string, since it was an undefined variable. Hence the dirname system command ended up seeing no arguments, in which case it complains of a "missing operand".

like image 129
bgoldst Avatar answered Sep 30 '22 04:09

bgoldst