TExcelTAD
Home › GNU Make & Ninja › Recipes, Tabs & the Shell

Recipes, Tabs & the Shell

Recipes look like shell scripts, and they are run by the shell (/bin/sh by default), but with rules of their own. Most first-week Makefile bugs come from three of them: the TAB, the one-shell-per-line rule, and the dollar sign.

The TAB rule and "missing separator"

Make recognises a recipe line by its first character: a TAB. Spaces are not good enough, even if they look identical in your editor. Here the recipe is indented with eight spaces:

bash · try it yourself
mkdir -p tabs && cd tabs
printf 'int main(void) { return 0; }\n' > app.c
cat > Makefile <<'MK'
app: app.c
        cc -o app app.c
MK
make -j1
echo "exit status: $?"
cat -A Makefile        # shows TABs as ^I and line ends as $
Makefile:2: *** missing separator (did you mean TAB instead of 8 spaces?).  Stop.
exit status: 2
app: app.c$
        cc -o app app.c$

GNU Make 4.3 even guesses the cause. cat -A (or cat -T) makes invisible characters visible: no ^I, so no TAB. Put a real TAB there and it works:

bash · try it yourself
mkdir -p tabs && cd tabs
printf 'int main(void) { return 0; }\n' > app.c
printf 'app: app.c\n\tcc -o app app.c\n' > Makefile
cat -A Makefile
make -j1
app: app.c$
^Icc -o app app.c$
cc -o app app.c

How to never fight this again:

  • Configure your editor: an .editorconfig section [Makefile] with indent_style = tab works in most editors (see Toolkit: Editors).
  • When generating a Makefile from a script, printf '\t…' writes a guaranteed TAB.
  • When you paste heredocs from a web page (including this one), check the result with cat -A; some copy paths turn TABs into spaces.
  • GNU Make lets you choose another prefix with .RECIPEPREFIX = >. It works, but every reader of your Makefile will be surprised, so use it only for generated or throwaway files.

Every line runs in its own shell

Each recipe line is handed to a new shell. A cd or a variable assignment on one line is gone on the next:

bash · try it yourself
mkdir -p shells/sub && cd shells
cat > Makefile <<'MK'
wrong:
	cd sub
	pwd
	X=42
	echo "X is '$X'"

right:
	cd sub && pwd
	X=42; echo "X is '$X'"
MK
make -j1 wrong
make -j1 right
cd sub
pwd
/home/learner/shells
X=42
echo "X is '$X'"
X is ''
cd sub && pwd
/home/learner/shells/sub
X=42; echo "X is '$X'"
X is '42'

Join dependent commands on one line with && (stop if the first fails) or ;, and use a backslash at the end of a line to continue a long command. The special target .ONESHELL: makes Make pass the whole recipe to one shell instead; it changes the meaning of every recipe in the file, so most projects don't use it.

Dollar signs belong to Make first

Make expands $(NAME) and $X (one-letter variable X) in recipes before the shell sees them. To pass a literal $ to the shell, write $. That's why the example above says $X. With a single $, Make would expand its own (empty) variable X and the shell would see echo "X is ''".

Echo, silence and errors

Prefix Effect on that recipe line
@ don't echo the command (the output still appears)
- ignore a failing exit status and continue
+ run even under make -n (used for $(MAKE) lines)

A recipe line that fails stops the build, unless it starts with -:

bash · try it yourself
mkdir -p errs && cd errs
cat > Makefile <<'MK'
strict:
	@echo "step 1"
	false
	@echo "never printed"

tolerant:
	@echo "step 1"
	-false
	@echo "step 3 still runs"
MK
make -j1 strict
echo "exit status: $?"
make -j1 tolerant
echo "exit status: $?"
step 1
false
make: *** [Makefile:3: strict] Error 1
exit status: 2
step 1
false
make: [Makefile:8: tolerant] Error 1 (ignored)
step 3 still runs
exit status: 0

The error line tells you the Makefile, the line number of the failing recipe line and the target being built: [Makefile:3: strict] Error 1, where 1 is the exit status of the command. Use - sparingly (-rm of files that may not exist is the classic case; rm -f is usually better).

make -k (keep going) is the build-wide version: it continues with other targets that don't depend on the failed one, which is handy for seeing every compile error in one run.

Test yourself

Make says missing separator (did you mean TAB instead of 8 spaces?). What is wrong?

  1. A colon is missing after the target
  2. A recipe line is indented with spaces instead of a TAB
  3. The file has Windows line endings only
  4. A variable is undefined

A recipe has cd build on one line and cmake .. on the next. Where does cmake run?

  1. In build
  2. In the directory Make was started in
  3. In /
  4. It depends on the shell

How do you pass the shell variable $HOME through a recipe?

  1. $HOME
  2. $HOME
  3. \$HOME
  4. ${{HOME}}