Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Autotools : how to set global compilation flag

I have a project with several sources directories :

src/A
   /B
   /C

In each, the Makefile.am contains

AM_CXXFLAGS = -fPIC -Wall -Wextra

How can avoid repeating this in each source folder ?

I tried to modifiy src/Makefile.am and the configure.in, but without success. I thought I could use AC_PROG_CXX to set the compilation flags globally but can't find much documentation on how to use those macro (do you have any pointer to such a documentation ?).

Thanks in advance

like image 778
Barth Avatar asked Nov 27 '08 13:11

Barth


People also ask

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.

How does Autoconf work?

Autoconf essentially runs the preprocessor on your script to produce a portable shell script which will perform all the requisite tests, produce handy log files, preprocess template files, for example to generate Makefile from Makefile.in and and take a standard set of command line arguments.

How does Automake work?

Automake is the component you'll use to create the Makefile, a template that can then be populated with autoconf . Automake does so using variables and primaries. An example of such a primary is bin_PROGRAMS = helloworld , where the primary is the _PROGRAMS suffix.


1 Answers

You can do several things:

(1) One solution is to include a common makefile fragment on all your Makefile.ams:

include $(top_srcdir)/common.mk
...
bin_PROGRAMS = foo
foo_SOURCES = ...

in that case you would write

AM_CXXFLAGS = -fpic -Wall -Wextra

to common.mk and in the future it will be easier to add more macros or rules to all Makefile.ams by just editing this file.

(2) Another solution would be to set these variables globally in your configure.ac (the name configure.in has been deprecated long ago), as in :

...
AC_SUBST([AM_CXXFLAGS], [-fpic -Wall -Wextra])
...

Then you don't even have to say anything in your Makefile.ams, they automatically inherit this global definition. The drawback is that you can't opt-out easily (with the first solution it's easy to decide not to include common.mk) and the dependency is not really explicit to third-party people (when they read the Makefile.am they have no hint about where the flags may come from).

(3) A third solution would be to do as orsogufo suggested: overwriting the user variable CXXFLAGS in configure.ac. I would advise against it, because it defeats one of the features of the GNU Build System: users are allowed to override this variable at make-time. For instance you may want to type

make CXXFLAGS='-O0 -ggdb' 

when debugging a piece of code, and this will overwrite any definition of CXXFLAGS (but not those in AM_CXXFLAGS). To be honest, most projects fails to support this correctly because they play tricks with CXXFLAGS.

Finally, I should mention that -fpic, -Wall, and -Werror are not portable options. Depending on the scope of your project you may want to add configure check for these (gnulib recently acquired new macros to tests for warnings flags, and libtool can be used to build shared libraries).

like image 116
adl Avatar answered Oct 14 '22 17:10

adl