Category / Make

GNU Make: Exporting Environment Variables October 23, 2011 at 14:38

I’m still learning the finer arts of writing Makefiles. Just this weekend I learned a subtlety when it comes to the export keyword – it doesn’t work within recipes. As it turns out, the export var=value cannot be part of a recipe but must be written in the general area. It took me awhile to figure this out. I can’t remember if I found it on a side note in the GNU Make handbook or on a forum, but it wasn’t well advertised. I visited close to a dozen forums, non mentioning it. So there you go.

A second thing that can throw of the export command is the shell that Make uses to execute the shell-commands. I was advisedĀ  to set the SHELL variable at the top of the Makefile to what ever shell I was using, to get the syntax correct. I’m not sure if this had an affect in my case.

For those not familiar to the export keyword, it is used to export a variable to the environment and hence make it visible to any program executed,e.g. in a recipe.


There is a way to export a variable just for a specific recipe. It looks something like this:
target: export VAR += value

It can also be used to just update process local variables:
target: VAR += value

I found it a while back, in the GNU Make handbook, even though I can’t remember the exact place.