autotools:启用编译器警告

时间:2010-08-31 18:58:19

标签: c compiler-warnings autotools

对于基于autotools的C项目,我想从编译器获得更多警告(例如至少在CFLAGS中使用-Wall)。在不破坏任何内容的情况下启用编译器标志的首选方法是什么?是否有m4宏来测试编译器是否理解给定的编译器标志? 有了这样一个宏,我可以做到

TEST_AND_USE(-Wall -Wextra <other flags>)

由于

4 个答案:

答案 0 :(得分:9)

您可以使用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年新增:AC_TRY_COMPILE现已弃用,您应该使用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")

答案 1 :(得分:7)

根本不需要更改configure.ac。只需使用您关注的./configure致电CFLAGS

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

答案 2 :(得分:4)

广泛使用的是xine项目中的attributes.m4 CC_CHECK_CFLAG_APPEND宏。虽然,您经常会在configure.ac

中找到直接编写的变体(因为它非常简单)

答案 3 :(得分:2)

我这样做:

# 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

这是一种低技术解决方案,但您无需容纳所有编译器

相关问题