18

I saw the following Makefile online (here):

hello:

clean:
    $(RM) hello

When there is a hello.c file in the same directory with the Makefile, make command in Terminal builds hello executable. When make clean is run, hello executable is removed by rm -f hello instead. So, $(RM) hello means rm -f hello here.

  • What does $(FOO) mean? Is it a special syntax of Makefile, or something bash command?
  • Can I run other commands as well as $(RM), like $(PWD)?
1
  • $() is variable expansion in make.
    – 123
    Dec 1, 2016 at 9:55

3 Answers 3

25

It's a Makefile variable. There are explicit variables (which are defined inside a Makefile) and implicit variables (defined by make, can be overriden by you).

You can see a list of implicit variables with this flag:

make -p

some of the most common variables can be found at: 10.3 Variables Used by Implicit Rules

You can expand a variable with the syntax $(NAME) or ${NAME}

0
10

$(RM) is a reference to a "make" variable (or macro in POSIX lingo). These come in two syntax flavors, with identical semantics: $(NAME) and ${NAME} (and there are one letter macros not needing parentheses, such as $a).

POSIX specifies many variables, errrrr, macros, to have a default:

MAKE=make
AR=ar
ARFLAGS=-rv
YACC=yacc
YFLAGS=
LEX=lex
LFLAGS=
LDFLAGS=
CC=c99
CFLAGS=-O 1
FC=fort77
FFLAGS=-O 1
GET=get
GFLAGS=
SCCSFLAGS=
SCCSGETFLAGS=-s

Interestingly, RM isn't one of them. The default value for the RM variable in your make implementation apparently is rm -f (and your make runs in non-POSIX mode by default).

Note that while $(PWD) has the same syntax as a shell command substitution (and ${PWD} the same as a shell parameter), they are completely different things. To answer your question, no, you can't expect $(PWD) as a "make" macro to run the pwd utility or expand to the current working directory. An undefined macro will be expanded to an empty string without "make" even raising an eyebrow.

2
  • Thanks Jens for answering! What is non-POSIX mode? Is my make run in some other mode because it's run on macOS, which I use?
    – Keita
    Dec 1, 2016 at 10:57
  • @Keita No "make" I know of runs in POSIX mode by default. There are always a ton of non-POSIX features available. For POSIX mode, POSIX says that you need a special target named .POSIX: near the top of your makefile.
    – Jens
    Dec 1, 2016 at 12:13
2

It's a make file variable. Previously or in your env you have RM="rm -f" syntax depending on the shell or if it's in the make itself, and now you're just executing.

You can run pwd, but to use $(PWD), you need to set PWD="pwd".

0

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.