DEV Community

10x learner
10x learner

Posted on • Originally published at 10xlearner.com on

How to handle user variable in a Makefile

Hello ! I’m Xavier Jouvenot and in this small post, we are going to see how to handle user variable in a Makefile.

Self promotion: You can find other articles on my website 😉

Problematic

When creating some make commands in your Makefile, you may want to have some parameters accessible to the user, so that he could either pass some inputs to your command, or specify some elements for which you will have set some default value.

There are several ways to do it, but we are going to see the simplest and most powerful solution to me. Hopefully, you will find it convenient too 😉

Solution

Let’s dive right in the Makefile and see what defining a user variable looks like:

VARIABLE ?= DEFAULT_VALUE

command:
    echo "${VARIABLE}"
Enter fullscreen mode Exit fullscreen mode

Pretty simple, isn’t it ?! Yet, if you don’t know it exists, you can’t guess it 😆
Everything is done by the operator ?= which is going to set our VARIABLE only if it has not been defined before.

Now, all we need is to pass a value to the variable like that:

make foo # uses the default value specified in the Makefile for VARIABLE
make foo VARIABLE= # set VARIABLE to an empty value
make foo VARIABLE=SPECIFIC_VALUE # set VARIABLE to a SPECIFIC_VALUE
Enter fullscreen mode Exit fullscreen mode

And voilà now, you know that the operator ?= exists and how to use it 😉


Thank you all for reading this article, And until my next article, have a splendid day 😉

Interesting links

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more