Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gcc: ignore unrecognized option

Tags:

gcc

makefile

Is there a way to make gcc ignore an invalid option, instead of dying with "unrecongized option"? Reason is I want to use an option only available in later versions of gcc (-static-libstdc++), but it should also compile on older compilers. I could check for gcc version in the makefile but it is a bit ugly.

like image 677
Rolle Avatar asked Nov 04 '22 15:11

Rolle


2 Answers

no, but you can set the flags based on the gcc version as follows:

version=`gcc --version | head -1 | cut -d ' ' -f3`

if [ `echo -e "$version\n4.6.1" | sort -V -C; echo $?` == 0 ]; then 
    flags = -static-libstdc++;
fi

gcc $flags ...

(Disclaimer: I'm not sure which version first uses static-libstdc++, 4.6.1 is just a guess).

John

like image 135
John Avatar answered Jan 04 '23 15:01

John


You can run gcc and check if it accepts the flag:

STATIC_LIBCPP_FLAG := $(shell if gcc -static-libstdc++ --version 2>&1 | grep -q 'unrecognized option'; then true; else echo -static-libstdc++; fi)

CFLAGS += $(STATIC_LIBCPP_FLAG)
like image 24
Chris Dodd Avatar answered Jan 04 '23 13:01

Chris Dodd