How to use it
- Choose the project type and enter the program name.
- Tick the targets you need. All except
lintare on at first. - Copy the result into a file named
Makefilein the project root. - Run
make,make test,make cleanormake help.
How it works
The file has three parts:
- Variables: tools with
?=so they can be overridden (CC ?= cc,GO ?= go,NPM ?= npm,PYTHON ?= python3) and the program name with:=. - .PHONY: every chosen target, so files with the same names cannot block them.
- Targets: each a rule line and one or more recipe lines indented with a tab. For C,
builddepends on$(TARGET), which is linked from the object files; a pattern rule%.o: %.ccompiles each source insrc/.
The help target prints the target names found in the Makefile itself with grep, so it stays correct when you add targets.
Example
The defaults (Go, name app) give:
GO ?= go
BINARY := app
.PHONY: build test run install clean help
build:
$(GO) build -o bin/$(BINARY) .
test:
$(GO) test ./...
run:
$(GO) run .
install:
$(GO) mod download
clean:
rm -rf bin
help:
@grep -E '^[a-zA-Z_-]+:' $(MAKEFILE_LIST) | cut -d: -f1
Tips
- Prefix a recipe line with
@to hide the command and show only its output. - Use
$$for a dollar sign the shell should see: a single$belongs to make. - Each recipe line runs in its own shell, so
cd diron one line does not affect the next; writecd dir && command.