Reputation: 8211
In my makefile I have a variable FOO
:
FOO = /path/to/bar
Is it possible to overwrite this variable during the makefile call? Somthing like the following:
FOO=/path/to/foo make all
Upvotes: 30
Views: 35354
Reputation: 26609
Specify them as arguments to make
as Var=Value
, like make FOO=/path/to/foo all
.
$ cat Makefile
Foo = asdf
all:
echo $(Foo)
$ make all
echo asdf
asdf
$ make Foo=bar all
echo bar
bar
Upvotes: 47
Reputation: 2634
You can also simply use the ?=
operator:
$ cat Makefile
Foo ?= asdf
all:
echo $(Foo)
$ make all
echo asdf
asdf
$ Foo=bar make all
echo bar
bar
Upvotes: 5
Reputation: 81032
The ways that variables get assigned values is specified in the How Variables Get Their Values section of the GNU make Manual.
Variables can get values in several different ways:
- You can specify an overriding value when you run make. See Overriding Variables.
- You can specify a value in the makefile, either with an assignment (see Setting Variables) or with a verbatim definition (see Defining Multi-Line Variables).
- Variables in the environment become make variables. See Variables from the Environment.
- Several automatic variables are given new values for each rule. Each of these has a single conventional use. See Automatic Variables.
- Several variables have constant initial values. See Variables Used by Implicit Rules.
So, as Colonel Thirty Two indicates, you can override variables set in the makefile on the command line.
You can also, if you expect this to be a variable that you want to set persistently, use the ?=
assignment and then environment values for that variable will be used.
Upvotes: 20