Activity  7-2 - Using Make and Makefiles


Makefiles

Every time you make a change to one of your files, you need to recompile that file and then re-link the object (.o) files to create the executable file.

Also, you do not have recompile files that have not been changed. Instead of manually handling this, we can use the Make tool.

Make allows you to create a file that is usually named Makefile - note the capitalization of the first letter. This file contains the "rules" of what and how to compile multiple files associated with a project. Actually, Make is a very powerful tool with a great deal of flexibility and is not limited to files in the project.

Makefile Rules

make rules have the general form:

target: dependency list 
	command_1 
	command_2 
	...

Remember, the first thing on the command line (g++ line above) must be a TAB character.

Use # in a make file for comments, # works like // in C++.

Exercise 7.3

Create or ssh the Makefile listed below.

Be very, VERY careful to use TABS and not spaces at the beginning of the line.

Copying and pasting may cause the TABS to be converted to spaces. You may download the file Makefile here and ssh it for ease of use.

# Makefile for Lab 7
# CS 23021 Separate Files Lab

# Makefile "variables", occurrences of ${CXX} are replaced with g++
CXX = g++
CXXFLAGS = -Wall

#----------------------------------------------------------------------------
# Link, make shapes executable

#target: (tab) file_1_it_needs (space) file_2_it_needs  #called dependencies
#(tab)  rule_to_make_dependencies
avg:    average.o avg_driver.o 
        ${CXX} ${CXXFLAGS} avg_driver.o average.o -o avg

# Compile average.cpp 
average.o:      average.hpp average.cpp 
        ${CXX} ${CXXFLAGS} -c average.cpp

# Compile avg_driver.cpp
avg_driver.o:   avg_driver.cpp
        ${CXX} ${CXXFLAGS} -c avg_driver.cpp

#----------------------------------------------------------------------------
clean:
        rm -f avg  # remove the executable avg file
        rm -f *.o  # remove ALL object files in the directory

Clearing the terminal window before compiling can make compiler error messages be more easy to read. Type:
clear
make executable_name

Typing make avg will compile the program.
Typing make clean will remove object files and the avg executable.

Be sure to do a listing (ls) to check and see that the .o files are removed once you use make clean.

Another way to write our Makefile in a more general sense is:

target: (tab) file_1_it_needs (space) file_2_it_needs #called dependencies 
	(tab) rule_to_make_dependencies -o target
	
file_1_target.o: (tab)	file_1_target.hpp (space) file_1_target.cpp 
	(tab) rule_to_make_dependencies 
	
file_2_target.o: (tab)	file_2_target.cpp 
	(tab) rule_to_make_dependencies 

Notes

Remember to follow the good programming practices for each program.

If you have any trouble with the assignment or using the system do not hesitate to ask your lab instructor.

Be sure that you have this working before you work on the post-lab's Makefile.

Your just used a Makefile! Congrats.