27

In makefile, ${CC} -o myfile myfile.c and $(CC) -o myfile myfile.c both work fine even without definition of CC. My question is:

  1. How to use $(..) and ${..}?
  • Are they just convert the string in {} or () to lowercase if that string is not defined?
  • Why does $${dir} need two $$ in
    for dir in ${DIR}; do (cd $${dir}; ${MAKE}); done
  1. Is there any differences between ${} and $()?

2 Answers 2

44

There is no difference between () and {} for Make.

If you use $$ in a recipe, then $ is "escaped" and passed to the shell. The shell may then make a difference between $() or ${}. But that is entirely up to the shell, and has nothing to do with Make or makefiles.

In the recipe command that you quote

for dir in ${DIR}; do (cd $${dir}; ${MAKE}); done

Make does this:

  • substitutes the value of DIR, for ${DIR}, and that could just as well have been $(DIR)
  • replaces $$ with $ ("escaping", so that $ could be passed to the shell)
  • substitutes the value of MAKE for ${MAKE}, again this could have been $(MAKE). The value of MAKE is automatically setup by Make, to the make executable that is being used.
  • passes the resulting string to shell for execution - the shell then interprets the one remaining ${} the way it wants.

CC, similarly to MAKE is one of those variables that are by default pre-defined by Make, that's why it "works" even if you don't set it yourself.

By the way, a better way to write this recipe for "target" is

.PHONY: $(DIR)
target: $(DIR)
$(DIR):
    $(MAKE) -C $@

Please consult the manual for explanation of things that are unclear.

1
-2

$(CC) - is evaluate ; this can be a command

Vs

${CC} - is dereference; just get value of a variable

1
  • 2
    Downvoted because this is for shell, not for Makefile. See accepted answer
    – Tino
    Jul 17, 2020 at 10:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.