Building Executables with "make"

You can use the Unix utility called make to help you create an executable file from several C++ files. There are many other uses for make, but this lab focuses on how to use it to help simplify working with C++ files. Here is a list of references for using make: When you type make at the Unix/Linux command prompt, it looks for a file called Makefile or makefile which you would have created with a text editor. Inside the Makefile you have to put in the names of the C++ and object files that you need to create the executable file for the C++ project you are working with.

Let's look at an example of how you would create a C++ project without a Makefile. We can then see how to make life simpler by using a Makefile to create that same C++ project.

We will work with files that you may have seen before. If you don't have them, you can copy them now:

cp /net/data/ftp/pub/class/170/ftp/cpp/SeparateCompile/main.cpp main.cpp
cp /net/data/ftp/pub/class/170/ftp/cpp/SeparateCompile/myFunction.cpp myFunction.cpp
cp /net/data/ftp/pub/class/170/ftp/cpp/SeparateCompile/myFunction.h myFunction.h
To obtain an executable, you must type: Now every time you make a change to either main or myFunction, you have to recompile the file that you modified and then perform the link again. And if you make a change to the .h file that all of those .cpp files depended upon, you have to recompile everything and do the link again. Of course you can just do this: but that is time-consuming and not at all necessary. It is much better to only compile the files you need to. This is where a Makefile can help you.

You have to be observe a little syntax when you create your Makefile with a text editor. Here are a few very simple rules:

This is a simple introduction to Makefiles. As you become comfortable with them you will find that you can elaborate with targets to clean up old .o files and so on. However, to start, here is a simple Makefile to deal with the myFunction project.
# Makefile to create the basic myFunction project

main.o:  main.cpp myFunction.h
        g++ -c main.cpp

myFunction.o: myFunction.cpp myFunction.h 
        g++ -c myFunction.cpp

main: myFunction.o main.o
        g++ myFunction.o main.o -o main
Now that you understand what is in a Makefile you need to know how to use it. It's quite straightforward - simply enter make target at the system prompt. The make process will report on what it is doing. The following examples illustrate how to compile a single module, and how to link all files.
[26]% make myFunction.o
        g++ -c myFunction.cpp

[27]% make main
        g++ -c main.cpp
        g++ myFunction.o main.o -o main
If you enter a target that is already current, "make" reports that.
[22]% make main.o
main.o is up to date.
The bottom line here is that Makefiles help you work with multiple C++ files that make up a project. By only compiling the C++ files that you have to compile, you save a lot of time recompiling files needlessly. For greater detail on Makefiles, go back to any of the tutorials given at the start of this explanation.