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 t开发者_如何学编程he compiler? With such a macro I could do
TEST_AND_USE(-Wall -Wextra <other flags>)
Thanks
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")
Don't bother changing the configure.ac
at all. Just call ./configure
with the CFLAGS
you care about:
./configure CFLAGS='-Wall -Wextra -O2 -g'
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
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
精彩评论