Learn the basics of the UNIX make utility
Make's job is to create filesMulti-file make
example:
suppose you have a file hello.cpp and you want to create the executable a.outSteps for using make
you would type the command $ g++ hello.cpp
and g++ would generate a.out for you
instead of typing the command $ g++ hello.cpp every time you want to compile hello.cpp, you could use the make utility
Advantages of make
- Create a file called makefile or Makefile (most people use Makefile so it appears first in directory using ls)
- Using an editor (such as pico) put the rules for creating the files you want to create in the makefile
- Run the make utility by typing make at the UNIX command prompt
Anatomy of a makefile:These are surprisingly big advantages.
- you only have to type the commands once
- make will only compile the files that have changed since the last time you compiled (this is really important in large projects where complete compilation may take several hours).
this is a simple and complete makefile:
NOTE: for many versions of make, the second line must start with a <tab> so it is a good habit to use a <tab>a.out: hello.cpp
g++ hello.cpp
it contains a single rule that you can read like this:
"when the user asks to make the file a.out, only do it if the current a.out is older than hello.cpp and then make it using the command <g++ hello.cpp>"
Format of rules in makefiles:
target_file : dependency_files
<tab> command to build target_file
in C++ it is common to put each class in its own files, name.h for the class header, name.cpp for the class's functionsDefault Rules
the next example contains three files:
main.cpp the file containing the main() function
sentence.h the file containing the definition of class Sentence
sentence.cpp the file containing the source code for class Sentence
the most efficient way to compile such program is to compile each component into an object file (.o) and then combine the object files into an executable
g++ -c main.cpp will create the object file main.o
g++ -c sentence.cpp will create the object file sentence.o
g++ -o sentence main.o sentence.o will create the executable sentence
# simple makefile
# This rule tells make how to "make" the executable sentence
sentence: main.o sentence.o
g++ -g -o sentence main.o sentence.o
# This rule tells make how to "make" the object file main.o
main.o: main.cpp sentence.h
g++ -c main.cpp
# This rule tells make how to "make" the object file sentence.o
sentence.o: sentence.cpp sentence.h
g++ -c sentence.cpp
# This rule tells make what to delete when the user type "make clean"
# BE VERY CAREFUL to only put generated files here
clean:
rm -f main.o sentence.o sentence
create a lab3 directory (use the mkdir command)
cd to your lab3 directory and type the command (don't forget the "." at the end)
$ cp -r ~tyson/112/src/lab3/* .
make sure you have two subdirectories: hello and sentence, if not ask for help
These files are also available on the web: src/lab3
cd to lab3/hello
type the command
$ make
What happened?
type the command agian
$ make
What happened?
look at the dates on the files a.out and hello.cpp
$ ls -l
when a.out is newer than hello.cpp, make does not try to recreate it
change the date on hello.cpp using the touch command
$ touch hello.cpp
touch just updates the last date changed to the current time
now look at the dates again
$ ls -l
what do you think will happen when you type make ?
type
$ make
what happened? why?
now edit the file hello.cpp and make some change (such as change the text that is printed)
make sure you save the file
now type
$ make
go to the lab3/sentence directory
$ cd ../sentence
the ".." means the parent directory (the parent of the current directory)
type
$ make
type
$ touch sentence.h
type
$ make
which files were recompiled?
look at the makefile and try to figure out why these files were recompiled when sentence.h was changed
this is an important aspect of make, ask if you don't understand why
Create a makefile for your second programming assignment Program 2
First create a directory for your next assignment, then cd to that directory.
You will need to create some empty files so your Makefile will work. You can create empty files using the touch command:
$ touch movie_db.h movie_db.cpp main.cpp
Later you can come back and fill in these files with the C++ code using an editor (such as pico or scite)
copy the Makefile from the previous example into this directory so you can use it as a starting point
$ cp ~tyson/112/src/lab3/sentence/Makefile .
Edit the makefile so it will work for your files.
Your empty program will "compile" if you put an empty main() function in main.cpp.
Use an editor to edit main.cpp and add the following text
int main()
{
}
Test your makefile by executing the make command at the UNIX command prompt while in your program 2 directory