Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I perform arithmetic in a makefile?

Tags:

makefile

Is it possible to perform some operations on variables in a makefile? For instance, defining

JPI=4 JPJ=2 

is it possible to define in the same makefile a variable JPIJ equal to the expanded value of $(JPI)*$(JPJ)?

like image 964
malaboca Avatar asked Dec 18 '09 03:12

malaboca


People also ask

What does += mean in makefile?

+= is used for appending more text to variables e.g. objects=main.o foo.o bar.o. objects+=new.o. which will set objects to 'main.o foo.o bar.o new.o' = is for recursively expanded variable.

What is $$ in makefile?

$$ means be interpreted as a $ by the shell. the $(UNZIP_PATH) gets expanded by make before being interpreted by the shell.


1 Answers

Using Bash arithmetic expansion:

SHELL=/bin/bash JPI=4 JPJ=2 all:     echo $$(( $(JPI) * $(JPJ) )) 

The first line is to choose the Bash shell instead of the default (sh). Typically, sh doesn't support arithmetic expansion. However in Ubuntu, /bin/sh is provided by Dash, which supports this feature. So that line could be skipped.

The double dollar sign is because we want the expansion to be done by the shell. Note: the JPI and JPJ variables are expanded by make first, then the expression is passed to bash like this:

$(( 4 * 2 )) 
like image 78
Dominic Avatar answered Sep 16 '22 15:09

Dominic