Default compiler flags with Autotools

autoconfautomakemakefile

I want to know how to set default compiler/linker/etc. flags if I use Autoconf/Automake combo.

For example, the default compiler flag is "-O2 -g" if I don't set anything. I can just override it with something else, for example if I want to debug:

./configure 'CXXFLAGS=-O0 -g'

But I find the default configuration stupid because if I enable optimization, debugging will become impossible. So the default flags should either be "-O2" or "-O0 -g", if I run configure without arguments. How do I do it?

Edit: I tried the following solutions:

  • Put progname_CXXFLAGS=whatever to Makefile.am. It doesn't work, because it adds the flags to the default flags instead of replacing them.
  • Put CXXFLAGS=whatever into configure.ac. This works, but then I can't override it later.

Best Answer

According to autoconf manual (about AC_PROG_CC):

If using the GNU C compiler, set shell variable GCC to ‘yes’. If output variable CFLAGS was not already set, set it to -g -O2 for the GNU C compiler (-O2 on systems where GCC does not accept -g), or -g for other compilers. If your package does not like this default, then it is acceptable to insert the line

: ${CFLAGS=""}

after AC_INIT and before AC_PROG_CC to select an empty default instead.

Similarly, according to autoconf manual (about AC_PROG_CXX):

If using the GNU C++ compiler, set shell variable GXX to ‘yes’. If output variable CXXFLAGS was not already set, set it to -g -O2 for the GNU C++ compiler (-O2 on systems where G++ does not accept -g), or -g for other compilers. If your package does not like this default, then it is acceptable to insert the line

: ${CXXFLAGS=""}

after AC_INIT and before AC_PROG_CXX to select an empty default instead.

Related Topic