Lab - Makefiles

Setup

You're going to create a fairly simple makefile that performs some basic, common tasks. Compile a C program. Run a C program. Run a Python program. Compile and run a java program. Check for a README, display it. Compare 2 files. Clean up intermediary files.

Go through the steps below, adding 1 target at a time (and testing!) to your makefile.

Create a directory for this lab, then copy the following files from ~kschmidt/public_html/CS265/Labs/Makefiles :

You will create a makefile called Makefile, and fill that in as follows:

To Do

  1. First, at the top of the makefile, define the following variables:
  2. Create a target called foo.o . It will depend upon the files foo.c and foo.h . Here is the command that would do it:

    $(cc) -c foo.c
  3. Create a target called main.o . It will depend upon the files main.c and foo.h . Here is the command that would do it:

    $(cc) -c main.c
  4. Now we want to make the executable from those object files. Create a target called $(EXE), which would depend upon the files foo.o and main.o . Here is the command that would do it:

    $(cc) -o$(EXE) main.o foo.o

    To test this file, you would type make foo . make $(EXE) wouldn't work.

  5. Now we want to run the program. Create a target called run-c, which would depend upon the file $(EXE) . Here is the command that would do it:

    ./$(EXE)
  6. Now, add a target called run-py that simply runs the foo.py, in the current directory. It would, of course, depend on foo.py .
  7. Create a target called read that checks for a file called README ; if it exists, display it using $(PAGER), from above.

    Note that you don't want to list any dependencies here. We don't want make to yell at us if README doesn't exist; we want to check, quietly.

  8. Create a target called foo.class . It will depend upon the file foo.java . Here is the command that would do it:

    $(JAVAC) foo.java
  9. Create a target called run-java . It will depend upon the file foo.class . Here is the command that would do it:

    $(JRE) foo
  10. Create a target called save-java , that will run your java program, but redirect the output to $(TEST_OUT). But for the redirection, this target will look very much like the previous one.

  11. Create a target called test-java . We want to compare your output w/the expected output. It'll depend on the 2 output files, $(TEST_OUT) and $(EXP_OUT) . Here're the bash lines that would do it:

    -@if diff $(TEST_OUT) $(EXP_OUT) &> /dev/null ; then \
      echo "Passed!" ;\
    else \
      echo "Not the same!" ;\
    fi
  12. Create a target called clean that cleans up all generated files (the object files, the class file, the executable, and the test.out file).

  13. Finally, note that clean run-py run-c read run-java save-java and test-java are all phony targets. Note this, near the top, under the variables.

  14. Submit this Makefile along w/the files from the other parts of the lab.