Uli Schlachter
Uli Schlachter

Reputation: 9867

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

Upvotes: 5

Views: 1980

Answers (4)

elmarco
elmarco

Reputation: 32973

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

Upvotes: 4

caf
caf

Reputation: 239041

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")

Upvotes: 10

Jack Kelly
Jack Kelly

Reputation: 18667

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

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

Upvotes: 5

Anycorn
Anycorn

Reputation: 51465

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

Upvotes: 2

Related Questions