Maybe you want to set CFLAGS when do configure, for example: ./configure CFLAGS="-g -O2 -std=gnu99".
– Jinghao ShiJul 25 '12 at 10:30

1

Yes, that's what I'm currently doing. The CFLAG I wish to add -std=gnu99 is essential, therefore I want it not be be something that the user has to configure. The code will not compile without it, so it must always be there, hence the question.
– fadedbeeJul 25 '12 at 10:32

Then how about ./configure CC="gcc -std=gnu99"?
– Jinghao ShiJul 25 '12 at 10:34

3

I want the user to not have to add -std=gnu99 anywhere. It needs to be added as a result of configuration somewhere in one of the autotools files. My objective is for the user to do ./configuremakesudo make install as normal.
– fadedbeeJul 25 '12 at 11:00

1

How about adding CFLAGS (or updating if it exists) in configure.ac or configure.in?
– another.anon.cowardJul 25 '12 at 11:17

-- Macro: AC_PROG_CC_STDC
If the C compiler cannot compile ISO Standard C (currently C99),
try to add an option to output variable `CC' to make it work. If
the compiler does not support C99, fall back to supporting ANSI
C89 (ISO C90).
After calling this macro you can check whether the C compiler has
been set to accept Standard C; if not, the shell variable
`ac_cv_prog_cc_stdc' is set to `no'.

Although using a macro like AC_PROG_CC_STDC is preferable to adding compiler options to CFLAGS, autoconf currently has no macros enabling C11 compiler support -- there is no AC_PROG_CC_C11 yet, and AC_PROG_CC_STDC only adds the -std=gnu99 option for gcc instead of -std=gnu11 or -std=c11.

You can add compiler flags simply by putting

CFLAGS+=" -std=c11"

in the configure.ac.

Alternatively, it might be better to check first if the compiler supports the added option, and print a warning otherwise, telling the user to manually add an equivalent option to CFLAGS that works for their compiler:

AX_CHECK_COMPILE_FLAG([-std=c11],
[AX_APPEND_FLAG([-std=c11])],
[AC_MSG_WARN([-std=c11 not supported, you may have to set CFLAGS to enable C11 support.])
])

This uses the AX_CHECK_COMPILE_FLAG and AX_APPEND_FLAG macros found in the GNU Autoconf Archive. To use them, put them into an m4 subdirectory and add AC_CONFIG_MACRO_DIR([m4]) to configure.ac

A slightly simpler way, although without the option to customize the warning message, is to use: AX_APPEND_COMPILE_FLAGS([-std=c11 -foo -bar])
– EricOct 16 '15 at 16:48

1

CFLAGS+=" -std=c11" - You should not be changing CFLAGS. The user owns them, not Autoconf or Automake. The answer lies in AM_CFLAGS and its per-target variants, but no one has provided an answer for it. @Arjun came close, but the answer was deleted.
– jwwNov 4 '17 at 14:39

This topic is covered in the Automake manual, 27.6 Flag Variables Ordering. There an interaction between configure.ac and Makefile.am, and its more than just setting a well known shell variable used in implicit make rules.

The short of it is, you should set it in a new variable called something like mumble_CFLAGS discussed in the Automake manual. mumble is just the name of your program, and it is often foo or bar in other examples. Later, when your makefile is created, the recipe for your program (mumble or foo or bar) will use $(mumble_CFLAGS) $(CFLAGS) to build the target.

Here is an example of how it might look. Instead of using mumble or foo or bar, it uses my_prog as a artifact name.

This section attempts to answer all the above questions. We will
mostly discuss CPPFLAGS in our examples, but actually the answer holds
for all the compile flags used in Automake: CCASFLAGS, CFLAGS,
CPPFLAGS, CXXFLAGS, FCFLAGS, FFLAGS, GCJFLAGS, LDFLAGS, LFLAGS,
LIBTOOLFLAGS, OBJCFLAGS, OBJCXXFLAGS, RFLAGS, UPCFLAGS, and YFLAGS.

CPPFLAGS, AM_CPPFLAGS, and mumble_CPPFLAGS are three variables that
can be used to pass flags to the C preprocessor (actually these
variables are also used for other languages like C++ or preprocessed
Fortran). CPPFLAGS is the user variable (see User Variables),
AM_CPPFLAGS is the Automake variable, and mumble_CPPFLAGS is the
variable specific to the mumble target (we call this a per-target
variable, see Program and Library Variables).

Automake always uses two of these variables when compiling C sources
files. When compiling an object file for the mumble target, the first
variable will be mumble_CPPFLAGS if it is defined, or AM_CPPFLAGS
otherwise. The second variable is always CPPFLAGS.

xyz.o will be compiled with ‘$(foo_CPPFLAGS) $(CPPFLAGS)’, (because
xyz.o is part of the foo target), while main.o will be compiled with
‘$(AM_CPPFLAGS) $(CPPFLAGS)’ (because there is no per-target variable
for target bar).

The difference between mumble_CPPFLAGS and AM_CPPFLAGS being clear
enough, let’s focus on CPPFLAGS. CPPFLAGS is a user variable, i.e., a
variable that users are entitled to modify in order to compile the
package. This variable, like many others, is documented at the end of
the output of ‘configure --help’.

For instance, someone who needs to add /home/my/usr/include to the C
compiler’s search path would configure a package with

./configure CPPFLAGS='-I /home/my/usr/include'

and this flag would be propagated to the compile rules of all
Makefiles.