| name | makefile |
| description | Help with Makefiles - targets, variables, patterns, and best practices |
| argument-hint | [target or question] |
| allowed-tools | Read, Glob, Grep, Bash |
Help with Makefiles based on $ARGUMENTS.
Makefile Basics
Variables
CC = gcc
CC := gcc
CC ?= gcc
CFLAGS += -Wall
Automatic Variables
| Variable | Meaning |
|---|
$@ | Target name |
$< | First prerequisite |
$^ | All prerequisites |
$* | Stem (matched by %) |
$(@D) | Directory of target |
$(@F) | File part of target |
Pattern Rules
%.o: %.c
$(CC) $(CFLAGS) -c $< -o $@
%: %.tmpl
envsubst < $< > $@
Common Patterns
.PHONY: all clean test build
all: build
build: $(OBJECTS)
$(CC) $(LDFLAGS) -o $@ $^
clean:
rm -rf build/ *.o
test:
go test ./...
help:
@grep -E '^[a-zA-Z_-]+:.*?
awk 'BEGIN {FS = ":.*?## "}; {printf "%-15s %s\n", $$1, $$2}'
Best Practices
- Use
.PHONY for non-file targets
- Use
@ prefix to suppress command echo
- Use
- prefix to ignore errors
- Define
clean target
- Use variables for repeated values