POSIX-style portable rule
Write simple, explicit file rules compatible with standard make.
all: app
app: main.o util.o
cc -o app main.o util.oExplicit rules are the most portable foundation.
Portable makefile patterns that work across POSIX make environments, avoiding GNU-only features when needed.
Patterns that avoid GNU-only behavior.
Write simple, explicit file rules compatible with standard make.
all: app
app: main.o util.o
cc -o app main.o util.oExplicit rules are the most portable foundation.
Honor user-overridable standard compiler variables.
CC = c99
CFLAGS = -O2
LDFLAGS =
LDLIBS =
app: main.o util.o
$(CC) $(LDFLAGS) -o $@ main.o util.o $(LDLIBS)Keeping `CC`, `CFLAGS`, `LDFLAGS`, and `LDLIBS` overridable is a long-standing portable convention.
Use classic suffix rules when targeting very portable make implementations.
.SUFFIXES: .c .o
.c.o:
$(CC) $(CFLAGS) -c $<Suffix rules are older but widely portable.
clean:
rm -f app *.o`.PHONY` is common in modern makes, but some historical portability guidance prefers not to depend on it unless you know the implementation supports it.
make CC=clang CFLAGS='-O0 -g'Command-line macro assignments are part of standard make usage.
Shell-safe and cross-environment recipe habits.
Stop on failure explicitly within one recipe line.
deploy:
cd infra && terraform apply -auto-approveSince each recipe line runs in its own shell by default, keep dependent shell commands on the same line in portable makefiles.
print-pid:
@echo $$PPIDMake uses `$` for variable expansion, so shell variables need `$$`.
Indent recipe commands with a tab character.
target:
printf 'hello
'Traditional make syntax requires tabs unless you explicitly use GNU `.RECIPEPREFIX`.
build:
mkdir -p build`mkdir -p` is broadly available on modern POSIX systems.
Common GNU-only constructs that reduce portability.
Prefer explicit rules over GNU metaprogramming.
# Prefer explicit rules when portability matters
app: main.o util.o
$(CC) -o $@ $^`eval`, `call`, `foreach`, and many text functions are GNU extensions.
List sources explicitly or generate files in configure scripts.
SRC = main.c util.c api.cSome highly portable makefiles avoid GNU-only discovery functions.
Keep recipe-line semantics standard.
release:
cd dist && tar -czf app.tar.gz app`.ONESHELL` is a GNU extension.