I am using a GNU-make Makefile to build a C project with several targets (all
, clean
, and a few project specific targets). In the process of debugging, I would like to append some flags to a single build without permanently editing the Makefile (e.g. add debugging symbols or set a preprocessor flag).
In the past, I have done that as follows (using the debugging symbols example):
make target CFLAGS+=-g
Unfortunately, this is not appending to the CFLAGS
variable, but instead, clearing it and stopping it from compiling. Is there a clean way of doing this without defining some sort of dummy variable appended to the end of CFLAGS
and LDFLAGS
?
There are two ways to pass variables to make:
Using command line arguments:
Using environment:
or (better because it change environment only for current command)
They are slightly different. The first one is stronger. It mean you know what you want. The second may be considered like a hint. Difference between them is about operators
=
and+=
(withoutoverride
). These operators are ignored when a variable is defined on command line, but are not ignored when variable is defined in environment. Thus, I suggest you to have a Makefile with:and call it with:
Notice, if you want to withdraw
-Wall
, you can use:Please don't use
override
keyword, else you won't have any way to change a variable affected withoverride
.For the record, @Carl Norum's answer prepends the variable, from the command line perspective.
I needed a way to actually append and came up with:
Just a note, as I got confused - let this be file
testmake
:Then:
With the
override
directives deleted from thetestmake
file:So,
override
once, it can only be appended with another statement withoverride
(the normal assignments will be ignored);override
at all; trying to append (as in+=
) from the command line overwrites every instance of that variable.Check out the override directive. You will probably need to modify the makefile once, but it should do what you want.
Example makefile:
Example command lines: