Skip to main content

Sponsors

Inside Makefile:

TARGET: DEPENDENCY LINE OR RULES LINE
COMMAND
COMMAND
...

A command line (with TAB in front) is running in a subshell by itself.

# A comment.
name = text in name Macro definition.
$A Single character macro reference.
$(name) or ${name} Macro references.
make -p Print internally defined macros.
make target "DIR=/dir /dir2" Use DIR defined in command even if you defined DIR in your Makefile!
DIR=/bin make target Use DIR in Makefile if any, otherwise use DIR=/bin.
make -e Shell variables have higher priority than file macro definitions.
FILE=a.c b.c
@echo ${FILE:.c=.o}
a.o b.o
Replace suffix .c by .o. The @ suppress the printing of commands executed.
$@ Current target. Use in command line only.
$? List of pre-requisites newer than current target. Use in command line only.
$$@ Current target. Use in dependency line only.
$* File name without suffix part. Use in commands in suffix rule only.
$% Name of the .o when target is a library module.
$< The pre-requisite file. Use in commands in suffix rule only.
.SUFFIXES: .o .c Suffixes that make will consider.
.c.o:
${CC} -c $<
Suffix rule. To produce .o from a .c.
t: libm.a(sin.o) Target t depends on sin.o in libm.a. Hence, if sin.o is newer than libm.a, implicit rule replace newer sin.o into libm.a, then create target t.
libm.a :: sin.c
libm.a :: cos.c
Rebuild libm.a using different commands depends on the pre-requisites.
- rm -f *.o Ignore errors in the command rm, hence, errors will not stop the make process.
t: a.c \
b.c
Use '\' to continue with the next line.
.SUFFIXES:
.SUFFIXES: .a .b
.SUFFIXES: .c
1. Clear implicit suffixes. 2. Define .a .b suffixes. 3. Add .c into the suffix list.
.c.a:
true
Override default suffix rule with null command
t:
echo $$HOME
Use $$ in commands for environment variable.
.IGNORE Force make to ignore errors and keep going.
${<D}, ${<F} Pre-requisite file directory, pre-requisite file name.
VPATH View path.
include inc.mk Include the file inc.mk.
.PRECIOUS: a.x b.x Do not let make deletes files a.x and b.x.
.SILENT Do not echo commands while executing.
.DEFAULT:
echo "hello"
Define a default action.

Reference: C Programming Utility: Managing Projects with make by Andrew Oram and Steve Talbott