Skip to content

mohitmishra786/low-level-dev-skills / skills/build-systems/make

GNU Make skill for C/C++ build systems. Use when writing or debugging Makefiles, understanding pattern rules and automatic dependency generation, managing CFLAGS/LDFLAGS, converting ad-hoc compile commands into maintainable Makefiles, or diagnosing incremental build issues. Activates on queries about Makefiles, make targets, pattern rules, phony targets, dependency tracking, recursive make, or make errors.

FileKindSize
SKILL.mdskill md4.7 KB
references/cheatsheet.mdmarkdown3.5 KB

references/cheatsheet.md

View on GitHub
# GNU Make Cheatsheet

Source: <https://www.gnu.org/software/make/manual/make.html>

## Automatic variables

| Variable | Meaning |
|----------|---------|
| `$@` | Target filename |
| `$<` | First prerequisite |
| `$^` | All prerequisites (no duplicates) |
| `$+` | All prerequisites (with duplicates) |
| `$?` | Prerequisites newer than target |
| `$*` | Stem (matched `%` in pattern rule) |
| `$(@D)` | Directory part of `$@` |
| `$(@F)` | File part of `$@` |
| `$(<D)` | Directory part of `$<` |
| `$(<F)` | File part of `$<` |

---

## Special targets

| Target | Effect |
|--------|--------|
| `.PHONY: all clean` | Declare targets that are not files |
| `.DEFAULT_GOAL := all` | Set default target |
| `.SUFFIXES:` | Clear default suffix rules |
| `.SILENT:` | Suppress command echoing globally |
| `.ONESHELL:` | Run recipe lines in one shell |
| `.DELETE_ON_ERROR:` | Remove target on recipe failure |

---

## Functions

| Function | Effect |
|----------|--------|
| `$(wildcard *.c)` | Expand glob |
| `$(patsubst %.c,%.o,files)` | Replace pattern |
| `$(subst from,to,text)` | Replace literal string |
| `$(strip text)` | Remove whitespace |
| `$(notdir path)` | Filename without directory |
| `$(dir path)` | Directory component |
| `$(basename file)` | Filename without extension |
| `$(suffix file)` | Extension only |
| `$(addprefix pre,list)` | Add prefix to each word |
| `$(addsuffix suf,list)` | Add suffix to each word |
| `$(filter %.c,list)` | Keep matching words |
| `$(filter-out %.c,list)` | Remove matching words |
| `$(sort list)` | Sort and deduplicate |
| `$(foreach var,list,expr)` | Loop |
| `$(if cond,then,else)` | Conditional |
| `$(shell cmd)` | Run shell command |
| `$(call var,arg1,arg2)` | Call a function-like variable |
| `$(origin var)` | Where a variable came from |
| `$(value var)` | Value without expansion |
| `$(info msg)` | Print message during parse |
| `$(error msg)` | Fatal error during parse |
| `$(warning msg)` | Warning during parse |

---

## Variable assignment

| Syntax | Type | When expanded |
|--------|------|---------------|
| `VAR = value` | Recursive | At use |
| `VAR := value` | Simple | At definition |
| `VAR ::= value` | POSIX simple | At definition |
| `VAR ?= value` | Conditional | Only if unset |
| `VAR += value` | Append | Depends on type |

---

## Conditionals

```makefile
ifeq ($(CC),gcc)
  CFLAGS += -fanalyzer
endif

ifneq ($(BUILD),release)
  CFLAGS += -g
endif

ifdef DEBUG
  CFLAGS += -DDEBUG
endif
```

---

## Multi-line variables

```makefile
define HELP_TEXT
Usage: make [target]
  all    - build everything
  clean  - remove build artifacts
endef

help:
	@echo '$(HELP_TEXT)'
```

---

## Order-only prerequisites

```makefile
# build/ must exist, but its timestamp doesn't trigger rebuild
build/%.o: src/%.c | build
	$(CC) -c -o $@ $<
```

---

## Recursive make (use sparingly)

```makefile
SUBDIRS := lib src

.PHONY: all $(SUBDIRS)
all: $(SUBDIRS)

$(SUBDIRS):
	$(MAKE) -C $@
```

Prefer a flat include-based approach over recursive make for correctness.

---

## Command line overrides

```bash
make CFLAGS="-O3 -march=native"    # override variable
make CC=clang                       # change compiler
make -n                             # dry run (print commands)
make -B                             # force rebuild all
make -k                             # keep going on error
make -j$(nproc)                     # parallel
make -p                             # print database (all rules)
make --warn-undefined-variables     # catch typos
```
reverse-engineer-anythingmorluto/reaReverse engineer native, managed, Electron/JavaScript, packaged, firmware, and browser targets with REA, and build agent environments (AgentEnv bundles) of web applications from a live instance or a capture, and of Linux desktop apps with a task from a request. Use shipped-artifact or requested runtime evidence to explain features, compare versions, decompile code, or guide a reconstruction. Skip REA for ordinary source-repository architecture analysis.Stars: 471kUpdated Claude CodeCodexVerifiedInstall
diagnosing-superpowersobra/superpowersUse when a superpowers session went wrong and your human partner wants to know why — repeated work, ignored plans, stumbles, poor results, a skill that didn't fire, "it took too long", "why is it so expensive", "what is it doing" — or wants to build a bug report for the superpowers maintainers, for the current session or a past one identified by id or path, on any harness.Stars: 322kUpdated Claude CodeCodexVerifiedInstall
dispatching-parallel-agentsobra/superpowersUse when facing 2+ independent tasks that can be worked on without shared state or sequential dependenciesStars: 322kUpdated Claude CodeCodexVerifiedInstall
executing-plansobra/superpowersUse when executing an implementation plan in the current session as the implementer yourself — your human partner chose inline execution, or no subagent tool is availableStars: 322kUpdated Claude CodeCodexVerifiedInstall