The points regarding not surprising the user are valid, but some flags are reasonable to turn on by default (e.g. -Wall -Wextra
), and other flags are code base specific and are sometimes needed (e.g. -std=gnu99
).
The question then becomes how to do this portably. I personally steal the flag check macros from the libuv project. To do this I add libuv-check-flags.m4
to the m4
directory of my project. I can then do the following in my configure.ac
:
m4_include([m4/libuv-check-flags.m4])
AM_INIT_AUTOMAKE([-Wall -Werror foreign 1.11.2])
# Checks for programs.
AC_PROG_CC
CC_CHECK_CFLAGS_APPEND([-std=gnu99])
CC_CHECK_CFLAGS_APPEND([-Wall])
CC_CHECK_CFLAGS_APPEND([-Wextra])
AC_PROG_LIBTOOL
My generated configure file then produces a compiler command line of:
gcc -g -O2 -std=gnu99 -Wall -Wextra
I can still override the -g -O2
default using the solutions above, e.g.:
./configure CFLAGS='-O0 -g'
Produces a command line of:
gcc -O0 -g -std=gnu99 -Wall -Wextra
And leveraging gcc semantics I can still disable base flags if necessary. For example I can disable warnings if I really really want to with:
./configure CFLAGS='-Wno-all -Wno-extra'
You may say, "well what if the compiler doesn't support those flags?" That's why these macros are so useful and great, since they ensure compiler features are checked first, so -Wall
and -Wextra
wouldn't be added in the first place if they weren't supported.
libuv is one of the most portable and widely used C libraries on the planet, so I think following their lead is reasonable. And while these macros are pure C specific, adapting them for use with CXXFLAGS would be trivial.
References: