CS 70

Homework 3: Written Part

Learning about Makefiles

Hopefully you've done the lab activity on make. If not, please do that before continuing with this assignment.

Style Readings

In this assignment we will begin grading your code more strictly on style. Please read one (or both) of the following documents on style. (You can skip this part and save it for later, if you and your partner want to get right to coding.)

  • LHS Cow speaking

    Remember, you don't have to do the readings during precious lab time. You can do the readings outside of class and wait to do the written part until then.

The Questions

The written questions for this assignment can be completed individually or with your partner. Before you begin answering, decide whether you'll answer them together or you'll each do them separately. Either way, your answers are saved as you go, and you can come back and change them until the deadline.

Question 1 – Readings

Question 1.1

Which style reading(s) did you choose?

Question 1.2

What were your main takeaways from the reading(s) you chose?

Question 2 – Explaining Makefiles

Before answering this question, review the information on Makefiles linked from the homework writeup (e.g., the example on GitHub) or other sources.

Question 2

How would you explain what a Makefile is to a friend in CS 5?

Question 3 – Reading Makefiles

If you haven't done so yet, clone the assignment repository on the CS 70 server so you can look at the code as you answer.

In the directory segfault-generator you'll find a simple program which uses one class (with a header) and one main file. In order to compile the generateSegfault executable, both .cpp files need to be compiled. The provided Makefile contains

  • Two rules for compiling .cpp code into object files (i.e., machine instructions);
  • One rule for linking these two object files into an executable program; and
  • A helper rule so that we can say make clean to get back to the initial state with nothing compiled or linked.

Consider the following rule from the provided Makefile (which you are free to examine in more detail):

helloSayer.o: helloSayer.cpp helloSayer.hpp
    clang++ -c -o helloSayer.o -g -std=c++20 -pedantic -Wall -Wextra helloSayer.cpp

Question 3.1

What does helloSayer.o mean in the first line?

Question 3.2

What does helloSayer.o mean in the second line?

Question 3.3

What does helloSayer.cpp mean in the first line?

Question 3.4

What does helloSayer.cpp mean in the second line?

Question 3.5

What does helloSayer.hpp mean in the first line?

Question 3.6

Why is helloSayer.hpp not in the second line?

Question 4 – Running Makefiles

If you haven't already done so, cd to the segfault-generator directory and run the command

cs70-make generateSegfault

Read over the output—you may need to make your terminal window bigger. See what actions cs70-make took and why.

Now that the program has compiled, run the command

touch generateSegfault.cpp

(The touch command sets the last-modified-time of a file to the current time, which will fool make into thinking we have made a change to the file, even though its contents are unchanged. The touch command is handy for testing/debugging Makefile rules, because you can easily check to see which commands make will run if a particular file appears to have changed.)

Now run the command again:

cs70-make generateSegfault

Read over the output and see what actions cs70-make took and why.

Question 4.1

Which of the commands in the Makefile get run when we execute the second call to cs70-make? (select all that apply)

Question 4.2

Explain why these were the right commands for cs70-make to run.

Question 5 – Improving Makefiles

The command make is the same as cs70-make but just less chatty. We'll switch to using it for the remaining examples.

If we just run make without specifying a specific target, then (only) the first target in the Makefile (and all its recursive dependencies) will be checked for possible rebuilding.

Question 5.1

According to the given Makefile, running the command make by itself is equivalent to running what more specific command?

Question 5.2

It would be better if running make with no argument would build the most useful target—the product we care about most. Which rule should we move to the start of the Makefile?

Question 6

Run make generateSegfault to generate the executable you can run. Then run the resulting executable (i.e., ./generateSegfault).

Question 6.1

Briefly describe what happens when you run the program.

Question 6.2

Look at the source code for generateSegfault; based on the code, why did it happen?

Choose a New Partner

Homeworks 4, 5, and 6 will be done with a new partner. You don't have to sort this out immediately, but start thinking about it now, and be sure to choose a partner and register before the deadline.

To Complete This Part of the Assignment…

You'll know you're done with this part of the assignment when you've done all of the following:

(When logged in, completion status appears here.)