Due date: Monday, March 8, 2021.
In this lab we will start understanding and creating Makefiles. You will also need to create a file called lab6.txt containing the commands run in the terminal and the results, plus answers to some questions in the lab.
Create a folder called week6 and another folder called sum. Go to this folder and copy the original file SumArray.java (http://www.cs.iusb.edu/~dvrajito/teach/c151/SumArray.java) that we used for Lab 5, and not the one after the modifications.
Create a file called Makefile with the editor of your choice. This file should not have any extension.
Place a comment (line starting with #) at the top of the file with your name in it.
If you need several lines for the comment, each line needs to start with #.
A Makefile usually contains one target called default so that the command make can be run without any argument, and a target for the main application we want to compile with it. In our case, it's the file SumArray.class.
Let's start with a target called default. For
this, add the line
default:
You can place it a couple of lines below the comment at the top.
Add a space after the colon and add the dependency SumArray.class to this target. Your line should look like this:
default: SumArray.class
The dependency SumArray.class will redirect the make command to the target called SumArray.class if no target is specified. There is no other command to execute for the default target, which means that the line right below this target should be left empty.
Add a target called SumArray.class the same way.
This will correspond to the compiled .class file by the same name.
As dependency list, write the file name SumArray.java. On the line right after it, start by typing a tab, and then continue with the compilation command
javac SumArray.java
It is important for the line containing the compilation command to start with a tab, otherwise you will get an error message when you run make. The effect of this target is that if the file SumArray.java has a last modification time stamp newer than the compiled SumArray.class, the compilation command will be run. The same will happen if the compiled file is not present in the folder.
Save the file. Go back to the terminal and run the command make.
This way we'll see if the content of the Makefile you have so far works. You should see the compilation command being executed because the executable doesn't exist yet.
From the terminal, execute the java command with the file SumArray.class as argument to see if it works. Then run the command make again.
Instead of the compilation command, the make will give you a message. Why is the result of the command different from the first time you run it? You will add the answer to this question to the file lab6.txt.
Copy the content of the terminal you have so far to the file lab6.txt. Right after it, add a short explanation for the outcome of the second command make that you've run.
Next, we would like to add a target that allows us to recompile the program with debug information.
Add another target called debug. List no dependencies for it. The command under it should be the same as before, but with the added option -g.
The lack of dependencies on this target means that if we run the make command with it, the compilation command will be run regardless of the source file being edited or not.
To run the make with this target, do
make debug
This third run of the command make behaves yet differently from the ones before. You will add an explanation for the outcome.
Copy the content of the terminal since the last time you've done it into the file lab6.txt and explain (briefly) the difference between the outcome of this command make and the previous one.
Normally, a Makefile needs a target that removes any compiled files and jar files, in case we want to start over.
Add another target called clean with no dependency. As the command to be executed for it, remove (delete) the compiled file SumArray.class. Remember that the command to delete a file is rm.
It is important to use the common name for this target, to make it easier for the user to figure it out. Let's see if it works.
Run the command make with this target to delete the executable, then run the make without any target (thus using the default) to compile the program again. Copy the terminal from the last time up to this point to the file lab6.txt.
Open the following Makefile in a browser, or download it and open it with a text editor, or look at its content with the command less:
Makefile (http://www.cs.iusb.edu/~danav/teach/c243/p2/Makefile)
You won't need to change this file, so you don't need to download it if the web browser will display it. If the browser can't display it, save it to your local computer and open it with a text editor such as Emacs, Notepad, or TextEdit. It doesn't matter if it is local or remote, as long as you can see its content.
A copy is uploaded to Canvas, Files, Week 6, with the name Makefile2. This is a Makefile sometimes used in C243.
Examining the file, you can identify some of the units (variables, comments, targets) to get familiar with it and to make sure you understand them.
Continue editing the file lab6.txt with the answer to the following:
a. What is the application file created by the command make with no explicit target using this file?
b. Give an example of a variable in this file and point out where it is used.
c. Give an example of a target with dependencies in this file. Identify its dependencies and specify whether they are files or not, and if they correspond to other targets or not.
d. Let's assume that since the last compilation, the file ListNode.h has been modified. What would happen in this case if the command make is run again? Specifically, which targets/commands can you expect to be triggered by the file being modified?
Upload to Canvas: under Assignments, Lab 6: