Fork me on GitHub

Project Notes


Notes on make and makefiles.


Make is a tool which controls the generation of executables and other non-source files of a program from the program’s source files. Traditionally used for compiling code, but can also be used to automate common tasks.

GNU Make is the standard implementation of Make for Linux and OS X. Common alternatives include BSD Make, Microsoft nmake, and CMake.

The Makefile

Makefiles comprise rules, macros that define the required process. Indent with TAB not spaces!

Rules have the following structure:

target .. : prerequisites ..

Built-In Rules

Run make -p in a folder with no makefile to see the default rules.

See Catalogue-of-Rules Includes:

  • Compiling C programs - n.o is made automatically from n.c with a recipe of the form ‘$(CC) $(CPPFLAGS) $(CFLAGS) -c’.
  • Compiling C++ programs -n.o is made automatically from, n.cpp, or n.C with a recipe of the form ‘$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c’.


Pre-defined variables available to make. See Implicit-Variables. Includes:

  • CC Program for compiling C programs; default ‘cc’.
  • CXX Program for compiling C++ programs; default ‘g++’.
  • CFLAGS Extra flags to give to the C compiler.
  • CXXFLAGS Extra flags to give to the C++ compiler.
  • LDFLAGS Extra flags to give to compilers when they are supposed to invoke the linker
  • LDLIBS Library flags or names given to compilers when they are supposed to invoke the linker

Internal Macros / Automatic Variables

See Automatic Variables Includes:

Macro Description
$? names of all the prerequisites that are newer than the target
$@ file name of the target of the rule
$* stem with which an implicit rule matches
$% target member name, when the target is an archive member
$< name of the first prerequisite

Special Target Names

See Special-Targets. Includes:

Target Description
.DEFAULT default commands if no matching target
.IGNORE ignore error codes, same as the -i option
.PRECIOUS Files you specify for this target are not removed
.SILENT do not echo commands, same as the -s option
.SUFFIXES suffixes that are meaningful in suffixes rules
.PHONY targets that should be run unconditionally


Compiling c++ to .exe

The exe extension is very ‘DOS/Windows’ and make doesn’t go out of its way for this to be easy with default implicit rules. But it can be done relatively generically by defining the suitable %.exe: %.cpp rule. e.g.

SOURCES := $(wildcard *.cpp)
TARGETS := $(SOURCES:.cpp=.exe)

.PHONY: all
all: $(TARGETS)

.PHONY: clean
  rm -f $(TARGETS) *.o
  rm -fR *.dSYM

%.exe: %.cpp
  $(LINK.cpp) $^ $(LOADLIBES) $(LDLIBS) -o $@


Credits and References

About LCK#72 tools
Project Source on GitHub Return to the Project Catalog

This page is a web-friendly rendering of my project notes shared in the LittleCodingKata GitHub repository.

LittleCodingKata is my collection of programming exercises, research and code toys broadly spanning things that relate to programming and software development (languages, frameworks and tools).

These range from the trivial to the complex and serious. Many are inspired by existing work and I'll note credits and references where applicable. The focus is quite scattered, as I variously work on things new and important in the moment, or go back to revisit things from the past.

This is primarily a personal collection for my own edification and learning, but anyone who stumbles by is welcome to borrow, steal or reference the work here. And if you spot errors or issues I'd really appreciate some feedback - create an issue, send me an email or even send a pull-request.