Laboratory Exercises, C++ Programming
Laboratory Exercises, C++ Programming
Laboratory Exercises, C++ Programming
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
12 Tools for Practical <strong>C++</strong> Development<br />
ltest.o: ltest.cc list.h<br />
On the line following the dependency specification you write a shell command that generates the<br />
target. This is called an explicit rule. Example (with a short command line without options):<br />
ltest.o: ltest.cc list.h<br />
g++ -c ltest.cc<br />
Make has a very unusual syntax requirement: the shell command must be preceded by a tab<br />
character; spaces are not accepted.<br />
An implicit rule does not give an explicit command to generate the target. Instead, it relies<br />
on make’s large collection of default rules. For instance, make knows how to generate .o-files<br />
from most kinds of source files, for instance that g++ is used to generate .o-files from .cc-files.<br />
The actual implicit rule is:<br />
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $<<br />
CXX, CPPFLAGS, and CXXFLAGS are variables that the user can define. The syntax $(VARIABLE) is<br />
used to evaluate a variable, returning its value. CXX is the name of the <strong>C++</strong> compiler, CPPFLAGS<br />
are the options to the preprocessor, CXXFLAGS are the options to the compiler. $@ expands to the<br />
name of the target, $< expands to the first of the prerequisites.<br />
We are now ready to write our first makefile, which will build the ltest program from lab 1.<br />
The value g++ for CXX is default, thus that definition is optional. CPPFLAGS and CXXFLAGS are<br />
empty by default. CXXFLAGS is almost always redefined.<br />
# Compiler and compiler options:<br />
CXX = g++<br />
CXXFLAGS = -pipe -O2 -Wall -W -ansi -pedantic-errors<br />
CXXFLAGS += -Wmissing-braces -Wparentheses -Wold-style-cast<br />
# Linking:<br />
ltest: ltest.o list.o<br />
$(CXX) -o $@ $^<br />
# Dependencies, the implicit rule .cc => .o is used:<br />
ltest.o: ltest.cc list.h<br />
list.o: list.cc list.h<br />
$^ expands to the complete list of prerequisites. We will make improvements to this makefile<br />
later.<br />
Suppose that none of the files ltest, ltest.o, list.o exists. Then, the following commands are<br />
executed when you run make (the long command lines have been wrapped):<br />
make ltest<br />
g++ -pipe -O2 -Wall -W -ansi -pedantic-errors -Wmissing-braces -Wparentheses<br />
-Wold-style-cast -c -o ltest.o ltest.cc<br />
g++ -pipe -O2 -Wall -W -ansi -pedantic-errors -Wmissing-braces -Wparentheses<br />
-Wold-style-cast -c -o list.o list.cc<br />
g++ -o ltest ltest.o list.o<br />
If an error occurs in one of the commands, make will be aborted. If there, for instance, is an error<br />
in list.cc, the compilation of that file will be aborted and the program will not be linked. When<br />
you have corrected the error and run make again, it will discover that ltest.o is up to date and<br />
only remake list.o and ltest.<br />
If you run make without a target name as parameter, make builds the first target it finds in the