06.08.2013 Views

Laboratory Exercises, C++ Programming

Laboratory Exercises, C++ Programming

Laboratory Exercises, C++ Programming

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Tools for Practical <strong>C++</strong> Development 11<br />

2 Tools for Practical <strong>C++</strong> Development<br />

Objective: to introduce a set of tools which are often used to facilitate <strong>C++</strong> development in a Unix<br />

environment. The make tool (GNU Make) is used for compilation and linking, gdb (the GNU<br />

debugger) for controlled execution and testing, valgrind (not GNU) for finding memory-related<br />

errors.<br />

Read:<br />

• GNU Make, http://www.gnu.org/software/make/manual/<br />

• GDB User Manual, http://www.gnu.org/software/gdb/documentation/<br />

• valgrind Quick Start, http://www.valgrind.org/docs/manual/QuickStart.html<br />

• Manpages for the tools used in the lab.<br />

The manuals have introductory tutorials that are recommended as starting points. The manuals<br />

are very well written and you should consult them if you want to learn more than what is treated<br />

during the lab.<br />

1 GNU Make<br />

1.1 Introduction<br />

As you saw in lab 1, make is a good tool — it sees to it that only files that have been modified<br />

are compiled (and files that depend on modified files). To do this, it compares the modification<br />

times 5 of source files and object files.<br />

We will use the GNU version of make. There are other make programs, which are almost but<br />

not wholly compatible to GNU make. In a Linux or Darwin system, the command make usually<br />

refers to GNU make. You can check what variant you have with the command make --version.<br />

It should give output like the following:<br />

make --version<br />

GNU Make 3.81<br />

Copyright (C) 2006 Free Software Foundation, Inc.<br />

This is free software; see the source for copying conditions.<br />

...<br />

If make turns out to be something else than GNU make, try the command gmake instead of make;<br />

this should refer to GNU make.<br />

Make uses a description of the source project in order to figure out how to generate executables.<br />

The description is written in a file, usually called Makefile. When make is executed, it reads<br />

the makefile and executes the compilation and linking (and maybe other) commands that are<br />

necessary to build the project.<br />

The most important construct in a makefile is a rule. A rule specifies how a file (the target),<br />

which is to be generated, depends on other files (the prerequisites). For instance, the following<br />

rule specifies that the file ltest.o depends on the files ltest.cc and ltest.h:<br />

5 In a network environment with a distributed file system, there may occur problems with unsynchronized clocks<br />

between a client and the file server. Make will report errors like:<br />

make: ***Warning: File "foo" has modification time in the future.<br />

make: warning: Clock skew detected. Your build may be incomplete.<br />

Unfortunately, there is not much you can do about this problem, if you don’t have root privileges on the computer.<br />

Inform the system administrators about the problem and switch to another computer if possible. If no other computer is<br />

available, temporarily work on a local file system where you have write access, such as /tmp.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!