Using a makefile for Managing a C++ Programming Project

A "makefile" is typically a list of rules and dependencies with corresponding commands to create "targets" using the rules and dependencies. We will be using a makefile to manage a list of files that "make" or build a C++ executable program.

A C++ program can be (and usually is) built (created) with several source code files and several header files. Source code files .cpp and .h header files typically build or create a program.

The purpose of the make utility is to build, upon request *by you* the user, the executable program from a set of files you describe in the actual makefile itself.

An Example :
Lets say we have 3 .cpp files and 2 .h files that make up our program. They are:
main1.cpp
mylib.cpp
mylib.h
openfile.cpp
openfile.h

We decide we want to create an executable program named lab1.out
So, we set up a make file. HOW? We use our favorite text editor (vi or pico or emacs) and we create our make file, lab1.make. We name it lab1.make so we know its a make file and not a source code or header or executable file. If you have separate directories for each assignment(a good idea) then naming the file "makefile" is perfectly logical, since it will be in the directory of the files to "make".

Once in our editor we write the make file which is really a group of targets, rules, and dependencies, here is a make file named lab1.make created using an editor.

# lab1.make -- this is a comment line, ignored by make utility

lab1.out : main1.o mylib.o openfile.o
      g++ -o lab1.out main1.o mylib.o openfile.o
# above, we are saying lab1.out depends on main1.o, mylib.o and openfile.o
# and to create lab1.out we give the g++ command as shown on the next line

# which starts with a TAB although you cannot see that .
# note that the command : g++ -o lab1.out main1.o mylib.o openfile.o
# creates an executable file named lab1.out from the 3 object files respectively.
main1.o: main1.cpp openfile.h mylib.h
      g++ -c main1.cpp
# above we are saying main1.o depends on main1.cpp openfile.h and mylib.h
# and to compile only main1.cpp if and only if main1.cpp or openfile.h or mylib.h
# have changed since the last creation of main1.o
mylib.o: mylib.cpp mylib.h
     g++ -c mylib.cpp
# above we are saying mylib.o depends on mylib.cpp and mylib.h
# so if either mylib.cpp or mylib.h CHANGED since creating mylib.o
# comple mylib.cpp again
openfile.o: openfile.cpp openfile.h
      g++ -c openfile.cpp
# above we are saying openfile.o depends on openfile.cpp and openfile.h
# so if either openfile.cpp or openfile.h has CHANGED since creating
# openfile.o, compile only (again) openfile.cpp
clean:
 &nbps    rm *.o lab1.out
# above we are stating how to run the rule for clean, no dependencies,
# what we want is when we ask to do a "make -f lab1.make clean"
# that will not do anything except remove executable and object files
# so we can "clean out" our directory of unneeded large files.
# we only do a make clean when we want to clean up the files.

# END OF MAKE FILE

Once you create this make file shown above, save it and it is now named lab1.make in your current directory.

Now at the command line (the terminal window), we want to tell the make utility to actually "make" or "build" the program. The command is

$ make     -f     lab1.make

If each .o is successfully created then the executable program, lab1.out, should be created.

To actually run the program we would "execute" lab1.out with the command:
$     ./lab1.out

If you change ANY file involved in the creation of the program, any .cpp file or any .h file, then you MUST "make" the program again by executing the command:
$ make    -f    lab1.make
Remember, the command "makes" or builds the program, but it does so wisely. The make utility will only rebuild those files that are used to build the targets if the dependency files have changed since the previous "make" of the program.

$make    -f    lab1.make    clean
CLEANS only the files lab1.out and any .o files.

Here are the steps to create a makefile from the unix command line:

 $ emacs lab1.make 
    ===> you are creating the make file described  above using emacs editor

 $ make  -f  lab1.make   
    ===> you are "making" the program by creating lab1.out
 
 $ ./lab1.out  
    ===> you are executing your program, lab1.out

Here is the actual make file  lab1.make  described above 

Here are the steps to create a  makefile from the unix 
command line named "makefile":
  emacs makefile
     ===> you are creating the file, "makefile" described  above using emacs editor

 $ make  
     ===> you are "making" the program, unix uses the file, makefile, by default
	   
 $ ./lab1.out  
     ===> you are executing your program, lab1.out
 $ make clean  
     ===> you are "cleaning" your directory of unwanted files.

Here are some very simple examples of various makefiles:
make file example #1
make file example #2
make file example #3

For another explanation of makefiles see this : makefile tutorial.
For a more complete explanation see this tutorial.


Last revised: