Starting Point Code

Start by creating a week08 directory in your cs44/weeklylabs subdirectory and copying over some files:

cd ~/cs44/weeklylabs
mkdir week08
cd week08
pwd
  /home/you/cs44/weeklylabs/week08
cp ~newhall/public/cs44/week08/* .
ls
Makefile  README  fancyMakefile  mylib.c  mylib.h  prog.c

make and Makefiles

We are going to briefly look at the lab Makefile together and discuss some of its syntax and how it is used when a user types make or make clean or …​

make is a very powerful tool for building programs, and is use extensively to build application and system software. It is important to know some of the basics of how it works and the syntax of Makefiles so that you can read and understand what a Makefile is doing, and also so that you can write (or edit) Makefiles to use make for your own purposes.

make is particularly useful when compiling code like CS44 lab that includes multiple source and header files, and that links in custom libraries. In cases like this, there may be multiple long g++ command to give to compile the executable program. By using make a user writes a Makefile with these compilation rules one time, and then just runs make to recompile their code. In addition, for programs that are compiled from many sources and that may take a very long time to compile, make only re-builds the parts that need to be rebuilt based on changes to source and header files, reducing the compilation time.

Finally, make and Makefiles can be used to help build all kinds of things (it is not only for building C and C++ programs). For example, the CS faculty use it to build and install webpages for our courses.

Makefile

First, let’s open up the Makefile in the code you copied over, and decipher its contents to get a sense of what it is doing.

I encourage you to also open Dive into System’s coverage of make and Makefiles in Appendix 2 (Chapt. 17) to refer to more explanation and examples as we go.

A Makefile typically starts with some variable definitions which are then followed by a set of rules (or target entries) for building specific targets (typically for building .o and executable files). Each rule consists of a target label and a set of associated commands (sometimes called recipes) that are run for the target.

We will look for a few things:

  1. comments

  2. variable definitions, and their syntax

  3. target entry syntax

    target:  dependency1 dependency2 ...
          <tab> command
  4. some common target entries in Makefiles and what they do

fancyMakefile

Next, let’s quickly look at fancyMakefile that includes some more advanced features of makefiles, and is a better generic Makefile.

Lab Makefile

Next, let’s look at the Makefile for the current lab assignment and see how it compares to this example.

Resources

Here is some more information from Dive into Systems Appendix 2 (Chapt. 17).

The example Makefile from 7.5.10 is an example of good generic featureful Makefile that one could use and and modify for their own programs.