Makefile templates for simple C and C++ programs.
Usage:
make [TARGET] [OPTIONS]
Possible options include
DEBUG=1
: enable debugging information (-ggdb3
), optimize for debugging (-Og
), and enable sanitizers.SANITIZE=1
: enable sanitizers (address, undefined, leak, thread).- For thread sanitization, needs
WANT_TSAN=1
, for leak sanitization needsLSAN=1
.
- For thread sanitization, needs
PTHREAD=1
: link with pthread library.PROFILE=1
: write profile information for use with the analysis programgprof
(-pg
).PIE=0
: disable generation of position independent executables.FAST=1
: enables optimization level 3 (-O3
), and generates CPU-specific code (-march=native
), and enables some other options that might slightly increase performance.
These options can also be set directly inside the Makefile.
Possible targets are
all
(default): builds all programs.format
: formats source files withclang-format
(according to the style defined in the.clang-format
file).clean
: removes build files and executables.
Recommendation: for faster builds, set your MAKEFLAGS
environment variable to use the full number of logical cores on your machine.
Linux:
export MAKEFLAGS="-j$(grep -c ^processor /proc/cpuinfo)"
MacOS:
export MAKEFLAGS="-j$(sysctl -n hw.ncpu)"
Adapted from the CS61 section notes Makefile at https://github.com/cs61/cs61-sections.