Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Default compiler flags with Autotools

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.
like image 425
petersohn Avatar asked Jun 25 '10 08:06

petersohn


People also ask

What is CPP flags?

CPPFLAGS - is the variable name for flags to the C preprocessor. CXXFLAGS - is the standard variable name for flags to the C++ compiler. CFLAGS is - the standard name for a variable with compilation flags. LDFLAGS - should be used for search flags/paths (-L) - i.e. -L/usr/lib (/usr/lib are library binaries).

How do I enable flags in Makefile?

The only way of doing that is to edit the makefile to change the options. There is no convenient way to override the options without modifying the makefile . This is where make flags come into play. Flags in make are just variables containing options that should be passed to the tools used in the compilation process.

How do I add Cflags in Makefile?

It should be CFLAGS := -Wall -Wextra $(CFLAGS) , the difference is that CFLAGS is explicitly appended. So for example, you may set -Og , but user don't want optimization and passes CFLAGS=-O0 on command line. By using CFLAGS += -Og your -Og will take over the user provided value.


1 Answers

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.

like image 91
malex984 Avatar answered Sep 23 '22 01:09

malex984