Class 7 — Tuesday, September 15

make, and the one line in a Makefile that decides whether the program you just ran is the program you just wrote.

1 Predict-then-run

On the screen: a program built from three files, with a Makefile beside it.

make rebuilds the program only when it decides it needs to.

Then this runs:

touch numbers.c

touch does not change a single byte of numbers.c. All it does is set the file’s last modified time to now.

Write your prediction in your Brightspace Journal before make runs: does it rebuild the program, or does it tell you there is nothing to do? One sentence on why.

Two minutes, and a guess counts.

2 The ladder

Note: If you have been stuck for three minutes, put your hand up.

Pairs, one keyboard between two people. The person not typing reads this page out loud and says what to type next, and you swap who types at every rung.

Nothing here changes what the program computes.

2.1 Step 0 — what you need before Rung 1

Two things: the three-file program, and a Makefile beside it.

The three files. If you already have numbers.h, numbers.c and main.c from last class, you are done with this part — go on to the Makefile below.

Otherwise, nothing new gets written here. Take the functions you already have, even if that is only max, and move them:

  • numbers.c — the definitions, and no main.
  • numbers.h — the prototypes. Last class’s page gives this file in full, include guard and all.
  • main.c — main and nothing else, opening with #include <stdio.h> and #include "numbers.h".

Check it builds, with both .c files named and the .h file not mentioned:

gcc -Wall main.c numbers.c -o numbers

The Makefile. If you wrote one for A3 already, use that one. If not, here it is:

numbers: main.c numbers.c numbers.h
	gcc -Wall -o numbers main.c numbers.c

clean:
	rm -f numbers

The whitespace before gcc and before rm is one tab character, not spaces. With spaces, make says missing separator and stops. In nano the Tab key can give you a real tab but may insert spaces if it has set tabstospaces. If it does, you can copy and paste the tabs from the listing above.

Done when make builds the program and ./numbers runs.

2.2 Rung 1 — make, clean, make

Run these three, in order, and read what each one prints:

make
make clean
make

Then run make a fourth time without touching anything at all.

Done when you can say, for each of the four, why it did what it did.

2.3 Rung 2 — give the program something to say about itself

Add this line to numbers.h, inside the #ifndef, above the prototypes:

#define VERSION "version one"

A #define is the same idea as #include: a paste. Everywhere the name VERSION appears, the text after it gets pasted in, before the compiler sees any of it.

Then add a line to the top of main:

printf("%s\n", VERSION);

Build it and run it.

Done when ./numbers prints version one before the rest of its output.

2.4 Rung 3 — take one file off the list

In your Makefile, the first line names the files the rule depends on. Delete numbers.h from the end of it, so the rule reads:

numbers: main.c numbers.c

Change nothing else. Now edit numbers.h and only numbers.h: make it say version two. Save it.

Then, in this order:

make
./numbers

Read both. Do not fix anything yet.

Done when you can say what make decided and what the program told you, and why those two facts are worth being upset about. Write down to notes.txt.

2.5 Rung 4 — put it back

Add numbers.h to the end of that first line again. Run make, then ./numbers.

Done when you can finish this sentence out loud: the word numbers.h in that line are what buy me … Write down to notes.txt.

3 If you have already finished

3.2 Then, still in the .o version

Leave the program built — no make clean here. With everything deleted there is nothing left for make to decide.

touch numbers.h
make

Count the commands. Then:

touch numbers.c
make

Count again. The two are not the same length. Work out why from the Makefile alone, without running anything else.

Then, for one line of reading that belongs to this course more than to this week: make -j2. Run make clean and then make -j2, a few times. Watch the order the lines come out in.

4 Then the book’s exercises for this week’s reading

This runs in the browser and marks itself, which is the fastest feedback available to you anywhere in this course. Nothing is collected.

  • Functions — eight questions, two coding exercises.

5 Before you leave

A3 is due tomorrow, and its last file is the Makefile you have spent today inside. If you are short on time tonight, the assignment has a box near the top that says what to get to first — read that rather than guessing.

The directory you have been working in is most of A3 already — the three files, and the Makefile. So put it on Brightspace now, before you stand up. That is not a separate chore from tonight: it makes tomorrow a replacement rather than a first attempt, and Brightspace keeps the latest version.