I will only answer for Windows, since there is no Unix environment here. It should give a good idea of ββhow it works in GNU make .
First, I assume that the environment variables you are talking about have a life cycle associated with the life cycle of the shell, so this is not a system environment variable.
Windows has two programs for setting a variable: SET and SETX . There are probably more subtleties, but to make it simple, SET will set the variable only for the current shell and its subprocesses, and SETX set the system environment variable. I use only SET , since I do not want to deal with system environment variables.
I will give an empirical answer. I checked this setting:
\---level1 | Makefile | \---level2 | Makefile | \---level3 Makefile
level1 - Makefile
LEVEL = LEVEL1 LEVEL1VAR = VAR1 varB = 12 export varB .PHONY: foo foo: @echo $(LEVEL) var level 1 : $(LEVEL1VAR) @echo $(LEVEL) varA is $(varA) @echo $(LEVEL) varB is $(varB) cd level2 & $(MAKE) foo
level2 - Makefile
LEVEL = LEVEL2 LEVEL2VAR = VAR2 MKID = MKID2 varC = 13 export varC .PHONY: foo foo: @echo $(LEVEL) var level 1 : $(LEVEL1VAR) @echo $(LEVEL) var level 2 : $(LEVEL2VAR) @echo $(LEVEL) varA is $(varA) @echo $(LEVEL) varB is $(varB) cd level3 & $(MAKE) foo
level3 - Makefile
LEVEL = LEVEL3 LEVEL3VAR = VAR3 .PHONY: foo foo: @echo $(LEVEL) var level 1 : $(LEVEL1VAR) @echo $(LEVEL) var level 2 : $(LEVEL2VAR) @echo $(LEVEL) var level 3 : $(LEVEL3VAR) @echo $(LEVEL) varA is $(varA) @echo $(LEVEL) varB is $(varB) @echo $(LEVEL) varC is $(varC)
At the beginning of the test, I open the shell (Windows command line) in the folder level1. I create a varA variable with a value of 11:
SET varA=11
Then I call the first Makefile, which will call the second, which will call the third.
make foo
Here is the result:
LEVEL1 var level 1 : VAR1 LEVEL1 varA is 11 LEVEL1 varB is 12 cd level2 & make foo LEVEL2 var level 1 : LEVEL2 var level 2 : VAR2 LEVEL2 varA is 11 LEVEL2 varB is 12 cd level3 & make foo LEVEL3 var level 1 : LEVEL3 var level 2 : LEVEL3 var level 3 : VAR3 LEVEL3 varA is 11 LEVEL3 varB is 12 LEVEL3 varC is 13
So we can see that:
- Access to the shell variable can be obtained from all the sub-headings of make
- The exported Makefile variable is available from all make subframes of this Makefile
- A non-exported Makefile variable cannot be accessed from the make subframes of this Makefile
You can easily reproduce this example to run more tests.
Note that you can actually include another Makefile and therefore get its variables and rules, see GNU make: Enable . I do not recommend using this if you do not pay close attention to what is happening, because you can redefine the rules if they have the same name in the included Makefile, as in what it includes.