Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function and difference between $() and ${} in Makefile

Tags:

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 $()?
like image 603
AuA Avatar asked Oct 20 '13 07:10

AuA


People also ask

What does $( make mean in makefile?

And in your scenario, $MAKE is used in commands part (recipe) of makefile. It means whenever there is a change in dependency, make executes the command make --no-print-directory post-build in whichever directory you are on.

What is $( CC in makefile?

$(CC) -o executablename fileA.o fileB.o fileC.o -lm. This is a dependency and it means that the target "myprogram" should invoke the compiler whenever fileA.o, fileB.o, or fileC.o change. In particular, it invokes the compiler referred to in the variable "CC" and creates an executable called "executablename".

What is $@ in makefile?

$@ is the name of the target being generated, and $< the first prerequisite (usually a source file). You can find a list of all these special variables in the GNU Make manual.

What is Patsubst in makefile?

$(patsubst PATTERN,REPLACEMENT,TEXT) Finds whitespace-separated words in TEXT that match PATTERN and replaces them with REPLACEMENT. Here PATTERN may contain a % which acts as a wildcard, matching any number of any characters within a word.


1 Answers

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.

like image 89
Mark Galeck Avatar answered Oct 20 '22 23:10

Mark Galeck