Software Carpentry Day 5 (Morning): Makefiles/Automated Builds

Exercises

The goal of these exercises is to introduce you to the execution model of Makefiles.

Type in (cut-and-paste if you want, but you'll learn more by typing it):


CMD=cut -f 1

myexecutable: main.o linkedlist.o matrix.o
        $(CMD) main.o linkedlist.o matrix.o > myexecutable
        
linkedlist.o:  linkedlist.c linkedlist.h main.h
        $(CMD) linkedlist.c > linkedlist.o
        
matrix.o:  matrix.c matrix.h main.h
        $(CMD) matrix.c > matrix.o
        
main.o:  main.c main.h
        $(CMD) main.c > main.o

test:   
        make myexecutable
        wc ./myexecutable 

clean:
        -rm *.o
        -rm myexecutable

setup:
        touch linkedlist.c linkedlist.h main.h
        touch matrix.c matrix.h main.h
        touch main.c

Please try each of the following exercises and answer the following questions:

  1. Type:
    make clean
    make setup
    make
    touch main.c
    make
    
    What actions are taken (aka what output do you see)? Can you explain why these actions (and no other actions) are taken?

  2. Write a new fresh target that does the equivalent of the clean and setup targets, without repeating any of their action lines. Once written, one would type make fresh to use your new target.

  3. Type:
    make fresh
    make
    touch linkedlist.c
    make
    
    What actions are taken (aka what output do you see)? Can you explain why these actions (and no other actions) are taken?

  4. Type:
    make fresh
    make
    touch main.h
    make
    
    What actions are taken (aka what output do you see)? Can you explain why these actions (and no other actions) are taken?

  5. What action(s) would be taken as a result of the make command in the following situation? Read the timestamps and filenames carefully.
    % ls -lt
    total 12
    -rw-r--r-- 1 user user    0 2009-07-17 06:25 main.h
    -rw-r--r-- 1 user user    0 2009-07-16 06:24 linkedlist.c
    -rw-r--r-- 1 user user    0 2009-07-16 05:22 linkedlist.h
    -rw-r--r-- 1 user user    0 2009-07-16 04:05 main.c
    -rw-r--r-- 1 user user    0 2009-07-16 03:29 matrix.c
    -rw-r--r-- 1 user user    0 2009-07-16 02:01 matrix.h
    -rw-r--r-- 1 user user  716 2009-07-16 01:25 Makefile
    
    % make
    
    What actions are taken (aka what output do you see)? Can you explain why these actions (and no other actions) are taken?

  6. What action(s) would be taken as a result of the make command in the following situation? Read the timestamps and filenames carefully.
    % ls -lt
    total 12
    -rw-r--r-- 1 user user    0 2009-07-17 06:25 matrix.c
    -rw-r--r-- 1 user user    0 2009-07-16 06:24 linkedlist.c
    -rw-r--r-- 1 user user    0 2009-07-16 05:22 linkedlist.h
    -rw-r--r-- 1 user user    0 2009-07-16 04:05 main.c
    -rw-r--r-- 1 user user    0 2009-07-16 03:39 matrix.h
    -rw-r--r-- 1 user user    0 2009-07-16 02:11 main.h
    -rw-r--r-- 1 user user  716 2009-07-16 01:25 Makefile
    
    % make
    
    What actions are taken (aka what output do you see)? Can you explain why these actions (and no other actions) are taken?

  7. Time permitting, your TA/Instructor will ask you to write a new Makefile, to specification.