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.)
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 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 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
.cppcode 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 cleanto 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 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 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 6
Run make generateSegfault to generate the executable you can run. Then run the resulting executable (i.e., ./generateSegfault).
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.
(When logged in, completion status appears here.)