Makefiles for Dummies

Don't tie yourself in knots

Introduction

Work In Progress :- to be continued! (started 2023-08-29)

Here is a valid GNU Make Makefile that introduces the mysteries and brain-numbing syntax. It begins with the basics and takes us step-by-step towards becoming confident in our understanding and use. It includes links to relevent manual pages and has an index of terms at the end that will take one directly to the example usage.

The HTML page you are reading now is generated by a Bash shell script and can be found alongside. Makefile.example is the valid source. If you would like to contribute visit the Matrix chat room.


#!/usr/bin/env make -f
# the first line is a "shebang" to allow the script to be directly executed

# The first target found is the default "goal"
#  it will be executed if 'make' is not given a goal on the command line
# Convention is to have it depend on the targets ("prerequisites")
#  that will create the project without further intervention

all: build install
	echo "target: $(@)"
	# This is a "recipe" - a list of shell commands to execute
	# It MUST be indented by one or more tab characters NOT spaces
	# It can contain Automatic Variables as shown here
	# $(@) expands to "all" - the name of the current target

# If the first target isn't the default goal set it explicitly
#  rather than re-ordering targets in the Makefile
.DEFAULT_GOAL := build

# If a target is not a real file tell 'make' so it handles it correctly
.PHONY: all

# For portability recipes should only use POSIX shell syntax
#  however, sometimes a specific shell may be required
SHELL := /usr/bin/env bash 

# ideally options could be passed to the shell
# but only recent versions of "env" have the -S|--split option
# so this would not be portable:

SHELL := /usr/bin/env --split bash -o pipefail

# However,  'make' has a way to pass arguments to the shell
#  so this would be (more) portable:

SHELL := /usr/bin/bash
.SHELLFLAGS := -o pipefail -c

build:
	@echo "Must build the project"



Index