This is a makefile designed to be dropped in to a project and for the most part
"just work" after a little bit of configuration in a
Make is a great tool, but I remember how hard it was to learn. It's very different from most other build systems. It's my hope that this file can be useful for someone who's new to C or C++.
If there's anything you'd like to see changed, for example limitations or bad practices, feel free to create an issue.
Download the Makefile:
and create a config file for it:
Now just run
make, and all C source files in
src/ will be compiled into a
example-program. If you change any header or source file, only
the the necessary files will be recompiled the next time you run
To compile C++ instead of C:
Here, we set the file extension to .cc (the UNIX convention for C++ files).
$(CC), the Makefile will now use
$(CXX), which is a C++
compiler (generally g++ on GNU/Linux systems).
Slightly bigger example
The example directory contains a small example project.
TARGET is the name of the binary.
FILES is the list of files to compile.
$(shell find $(SRC) -name '*$(EXT)' | sed 's/^.\///) - Find
all files with the extension you specify with
EXT in the directory you
sed command is to remove the annoying
./ prefix you
get when setting
SRC to the current directory (
EXT is the filename extension for your source files, generally
.c for C and
SRC is the directory which contains the source files.
COMPILER is the compiler. This defaults to
$(CC) otherwise. You generally don't need to change this yourself, as long as
you use the extension
.cc for C++ and
.c for C.
WARN is a list of warning options.
all pedantic (expands to
LINK is a list of shared libraries to link with. To link with the math
library for example (
LINK=m (expands to
INCLUDE is a list of directories to append to your include search path. To
include a directory called
INCLUDE=headers (expands to
LIBS is a list of statically linked libraries (generally
the final binary will be linked with.
FLAGS is general compiler flags.
FLAGS_DBG is flags which are only applied when compiling in debug mode (aka
-g -o0 -DDEBUG
FLAGS_NDBG is flags which are only applied when compiling without debug mode.
DEPS is additional targets you want to add as a dependency for
JUNK is additional files to be deleted with a
TOOLCHAIN is mainly for cross compiling. If you set
CC=gcc, source files will be compiled with
aarch64-linux-gnu-gcc, and thus be compiled for 64-bit ARM.