Home

HW02: Makefile

Create a Makefile for existing code.

Name your BitBucket repository exactly csci221-HW02. Test with the script /home/jeckroth/csci221/homework/HW02/grader.pl [bitbucketusername]

Requirements

Copy the contents of the folder (on Londo) /home/jeckroth/csci221/homework/HW02/rational. It contains three files: main.cpp, rational.cpp, and rational.h.

Create a Makefile so that one could easily build a binary file out of these sources.

  • main.o depends on main.cpp and rational.h
  • rational.o depends on rational.h and rational.cpp
  • ratdemo depends on main.o and rational.o

The Makefile must have the following “targets”:

  • all, which depends on ratdemo target and otherwise does nothing
  • ratdemo, which links main.o and rational.o, creating the binary ratdemo
  • main.o, which compiles main.cpp
  • rational.o, which compiles rational.cpp
  • clean, which removes (rm -f) the generated files main.o, rational.o, and ratdemo

Your Makefile should enable the following sequence of events. Note that the touch command simply updates a file’s last-modified timestamp, thus causing the make program to belief the file has changed (and so dependent files are out of date). Notice that, in each case, only necessary actions are taken to update the binary. (This makes a huge difference when you have thousands of files and build times that are measured in hours.)

$ make
g++ -c main.cpp
g++ -c rational.cpp
g++ -o ratdemo main.o rational.o
$ touch main.cpp
$ make
g++ -c main.cpp
g++ -o ratdemo main.o rational.o
$ touch rational.cpp
$ make
g++ -c rational.cpp
g++ -o ratdemo main.o rational.o
$ touch rational.h
$ make
g++ -c main.cpp
g++ -c rational.cpp
g++ -o ratdemo main.o rational.o
$ make clean
rm -f main.o rational.o ratdemo
$ make
g++ -c main.cpp
g++ -c rational.cpp
g++ -o ratdemo main.o rational.o

CSCI 221 material by Joshua Eckroth is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License. Source code for this website available at GitHub.