all: Compile the entire program.install: Compile and copy executables, libraries, etc., to their installation directories.install-html,install-dvi,install-pdf,install-ps: Alternative installations for documentation.uninstall: Delete all installed files.clean: Delete files created during compilation.distclean: Delete all files not in the source distribution.mostlyclean: Likeclean, but keep files you may want to keep around.maintainer-clean: Delete almost everything that can be rebuilt.TAGS: Update all tags files.checkortest: Run test suites.installcheck: Check the installed programs or libraries.dist: Create a source distribution archive.
stage1,stage2, etc.: Partially build the system, useful for bootstrapping or rebuilding.info,dvi,pdf,ps,html: Create documentation in alternative formats.
CFLAGS: Compilation flags for C code.CXXFLAGS: Compilation flags for C++ code.LDFLAGS: Flags for linking.CPPFLAGS: Flags for the C preprocessor.
prefix: Prefix for installation directories, usually/usr/local.exec_prefix: Prefix for executable-specific files, derived fromprefix.bindir: Location for executables, typically${exec_prefix}/bin.sbindir: Location for system executables, typically${exec_prefix}/sbin.libexecdir: Location for program executables not meant for user interaction, typically${exec_prefix}/libexec.datarootdir: Location for read-only data, typically${prefix}/share.datadir: Location for data files that can be shared among different architecture platforms, typically${datarootdir}.sysconfdir: Location for configuration files, typically${prefix}/etc.sharedstatedir: Location for data that can be shared by different versions of a program, usually${prefix}/com.localstatedir: Location for data that changes as the program runs, usually${prefix}/var.includedir: Header file directory, typically${prefix}/include.oldincludedir: Directory for storing old header files, typically/usr/include.docdir: Location for documentation files, typically${datarootdir}/doc/${PACKAGE}.infodir,htmldir,dvidir,pdfdir,psdir: Specialized directories for different types of documentation, typically under${datarootdir}.
DESTDIR: Use this variable for staged installs, which allows you to install files into a temporary location (DESTDIR) rather than their final location.- Commonly used in package building.
- Example:
make install DESTDIR=/tmp/package-root - Note:
DESTDIRis prepended to the file paths but should not change the code's behavior.
- Use
-MMD -MPoptions with GCC to generate dependency files. - Include dependencies in the Makefile:
-include $(DEPS).
ifeq ($(CC),gcc)
CFLAGS += -Wall
endifUse .PHONY to declare phony targets, ensuring they always execute.
.PHONY: all install uninstall clean distclean mostlyclean maintainer-clean TAGS check test installcheck dist info dvi pdf ps html
Suppress command echoing with @:
@echo "Building..."
Continue despite errors in certain rules with -:
-rm some-file
-
=: Recursive expansion. Value is expanded each time the variable is used.VAR = value -
:=: Simple expansion. Value is expanded at the time of assignment.VAR := value -
?=: Conditional variable assignment. Assigns value only if the variable is not yet defined.VAR ?= value -
+=: Append to the variable value. Adds the given value to the existing variable value.VAR += value
- Define implicit rules for transforming file types.
%.o: %.c $(CC) $(CFLAGS) -c $< -o $@
- Implicit rules can simplify your Makefile by applying general rules for file transformations.
-
Use functions for string and file manipulations.
-
$(wildcard ...): Gets a list of filenames matching the pattern.SOURCES := $(wildcard *.c)
-
$(patsubst ...): Pattern-based string substitution.OBJECTS := $(patsubst %.c,%.o,$(SOURCES))
-
$(subst ...): Replace text within a string.NEW_TEXT := $(subst old,new,$(TEXT))
-