Chapter 11 Exercise Set 3: 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 HelloMake 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:

    • HelloMake.h

    • HelloMake.cpp

    • test_hellomake.cpp

    You can remove the .tgz file after extracting it.

  3. Add a file named Makefile in the HelloMake 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_hellomake: build/test_hellomake.o build/HelloMake.o 
    	$(CC) -o $@ $^ -std=$(STD)
    
    -include build/*.d
    
    .PHONY: test all clean
    
    test_hellomake: build/test_hellomake
    	./build/test_hellomake
    
    test: test_hellomake
    
    clean:
    	rm -rf build
    
  4. Assuming you have Make installed on your system, you should now be able to run the following commands:

    - make
    - make test
    - make clean
    

    Try each of these to see what they do.