Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

autotools: Enable compiler warnings

for an autotools-based C project, I'd like to get some more warnings from the compiler (e.g. at least -Wall in CFLAGS). What is the prefered way to enable compiler flags without breaking anything? Is there a m4 macro that tests if a given compiler flag is understood by the compiler? With such a macro I could do

TEST_AND_USE(-Wall -Wextra <other flags>)

Thanks

like image 844
Uli Schlachter Avatar asked Aug 31 '10 18:08

Uli Schlachter


2 Answers

You can just use AC_TRY_COMPILE:

AC_MSG_CHECKING(whether compiler understands -Wall)
old_CFLAGS="$CFLAGS"
CFLAGS="$CFLAGS -Wall"
AC_TRY_COMPILE([],[],
  AC_MSG_RESULT(yes),
  AC_MSG_RESULT(no)
  CFLAGS="$old_CFLAGS")

2015 addition: AC_TRY_COMPILE is now deprecated, instead you should use AC_COMPILE_IFELSE:

AC_MSG_CHECKING(whether compiler understands -Wall)
old_CFLAGS="$CFLAGS"
CFLAGS="$CFLAGS -Wall"
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([],[])],
  AC_MSG_RESULT(yes),
  AC_MSG_RESULT(no)
  CFLAGS="$old_CFLAGS")
like image 163
caf Avatar answered Oct 01 '22 15:10

caf


Don't bother changing the configure.ac at all. Just call ./configure with the CFLAGS you care about:

./configure CFLAGS='-Wall -Wextra -O2 -g'
like image 29
Jack Kelly Avatar answered Oct 01 '22 14:10

Jack Kelly