Skip to content

Instantly share code, notes, and snippets.

@aprell
Last active November 4, 2021 15:51
Show Gist options
  • Save aprell/5dae170cecb68853968c0395ee2e21fa to your computer and use it in GitHub Desktop.
Save aprell/5dae170cecb68853968c0395ee2e21fa to your computer and use it in GitHub Desktop.
Overriding Makefile variables
test1: CFLAGS ?= -Wall -Wextra
test2: CFLAGS += -Wall -Wextra
test3: override CFLAGS += -Wall -Wextra
test4: override CFLAGS := -Wall -Wextra $(CFLAGS)
test1 test2 test3 test4:
@echo "$@: $(CFLAGS)"
.PHONY: test1 test2 test3 test4
@aprell
Copy link
Author

aprell commented Nov 4, 2021

$ for test in test{1,2,3,4}; do make -f override.mk $test; done
test1: -Wall -Wextra
test2: -Wall -Wextra
test3: -Wall -Wextra
test4: -Wall -Wextra

$ for test in test{1,2,3,4}; do make CFLAGS="-g" -f override.mk $test; done
test1: -g
test2: -g
test3: -g -Wall -Wextra
test4: -Wall -Wextra -g

$ for test in test{1,2,3,4}; do CFLAGS="-g" make -f override.mk $test; done
test1: -g
test2: -g -Wall -Wextra
test3: -g -Wall -Wextra
test4: -Wall -Wextra -g

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment