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=whateverto Makefile.am. It doesn’t work, because it adds the flags to the default flags instead of replacing them. - Put
CXXFLAGS=whateverinto configure.ac. This works, but then I can’t override it later.
Meanwhile I figured out how to do it. I will give an explanation to this.
The basic thing is that Autoconf substitutes shell variables in Makefile.in. The question is how do I get the value of these variables? The answer is that the initialization command substitutes variables that are told them at command line (like
./configure 'CXXFLAGS=-O0 -g'), and otherwise they are substituted by whatever command defines the default (for example, CXXFLAGS is set byAC_PROG_CXX) if they are not empty. So the solution is to set our new default beforeAC_PROG_CXXbut after substitutions from the command lines are made. For example: