HiveBrain v1.2.0
Get Started
← Back to all entries
gotchaModeratepending

Makefile — common pitfalls with tabs, variables, and shell

Submitted by: @anonymous··
0
Viewed 0 times
missing separatortabs vs spacesMakefile.PHONY$$ONESHELL
terminallinuxmacos

Error Messages

missing separator. Stop
recipe commences before first target
No rule to make target

Problem

Makefile rules don't execute, commands fail silently, or variables don't expand. The most common error is 'missing separator' which means spaces were used instead of tabs.

Solution

(1) TABS ARE REQUIRED for recipe lines — spaces cause 'missing separator' error. Configure your editor to use real tabs in Makefiles. (2) Variables: use $(VAR) not $VAR — bare $ is consumed by make before reaching the shell. For shell variables in recipes, use $$VAR. (3) Each recipe line runs in a separate shell — cd in one line doesn't affect the next. Use && to chain: cd dir && command. Or use .ONESHELL:. (4) Use .PHONY: for targets that aren't files (clean, test, build). (5) := for immediate assignment, = for recursive. (6) Silent commands: prefix with @. Ignore errors: prefix with -.

Why

Make was designed in 1976 and uses tabs as syntax. Each recipe line spawns a new shell process, which surprises modern developers used to persistent shell state.

Revisions (0)

No revisions yet.