1

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; \
fi

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; \
fi)

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 Sep 20 '17 at 21:20
3

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)
  CFLAGS += -D _POSIX_C_SOURCE=199309L
endif

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. – Mark Plotnick Sep 20 '17 at 19:44
  • Your answer has helped me grow in strength and in wisdom. Thanks! – Nikos Sep 21 '17 at 10:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.