I'm working on a C-project that uses automake to generate Makefiles for its subprojects. In the Makefiles there is a variable CFLAGS
which contains the default compiler arguments, currently -O2 -fcommon
. From time to time I want to additionally pass -D DEBUG
to the compiler. In this question I learned that I can pass additional arguments to make via make foo=bar target
so I was able to call make CFLAGS="-O2 -fcommon -D DEBUG" all
which works fine. However doing it like this I obviously always need to know the default arguments which is a bit tedious if they are ever going to change or if I'm not working on the project for a while and just forget about the default flags.
Now there are two solutions I'm currently thinking of:
Adding an additional variable
DBG
to the Makefile which is empty per default and gets added to the compile command so I can usemake DBG="-D DEBUG" all
and avoid touchingCFLAGS
at all. However because the Makefile is autogenerated by automake I can't edit it directly and I don't know how to do it in an autotools fashion, if it's possible at all, since I don't know much about autotools. Currently the compile command looks like this:COMPILE = $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) \ $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS)
and I would need it to look like this with the approach from above:
COMPILE = $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) \ $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) $(DBG)
I guess a quiet hack-ish way would be to abuse
CPPFLAGS
for my purpose, e.g.make CPPFLAGS="-D DEBUG" all
which works just fine but doesn't feel right (and might have some unwanted side-effects I'm not aware of).Submitting
CFLAGS
as part of itself so it gets expanded inside the makefile, e.g.make CFLAGS="\$CFLAGS -D DEBUG" all
. However that does not work albeit escaping the dollar sign. The command then results in:mpicc <some_other_flags> FLAGS -D DEBUG <sources>
instead of what I expected:
mpicc <some_other_flags> $CFLAGS -D DEBUG <sources>
Note that
CFLAGS
is set only inside of the makefile. I don't have it set as an environment variable in my default shell session so I can't expand it before passing it to the makefile and even if I did the content probably would not be the same as the default arguments of the makefile.
I'm not that comfortable working with makefiles and autotools since I only have very basic experience in this whole topic. So any help or clarification is much appreciated!