Autotools : how to set global compilation flag
Asked Answered
J

2

19

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

Joby answered 27/11, 2008 at 13:36 Comment(0)
N
26

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).

Narvik answered 28/11, 2008 at 10:17 Comment(4)
Using the include idiom in Makefile.am will make cause automake to output dependencies for Makefile and Makefile.in that eventually get back to the included file and the automake file. So changing this will lead to updates later too.Penalty
It's actually very easy to opt out if you AC_SUBST the flags. In Makefile.am, if you assign AM_CPPFLAGS, they override. You can opt in by doing nothing, or by doing AM_CPPFLAGS = @AM_CPPFLAGS@ ..., or opt out by not putting @AM_CPPFLAGS@ in the assignment.Beefsteak
Warning flags does not belong in CPPFLAGS like you claim, since they are use by the compiler as opposed to the pre-processor.Unconquerable
Am I missing something, or doesn't the AC_SUBST need quotes '' around multiple flags, to avoid getting a shell error while running configure?Kittiekittiwake
P
0

Use EXTRA_CFLAGS in configure.ac

For example for your case it would be this:

EXTRA_CFLAGS=-fPIC -Wall -Wextra

Pondweed answered 17/3, 2016 at 12:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.