Exit Makefile on error

Published: Tuesday, Jul 6, 2010 Last modified: Thursday, Nov 14, 2024

What’s the problem with this?

all: foo bar
	@for i in $^ ; \
	do \
		false ; \
		echo $$i ; \
	done
.PHONY: all foo bar

Well even though false trips up, the make will just continue. Usually Make will just croak if something goes wrong, but since everything is neatly in a for stanza, this is easy to miss. I wonder if there is a way to set -e on Make invoked shells?

Otherwise to fix the problem, you need to add set -e, like so:

all: foo bar
	@set -e; for i in $^ ; \
	do \
		false ; \
		echo $$i ; \
	done

.PHONY: all foo bar