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.
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.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments