I have a simple Autotools C project (not C++).
CFLAGs (by inspection) seem to be -g -O2
.
I want all of the generated make files to also have
autoconf has a macro for this:
Just put:
AC_PROG_CC_STDC
after your AC_PROG_CC
and everything will be right.
Especially when you use other compilers that do not have -std=gnu99
but operate in C99 mode by default (or have a different option hpcc's -AC99
springs to mind).
I would NOT use CFLAGS
for that kind of thing.
From the docs:
-- 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'.
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.
configure.ac:
# Perform a compile test using -std=gnu99, set has_gnu99
if test "$has_gnu99" -eq "1"; then
AC_SUBST([MY_GNU99], [-std=gnu99])
fi
Makefile.am:
bin_PROGRAMS = my_prog
my_prog_CFLAGS = $(MY_GNU99) $(MY_ANOTHER_FLAG) $(MY_YET_ANOTHER_FLAG) ...
Later, when the makefile is generated, it will have a recipe similar to the following, where $(MY_PROG_CFLAGS)
is applied to all the objects that build my_prog
:
my_prog :
$(CC) $(CPPFLAGS) $(MY_PROG_CFLAGS) $(CFLAGS) -c $< -o $@
The extra indirections of my_prog_CFLAGS
allows you to have multiple flags for different targets. For example, you could have a my_prog_CFLAGS
, a my_archive_CFLAGS
and a my_sharedobj_CFLAGS
.
And its not limited to my_prog_CFLAGS
. You could also have my_prog_CPPFLAGS
, my_prog_CXXFLAGS
and other variables used implicitly in makefiles.
This is from the Automake manual:
Compile Flag Variables
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.
In the following example,
bin_PROGRAMS = foo bar foo_SOURCES = xyz.c bar_SOURCES = main.c foo_CPPFLAGS = -DFOO AM_CPPFLAGS = -DBAZ
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.
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