Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can I check a particular gcc feature in configure.ac

Tags:

c++

c

gcc

autoconf

For example, gcc 4.7 has a new feature -Wnarrowing. In configure.ac, how can I test where a feature is supported by the current gcc or not?
There's a file in gnulibc, but doesn't make much sense to me.

like image 528
nzomkxia Avatar asked Jul 18 '14 07:07

nzomkxia


1 Answers

Both gcc and clang support -W[no-]narrowing and -W[no-]error=narrowing options.

With -std=c++11, gcc emits a warning by default, and clang emits an error by default. Even though you only mention gcc, I think you could extend the functionality check to compilers like clang that attempt to provide the same options and extensions. That might include Intel's icc too.

Let's assume you've selected the C++ compiler with AC_PROG_CXX, and have ensured that it's using the C++11 standard.

ac_save_CXXFLAGS="$CXXFLAGS"
CXXFLAGS="$CXXFLAGS -Werror -Wno-error=narrowing"
AC_LANG_PUSH([C++])

AC_COMPILE_IFELSE([AC_LANG_PROGRAM([],
  [[int i {1.0}; (void) i;]])],
  [ac_cxx_warn_narrowing=1], [ac_cxx_warn_narrowing=0])

AS_IF([test $ac_cxx_warn_narrowing -ne 0],
  [AC_MSG_RESULT(['$CXX' supports -Wnarrowing])])

AC_LANG_POP([C++])
CXXFLAGS="$ac_save_CXXFLAGS"

Compilation will only succeed if: 1) the compiler supports -Wnarrowing related options, which implies it supports -Werror, and: 2) recognizes C++11 initialization syntax.

Normally, configure.ac scripts and flags passed to configure should avoid -Werror, as it breaks too many internal tests. In this context, we ensure there are no other warnings besides the narrowing, which is why (void) i; is needed to prevent a warning about unused variables.

like image 119
Brett Hale Avatar answered Oct 11 '22 19:10

Brett Hale