make
can take variable assignments as part of his command line. Is DEBUG=1 make
the same as make DEBUG=1
? With this simple Makefile, both print echo 1
.
$ cat Makefile
all:
echo ${DEBUG}
It's clear in the latter case, DEBUG=1
is part of the argument to make
, but the first one only seems like a variable assignment in shell. So I'd assume make
gets the value differently. Some clarification about the difference will be helpful.
Variables defined with ' := ' or ' ::= ' are simply expanded variables; these definitions can contain variable references which will be expanded before the definition is made.
Double dollar sign If you want a string to have a dollar sign, you can use $$ . This is how to use a shell variable in bash or sh . Note the differences between Makefile variables and Shell variables in this next example.
To set system-wide Linux environment variables, you can edit the /etc/environment file. Instead of appending export commands to the file, append the <NAME>='<VALUE>' pair to the end of the file.
Is
DEBUG=1 make
the same asmake DEBUG=1
?
Not really.
DEBUG=1 make
: make
obtains the variable from the environment. That is, the shell sets the variable DEBUG
, then make
is executed and it (the process running make
) inherits that environment variable.make DEBUG=1
: make
obtains the variable from the command line (i.e.: make
sets the variable itself).Variables obtained from the command line override the variables obtained from the environment. That is, in:
DEBUG=0 make DEBUG=1
DEBUG
is 1
, not 0
.
Note as well that variables obtained from the command line override the variables set inside the makefile (unless the override
directive is used), whereas environment variables don't (unless the option -e
or --environment-override
is passed to make
).
Therefore, if the makefile sets a variable named DEBUG
as in the following:
DEBUG = 0
all:
@echo ${DEBUG}
Then:
make DEBUG=1
echoes 1
, whereas:
DEBUG=1 make
echoes 0
.
origin
built-in functionYou can use the origin
built-in function to find out where a variable comes from. Consider this modification of your makefile:
$ cat Makefile
all:
@echo DEBUG is from $(origin DEBUG)
Then:
$ make DEBUG=1
DEBUG is from command line
but:
$ DEBUG=1 make
DEBUG is from environment
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With