Skip to content
James Perretta edited this page Mar 7, 2015 · 7 revisions

How to look at this file

  1. See the Makefiles wiki page for a list of Makefile tutorials
  2. If some of it appears to be black magic, that's probably fine. The only part you need to understand is how and when to add new Makefile rules like the foo.o one. See the inline comment for a brief explanation.

Have questions? Ask in the UMich CSE Group or, better still, your class' Piazza.

The Makefile

# Switches to a modern C++ compiler on CAEN
PATH := /usr/um/gcc-4.8.2/bin:$(PATH)
LD_LIBRARY_PATH := /usr/um/gcc-4.8.2/lib64
LD_RUN_PATH := /usr/um/gcc-4.8.2/lib64

CXX              = g++
CPP_FILES        = $(wildcard *.cpp)
COMPILED_OBJECTS = $(CPP_FILES:%.cpp=%.o)
EXECUTABLE       = exe
SUBMIT_FILE = submit.tar.gz

CXXFLAGS = -std=c++11 -Wall -Wextra -pedantic

release: CXXFLAGS += -O3 -DNDEBUG
release: all

debug: CXXFLAGS += -g3 -DDEBUG
debug: clean all

all: $(COMPILED_OBJECTS)
    $(CXX) $(CXXFLAGS) $(COMPILED_OBJECTS) -o $(EXECUTABLE)
    @echo -e '\033[0;32m==========Successfully finished build==========\033[0m'

# This is an example of how to utilize the pattern matching rule below. 
# Note how a literal compilation line is unnecessary.
foo.o: foo.h foo.cpp bar.h

# The default rule for creating object files
%.o:
    $(CXX) $(CXXFLAGS) -c $*.cpp

clean:
    rm -f $(COMPILED_OBJECTS) $(EXECUTABLE) $(SUBMIT_FILE)

test: debug
# You add tests here

MY_FILES=$(wildcard Makefile *.h *.cpp test*.txt)
$(SUBMIT_FILE): $(MY_FILES)
    dos2unix $(MY_FILES)
    tar -vczf $(SUBMIT_FILE) $(MY_FILES)

submit_file: $(SUBMIT_FILE)

# These targets do not create any files
.PHONY: all release debug clean submit test
# Disable built-in rules
.SUFFIXES:

Alternate Version with Automatic Dependency Generation

This version of the Makefile will automatically determine what needs to be recompiled when a file changes. (Note that this version currently will not work on the 281 autograder)

# Switches to a modern C++ compiler on CAEN
PATH := /usr/um/gcc-4.8.2/bin:$(PATH)
LD_LIBRARY_PATH := /usr/um/gcc-4.8.2/lib64
LD_RUN_PATH := /usr/um/gcc-4.8.2/lib64

CXX              = g++
CPP_FILES        = $(wildcard *.cpp)
COMPILED_OBJECTS = $(CPP_FILES:%.cpp=%.o)
EXECUTABLE       = exe
SUBMIT_FILE = submit.tar.gz

# Flags passed to the preprocessor. -MMD and -MP will cause the preprocessor
# to detect dependencies automatically.
CPPFLAGS = -MMD -MP

CXXFLAGS = -std=c++11 -Wall -Wextra -pedantic

release: CXXFLAGS += -O3 -DNDEBUG
release: all

debug: CXXFLAGS += -g3 -DDEBUG
debug: clean all

all: $(COMPILED_OBJECTS)
	$(CXX) $(CXXFLAGS) $(COMPILED_OBJECTS) -o $(EXECUTABLE)
	@echo -e '\033[0;32m==========Successfully finished build==========\033[0m'

# The default rule for creating object files
%.o: %.cpp
	$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $*.cpp

clean:
	rm -f $(COMPILED_OBJECTS) $(EXECUTABLE) $(SUBMIT_FILE)

test: debug
# You add tests here

MY_FILES=$(wildcard Makefile *.h *.cpp test*.txt)
$(SUBMIT_FILE): $(MY_FILES)
	dos2unix $(MY_FILES)
	tar -vczf $(SUBMIT_FILE) $(MY_FILES)

submit_file: $(SUBMIT_FILE)

# Imports the dependency files generated by the preprocessor.
-include *.d

# These targets do not create any files
.PHONY: all release debug clean submit test
# Disable built-in rules
.SUFFIXES: