Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Four Dollar signs in Makefile

Tags:

I am reading the document of GNU Make. Here is an example

%.d: %.c

    @set -e; rm -f $@; \       $(CC) -M $(CPPFLAGS) $< > $@.$$$$; \       sed ’s,\($*\)\.o[ :]*,\1.o $@ : ,g’ < $@.$$$$ > $@; \       rm -f $@.$$$$ 

I tried this on a C++ program, and got the list of files

init3d.d init3d.d.18449 input.d input.d.18444 main.d main.d.18439

Here is what I found but don't understand in the same document

If you have enabled secondary expansion and you want a literal dollar sign in the prerequisites list, you must actually write four dollar signs (‘$$$$’).

I wonder what the four dollar signs "$$$$" mean actually? How do they 18449, 18444 or 18439?

Thanks and regards!

like image 629
Tim Avatar asked Aug 24 '09 02:08

Tim


People also ask

How do you put a dollar sign in Makefile?

Variable and function references in recipes have identical syntax and semantics to references elsewhere in the makefile. They also have the same quoting rules: if you want a dollar sign to appear in your recipe, you must double it ('$$').

What is dollar sign in Makefile?

$@ (“dollar at”) is part of the Makefile language. In your recipe, it refers to the thing it is going to build (above, that's prog ). Mnemonic: it's the target you're aiming at. $^ (“dollar caret”) refers to the dependencies/inputs (above, that's main. c ).

What does ?= Mean in Makefile?

?= indicates to set the KDIR variable only if it's not set/doesn't have a value. For example: KDIR ?= "foo" KDIR ?= "bar" test: echo $(KDIR)

How do I get rid of the dollar sign in Makefile?

I just learned that if you want to use a dollar sign inside a Makefile you need to escape $ with an extra $ , so double it. Otherwise make will think that you are accessing a make variable, not a shell one.


2 Answers

If make "secondary expansion" is enabled, $$$$ is required in order to generate a single $ in the actual output. $ is normally used to expand variables, call make functions, etc. $$ with secondary expansion enabled does something else, but otherwise it generates an actual $ in the output.

The shell that make uses to execute command-lines on Unix-like systems normally interprets $$ as expand to shell process ID. So, without secondary expansion enabled, $$$$ will turn into $$ in the output, which the shell will expand to the process ID.

(Using the shell process ID as a suffix is a simple way of trying to guarantee uniqueness of file name for a temporary file.)

like image 61
Barry Kelly Avatar answered Sep 21 '22 13:09

Barry Kelly


$$ will be converted to $, but in Makefile rules (which are shell expressions) you'll have to also escape the resulting $ using a \ or by using single quotes ' around your expression.

Here is an example that demonstrates it:

DOLLAR:=$$ dollar:     echo '$$'  >  $@     echo "\$$" >> $@     echo '$(DOLLAR)'  >> $@     echo "\$(DOLLAR)" >> $@     cat dollar 
like image 42
Sébastien Pierre Avatar answered Sep 21 '22 13:09

Sébastien Pierre