Laboratory Exercises, C++ Programming
Laboratory Exercises, C++ Programming
Laboratory Exercises, C++ Programming
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
Tools for Practical <strong>C++</strong> Development 13<br />
makefile. Since ltest is the first target, make ltest and make are equivalent. By convention, the<br />
first target should be named all. Therefore, the first rules in the makefile should look like this:<br />
# Default target ‘all’, make everything<br />
all: ltest<br />
# Linking:<br />
ltest: ltest.o list.o<br />
$(CXX) -o $@ $^<br />
Makefiles can also contain directives that control the behavior of make. For example, a makefile<br />
can include other files with the include directive.<br />
A1. The file Makefile contains the makefile that has been used as an example. Experiment with<br />
make: copy the necessary source files (list.h, list.cc and ltest.cc) to the lab2 directory and<br />
run make. Run make again. Delete the executable program and run make again. Change<br />
one or more of the source files (it is sufficient to touch them) and see what happens. Run<br />
make ltest.o. Run make notarget. Read the manpage and try other options. Etc., etc.<br />
1.2 Phony targets<br />
Makefiles may contain targets that do not actually correspond to files. The all target in the<br />
previous section is an example. Now, suppose that a file all is created in the directory that<br />
contains the makefile. If that file is newer than the ltest file, a make invocation will do nothing<br />
but say make: Nothing to be done for ‘all’., which is not the desired behavior. The solution<br />
is to specify the target all as a phony target, like this:<br />
.PHONY: all<br />
Another common phony target is clean. Its purpose is to remove intermediate files, such as object<br />
files, and it has no prerequisites. It typically looks like this:<br />
.PHONY: clean<br />
clean:<br />
$(RM) $(OBJS)<br />
The variable RM defaults to rm -f, where the option -f tells rm not to warn about non-existent<br />
files. The return value from the command is always 0, so the clean target will always succeed.<br />
A cleaning rule that removes also executable files and libraries could be called, e.g., cleaner or<br />
realclean.<br />
All Unix source distributions contain one or more makefiles. A makefile should contain<br />
a phony target install, with all as its prerequisite. The make install command copies the<br />
programs and libraries built by the all rule to a system-wide location where they can be reached<br />
by other users. This location, which is called the installation prefix, is usually the directory<br />
/usr/local. Installation uses the GNU command install (or plain cp) to copy programs to<br />
$(PREFIX)/bin and libraries to $(PREFIX)/lib. Since only root has permission to write in<br />
/usr/local, you will have to use one of your own directories as prefix.<br />
A2. Create the bin and lib directories. Add all, clean and install targets to your makefile and<br />
specify suitable phony targets. Also provide an uninstall target that removes the files that<br />
were installed by the install target.