Skip to content

Instantly share code, notes, and snippets.

@mstrehler
Last active March 5, 2016 08:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mstrehler/056d025b49eed7b1eb04 to your computer and use it in GitHub Desktop.
Save mstrehler/056d025b49eed7b1eb04 to your computer and use it in GitHub Desktop.
Flexible handling of variables in a makefile
# Example for the ultimate flexible variable handling in a makefile.
# Code adapted from "The GNU Make Book", pos. 300 (e-book).
#
# Case 1: Default in makefile
# With no environment variable and no option set in command line,
# TEST contains foo:
# $ make
# foo file
#
# Case 2: Environment variable
# With environment variable set, TEST contains bar
# $ export TEST=bar
# $ make
# bar environment
#
# Case 3: Command line variable
# Setting the variable via command line beats all.
# $ make TEST=foobar
# foobar command line
# if TEST is not set, it will set to foo;
# otherwise, it is left unchanged.
TEST ?= foo
PHONY: all
all:
$(info $(TEST) $(origin TEST))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment