Pergunta

Here is what it finally took to get my code in my makefile to work Line 5 is the question area

BASE=50
INCREMENT=1
FORMATTED_NUMBER=${BASE}+${INCREMENT}

all:
        echo $$((${FORMATTED_NUMBER}))

why do i have to add two $ and two (( )) ?

Formatted_Number if i echo it looks like "50+1" . What is the logic that make is following to know that seeing $$(("50+1")) is actually 51?

sorry if this is a basic question i'm new to make and dont fully understand it.

Foi útil?

Solução

First, whenever asking questions please provide a complete example. You're missing the target and prerequisite here so this is not a valid makefile, and depending on where they are it could mean very different things. I'm assuming that your makefile is something like this:

BASE=50
INCREMENT=1
FORMATTED_NUMBER=${BASE}+${INCREMENT}

all:
        echo $$((${FORMATTED_NUMBER}))

Makefiles are interesting in that they're a combination of two different formats. The main format is makefile format (the first five lines above), but inside a make recipe line (that's the last line above, which is indented with a TAB character) is shell script format.

Make doesn't know anything about math. It doesn't interpret the + in the FORMATTED_NUMBER value. Make variables are all strings. If you want to do math, you have to do it in the shell, in a shell script, using the shell's math facilities.

In bash and other modern shells, the syntax $(( ...expression... )) will perform math. So in the shell if you type echo $((50+1)) (go ahead and try it yourself) it will print 51.

That's why you need the double parentheses ((...)): because that's what the shell wants and you're writing a shell script.

So why the double $? Because before make starts the shell to run your recipe, it first replaces all make variable references with their values. That's why the shell sees 50+1 here: before make started the shell it expanded ${FORMATTED_NUMBER} into its value, which is ${BASE}+${INCREMENT}, then it expanded those variables so it ends up with 50+1.

But what if you actually want to use a $ in your shell script (as you do here)? Then you have to tell make to not treat the $ as introducing a make variable. You do this by doubling it, so if make sees $$ then it does not think that's a make variable, and sends a single $ to the shell.

So for the recipe line echo $$((${FORMATTED_NUMBER})) make actually invokes a shell script echo $((50+1)).

Outras dicas

You can use this in BASH:

FORMATTED_NUMBER=$((BASE+INCREMENT))

Is using non BASH use:

FORMATTED_NUMBER=`echo "$BASE + $INCREMENT" | bc`
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top