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.hTo obtain an executable, you must type:
You have to be observe a little syntax when you create your Makefile with a text editor. Here are a few very simple rules:
main.o: main.cpp myFunction.h
g++ -c main.cpp
^-- there would be a tab character there before the command
# 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 mainNow 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.
%If you enter a target that is already current, "make" reports that.
make myFunction.og++ -c myFunction.cpp %
make maing++ -c main.cpp g++ myFunction.o main.o -o main %
%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.
make main.omain.o is up to date.