I would like to use some gcc warning switchs that aren't available in older gcc versions (eg. -Wtype-limits).
Is there an easy way to check the gcc version and only add those extra options if a recent gcc is used ?
I would like to use some gcc warning switchs that aren't available in older gcc versions (eg. -Wtype-limits).
Is there an easy way to check the gcc version and only add those extra options if a recent gcc is used ?
I just encountered this problem where I needed to test the first two digits of gcc and wanted a more readable option than the clever sed hackery above. I used bc to do the comparison since it supports floating point (expr treats non-integers as strings):
If they release gcc 4.10 after gcc 4.9, then a bit of sed hacking is necessary, but this is still pretty readable:
To transform full 3-part gcc version (not only first digit) into numerical format, suitable for comparison (e.g.
40701
) useWhich addresses the possibility of double-digit numbers in any of the version part, and possibility of missing 3-rd part of the version in output of
gcc -dumpversion
(which is the case in some earlier gcc versions).So to test the version in makefile, use something like (note
$$
inside last sed command)I've made a ready-to-use IF_GCC macro, based on the answers above:
Usage:
$(call MY_IF_GCC,ge,30305,-fan_option_for_gcc_ge_3.3.5)
As the second argument, you can use any operator of those supported by test(1): eq, gt, lt, ge, le etc.
If cc -V doesn't work for you, replace it with gcc -dumpversion or whatever suitable
Hope that helps.
Are you using something like autoconf?
It might be worth invoking a 'dummy' compile via gcc with the flag enabled and if that one fails because the compiler doesn't recognise the flag, you can fall back to the command line that doesn't use the newer warning options.
Following Chris, but using awk
note
$
needs to be escaped in Makefile with another$
.Sometimes you want to display the version with the extra info.
gcc (Ubuntu 4.8.1-2ubuntu1~12.04) 4.8.1