Chapter 13 Exercise Set 1: Introducing Make¶
Make is a build automation mainly for C and C++ programs, but used in a variety of other build contexts as well.
Make is tricky to learn, and our approach here will be to introduce its use with the software we are already building, so that you can enjoy its benefits and see a few examples of it in use.
Use Learn Makefiles With the tastiest examples to get a high level feel for what Make is and how it works.
Create a directory named
Cards
and downloadsrc.tgz
into this directory. Run:tar xzvf src.tgz
in the unix command shell to extract the directory containing the following source files:
Cards.h
Cards.cpp
random.h
random.cpp
test_cards.cpp
test_decks.cpp
You can remove the
.tgz
file after extracting it.Add a file named
Makefile
in theCards
directory (along side thesrc
subdirectory) with the following contents:CC=g++ STD=c++11 build/%.o: src/%.cpp @mkdir -p build @$(CC) -MM -MT $@ $< > build/$*.d $(CC) -c -o $@ $< -std=$(STD) build/test_cards: build/test_cards.o build/random.o build/Cards.o $(CC) -o $@ $^ -std=$(STD) build/test_decks: build/test_decks.o build/random.o build/Cards.o $(CC) -o $@ $^ -std=$(STD) -include build/*.d .PHONY: test all clean test_cards: build/test_cards ./build/test_cards test_decks: build/test_decks ./build/test_decks test: test_cards test_decks clean: rm -rf build
Assuming you have Make installed on your system, you should now be able to run the following commands:
- make test_cards - make test_cards - make test - make clean
Try each of these to see what they do.