zhenguoli
zhenguoli

Reputation: 2308

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

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.

Upvotes: 8

Views: 3573

Answers (1)

bgoldst
bgoldst

Reputation: 35314

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".

Upvotes: 17

Related Questions