views:

79

answers:

4

Hi,

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

+2  A: 

I do this:

# debug compilation
AC_ARG_ENABLE(debug,
    AC_HELP_STRING(--enable-debug, [Debug compilation (Default = no)]),
    enable_debug=$enableval, enable_debug=no)

if test "$enable_debug" = "yes" ; then
    CFLAGS="$CFLAGS  -g -O0 -Wall -Wno-uninitialized"
    CXXFLAGS="$CXXFLAGS -g -O0 -Wall -Wno-uninitialized"
fi

it is a low-tech solution, but you do not have to accommodate all compilers

aaa
If you don't want to accomodate all compilers, why are you using Autotools? :)
mathepic
@math I meant with respect to particular flags
aaa
why would you do production compiles with Wall turned off? I would always add -Werror too, enforces hygiene
pm100
@pm1 because some warnings I cannot suppress
aaa
@pm100: `-Werror` is a bad idea, particularly with `-Wall`, because it can cause compilation failures on test compiles which really should pass. Better, IMHO, to compile with `-Wall -Wextra` and have some discipline.
Jack Kelly
+3  A: 

Widely used is the attributes.m4 CC_CHECK_CFLAG_APPEND macro from the xine project. Although, you often find variants (since it's quite simple) written directly in configure.ac

elmarco
+5  A: 

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")
caf
A: 

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

./configure CFLAGS='-Wall -Wextra -O2 -g'
Jack Kelly