Build Automation

make is a standard tool for automating builds.

convert : units.o convert.o
	g++ -std=c++17 -Wall units.o convert.o \
		-o convert

units.o : units.cpp units.h
	g++ -std=c++17 -Wall -c units.cpp

... (more rules)

clean :
	rm -f convert hw3testunits *.o *~

Usually, the first target in the makefile is named all, and it builds everything of interest.

all : convert hw3testunits

Specify one or more build targets to make.

> make clean convert

Build Targets

.PHONY: clean all

Chains of Build Rules

make figures out the graph of dependencies. If the dependencies don't exist, make will use their build ruels to make them.

convert : units.o convert.o
	g++ -std=c++14 -Wall units.o convert.o \
		-o convert

units.o : units.cpp units.h
	g++ -std=c++14 -Wall -c units.cpp

Makefile Variables

# Usually ALL_CAPS
CONVERT_OBJS = units.o convert.o

convert : $(CONVERT_OBJS)
	g++ $(CONVERT_OBJS) -o convert

Implicit Build Rules

There are built-in implicit build rules for the convenience.

e.g. if foo.o is a dependency but it doesn't have real build target, make look up the file and build it.

CONVERT_OBJS = units.o convert.o
all : convert hw3testunits

convert : $(CONVERT_OBJS)
	g++ -std=c++14 -Wall $(CONVERT_OBJS) \
		-o convert

clean :
	rm -f convert hw3testunits *.o *~

.PHONY : all clean

C++ compilation implicit rule:

%.o : %.cpp
	$(CXX) -c $(CPPFLAGS) $(CXXFLAGS) $< -o $@

# CXX: the compiler to use (g++ as a default)
# CPPFLAGS: the preprocessor flags
# CXXFLAGS: the compiler flags

So that,

CXXFLAGS = -Wall -std=c++14
CONVERT_OBJS = units.o convert.o
all : convert hw3testunits

convert : $(CONVERT_OBJS)
	$(CXX) $(CXXFLAGS) $(CONVERT_OBJS) \
		-o convert $(LDFLAGS)

clean :
	rm -f convert hw3testunits *.o *~

.PHONY : all clean

Using Automatic Variables

CXXFLAGS = -Wall -std=c++14
CONVERT_OBJS = units.o convert.o
all : convert hw3testunits

convert : $(CONVERT_OBJS)
	$(CXX) $(CXXFLAGS) $^ -o $@ $(LDFLAGS)

clean :
	rm -f convert hw3testunits *.o *~

.PHONY : all clean

Doxygen: Automatic Document Generation

Write the comment and it converts to the document using Doxygen.

# Doxyfile is a default filename
> doxygen -g [filename]
INPUT = .
OUTPUT_DIRECTORY = ./docs
PROJECT_NAME = HelloWorld
JAVADOC_AUTOBRIEF = YES
EXTRACT_ALL = YES
EXTRACT_PRIVATE = YES
EXTRACT_STATIC = YES
/**
 * this is a comment
 *
 * @param name Description
 * @return something fun
 */

/** @file ... */

/** My widget */
MyWidget mw;
MyWidget mw; /**< My widget */

Coming up next

Website Setting

Select a website color

Darkreader plugin could override color setting here.