Razer
Razer

Reputation: 8211

Overwrite variable from makefile

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

Answers (3)

Colonel Thirty Two
Colonel Thirty Two

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

Matteo
Matteo

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

Etan Reisner
Etan Reisner

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:

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

Related Questions