I want to add another option to the CFLAGS make variable, depending on the result of a shell command that i want to execute outside of a recipe in my "configuration" section of the makefile. This is what i have come up with:

GCC_VERSION := $(shell gcc -dumpversion); \
if [[ ${GCC_VERSION} > 5.0 ]] ; then \
    CFLAGS += -D _POSIX_C_SOURCE=199309L; \

At first i execute the command with the shell make function as you see above. If i execute the above it doesn't add this define flag. I intentionally do this on linux with GCC Version 5.4.0. I believe this is wrong because then i have to create a new shell to execute the conditional statement. In that new shell though the GCC_VERSION variable will not exist. I could be wrong though.

If i do like this then (all in one shell):

$(shell GCC_VERSION=$(gcc -dumpversion); \
if [[ ${GCC_VERSION} > 5.0 ]] ; then \
    CFLAGS += -D _POSIX_C_SOURCE=199309L; \

i get error:

*** recipe commences before first target.  Stop.

Yeah, very confusing.

If someone could help i would appreciate it. Thanks.

  • 1
    The error means that you have an out-of-place shell command. That's because your if follows the bash syntax, not the make syntax.
    – xhienne
    Commented Sep 20, 2017 at 21:20

1 Answer 1


There are many solutions, including this one. In your Makefile use

VERSION5 := $(shell \
 GCC_VERSION=$$(gcc -dumpversion); \
 [[ $$GCC_VERSION > 5.0 ]]; \
 echo $$? )

ifeq (${VERSION5}, 0)

Note in particular, that you need to use $$ for every $ in your shell script. This shell echos 0 if the string comparision with 5.0 is true, else 1, and this is saved in make variable VERSION5.

Then the ifeq test in the Makefile compares the variable with 0 and if it matches edits the CFLAGS variable.

  • 3
    You might need to use [...] instead of [[...]] if the shell that gmake runs is old or strictly POSIX conforming. Commented Sep 20, 2017 at 19:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .