06.08.2013 Views

Laboratory Exercises, C++ Programming

Laboratory Exercises, C++ Programming

Laboratory Exercises, C++ Programming

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

6 The GNU Compiler Collection and <strong>C++</strong><br />

It is important that you become used to reading and understanding the GCC error messages.<br />

The messages are sometimes long and may be difficult to understand, especially when the errors<br />

involve the standard STL classes (or any other complex template classes).<br />

3 Introduction to make, and a list example<br />

You have to type a lot in order to compile and link <strong>C++</strong> programs — the command lines are long,<br />

and it is easy to forget an option or two. You also have to remember to recompile all files that<br />

depend on a file that you have modified.<br />

There are tools that make it easier to compile and link, “build”, programs. These may be<br />

integrated development environments (Eclipse, Visual Studio, . . . ) or separate command line<br />

tools. In Unix, make is the most important tool. We will explain make in detail in lab 2. For now,<br />

you only have to know this:<br />

• make reads a Makefile when it is invoked,<br />

• the makefile contains a description of dependencies between files (which files that must be<br />

recompiled/relinked if a file is updated),<br />

• the makefile also contains a description of how to perform the compilation/linking.<br />

As an example, we take the program from assignment A3 (see below). There, two files (list.cc and<br />

ltest.cc) must be compiled and the program linked. Instead of typing the command lines, you<br />

just enter the command make. Make reads the makefile and executes the necessary commands.<br />

The makefile looks like this:<br />

# Define the compiler options<br />

CXXFLAGS = -pipe -O2 -Wall -W -ansi -pedantic-errors<br />

CXXFLAGS += -Wmissing-braces -Wparentheses -Wold-style-cast<br />

# Define what to do when make is executed without arguments.<br />

all: ltest<br />

# The following rule means "if ltest is older than ltest.o or list.o,<br />

# then link ltest".<br />

ltest: ltest.o list.o<br />

g++ -o ltest ltest.o list.o<br />

# Define the rules to create the object files.<br />

ltest.o: ltest.cc list.h<br />

g++ -c $(CXXFLAGS) ltest.cc<br />

list.o: list.cc list.h<br />

g++ -c $(CXXFLAGS) list.cc<br />

You may add rules for other programs to the makefile. All “action lines” must start with a tab,<br />

not eight spaces.<br />

A3. The class List describes a linked list of integer numbers. 1 The numbers are stored in<br />

nodes. A node has a pointer to the next node (0 in the last node). Before the data nodes<br />

there is an empty node, a “head”. An empty list contains only the head node. The only<br />

purpose of the head node is to make it easier to delete an element at the front of the list.<br />

1 In practice, you would never write your own list class. There are several alternatives in the standard library.

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

Saved successfully!

Ooh no, something went wrong!