Infinite loop in a Makefile

I had to kick myself for writing this bug into a Makefile today. I am writing it down so that I remember to use &&, and not ; next time.

The Makefile had this target in it:

1
2
3
4
clean:
	cd folder; \
		make clean
	rm -f file

The problem here is that the statement cd folder; make clean counts as one “line”, and make won’t notice a problem (and stop) until that line has executed and returns non-zero.

Unfortunately, in the case that the directory does not exist, this script had a chance to call make clean without changing directory. It then calls itself and becoming stuck in an infinite loop.

So lesson learned, this is why Makefiles should be littered with the “and” operator.

1
2
3
4
clean:
	cd folder && \
		make clean
	rm -f file
Last updated on Aug 06, 2025 23:13 -0400