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.

  1. Use Learn Makefiles With the tastiest examples to get a high level feel for what Make is and how it works.

  2. Create a directory named Cards and download src.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.

  3. Add a file named Makefile in the Cards directory (along side the src 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
    
  4. 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.