1
votes

I'd like to use -Wall and -Werror as flags for gcc in an autotools project, but I don't want to put them in my configure.ac.

As such, I tried using ./configure CFLAGS='-Wall -Werror', only to get an error from one of my AC_SEARCH_LIBS macro calls:

AC_SEARCH_LIBS([pow], [m], , AC_MSG_ERROR([Could not find standard math library.]))

Resulting error when running configure with the CFLAGS added:

configure: error: Could not find standard math library.

What am I doing wrong here? Configuration works fine without the CFLAGS variable set.

1
Check your config log file for the output that came from it trying to run that step. You'll see what the error was and can probably fix it. - Carl Norum
The log file tells me: error: conflicting types for built-in function 'pow' [-Werror], after trying to compile conftest.c. Removing -Werror from the CFLAGS gets rid of the issue, but that's not really any use to me. - heuristicus
So fix the prototype to match in whatever hike generates it, and then try again. - Carl Norum
@Carl You cannot do that. This is a known issue; the tests that autoconf uses typically compile with warnings, and elevating warnings to errors via -Werror will cause the tests to fail. A reasonable way to deal with this (if using automake) is to conditionally add the flags to AM_CFLAGS (the condition being a configure time check that CC accepts the flags). - William Pursell

1 Answers

2
votes

As you now know, elevating the compile warnings to errors confuses ./configure.

What you can do is pass custom CFLAGS at make time:

$ ./configure
$ make CFLAGS='-O2 -g -Wall -Wextra -Werror'

The other option is William Pursell's approach: add an option to ./configure to turn on -Werror if supported:

(configure.ac)

AC_ARG_ENABLE([werror],
              [AS_HELP_STRING([--enable-werror], [Use -Werror @<:@no@:>@])],
              [:],
              [enable_werror=no])
AM_CONDITIONAL([ENABLE_WERROR], [test "$enable_werror" = yes])

(Makefile.am)

if ENABLE_WERROR
AM_CFLAGS += -Werror
endif