February 3, 2016 · Makefile unix-as-ide linux

Makefile - Top-level Environment Variables

Did you know it was possible to export environment variables at the top-level of a Makefile?

The following Makefile illustrates just that:

export MSG="Hello World!"

.PHONY: hello
hello:  
    @echo $(MSG)

And the output:

$ make hello
Hello World!  

It's also possible to override the environment variable at runtime:

make -e MSG="Hello Venus!" hello  
Hello Venus!  

Read more about GNU Make Environment Variables in 6.10 Variables from the Environment.