How do I perform arithmetic in a makefile?

Using Bash arithmetic expansion:

    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 ))

Leave a Comment