The C++ Programming Language

C++ is an object oriented language.  This language has evolved from C,  and all C++ compilers should successfully compile C programs.  There is an ISO standard for C++, but you will find situations where a compiler is not fully compliant.  Also, the standard does change.

On the Sun Unix machines there are at this time two compilers, the Sun compiler CC and the Gnu compiler g++.  Typically the Gnu compiler is also installed on Linux machines.  The Borland Designer C++ compiler is available on the ThinkPads.

The transformation of a C++ program into an executing routine involves three major steps:  compiling, linking and loading.  When the source program a.cc  with header file a.h is compiled, the object file a.o is produced.    The functions in a.o can be combined with other library functions to produce a program a.out.  The program a.out can be run after it is loaded in memory.  In each of these three steps errors can occur.

In the simplest case, the compiling and linking steps can be combined in one command.  For example, with the Sun Unix compiler, the command is

CC a.cc

to create the executable program a.out.


The Sun Compiler CC

CC is the proprietary C++ compiler available on the Sun Unix machines.  (Please notice the compiler is CC, not cc.)   Often both source programs and header files are used during compilation.  During linking, the user and system object files are combined.  Paths must be set appropriately to find compilers, header files and libraries.   The .cshrc file provided with your account should set the environmental variables appropriately.

Part of the current standard, ISO IS 14882:1998, is the specification for a standard template class library.  On our Sun/Unix network, this library is referred to as the Rogue Wave template library and is only available for compatibility level 5 (the default).

The paths relevant to CC are listed below.  These are either set in your .cshrc file or given in the compile/link commands (which may also be done by means of a makefile, described below).
 
Description Path Usage
Path to compiler CC /opt/SUNWspro/bin/CC /opt/SUNWspro/bin must be part of environment variable PATH
Path to standard include library /opt/SUNWspro/SC5.0/
     /include/CC
Path to standard object library /opt/SUNWspro/lib /opt/SUNWspro/lib must be part of environmental variable LD_LIBRARY_PATH
Path to Rogue Wave standard 
template include file
/opt/SUNWspro/SC5.0/
    /include//CC/rw7
Either CCFLAGS = -I/opt/SUNWspro/SC5.0/include/CC/rw7
and #include <xxx.h>
or #include <rw7/xxx>  for RW include file xxx.h
Path to Rogue Wave standard 
template libraries
/opt/SUNWspro/lib/rw7 LDFLAGS = -lxxx for RW function xxx, e.g. -lrwtools

When CC is run, it also produces a subdirectory named SunWS_cache. This subdirectory contains debugging information and other files.  SunWS_cache is also used when running Sun Workshop, and integrated development environment for C++ programming on the Sun workstations.

When you use template classes, a second subdirectory is created, template_DB.   This subdirectory is a cache for compiled template functions.

It may become necessary to run CCadmin to clean up these two subdirectories.  Consult the manual pages online for more information.  You can do this by typing

man CCadmin

at the prompt line.

Compiling Using Makefiles

Most often you will compile your C++ program in the context of a makefile.  Makefiles are described at http://www.cs.wfu.edu/~*****.  Information about their use with templates classes is given below.

Macros can be used to facilitate the makefile construction.  A macro is a command, environment variable, or other component of a makefile that is defined with a symbolic name, usually at the beginning of the makefile.  Then, everywhere that word appears in the makefile, it is substituted by the value you gave it.  This makes it easy for you to change a word or phrase that is repeated several places in the makefile, and to see in a glance what compiler, libraries, and compiler options you are using for a particular program.  You can also often reuse an old makefile and edit it for a new program simply by changing the macros defined at the top.

By convention, macros are defined all in capital letters.  There are some macro names that are predefined for the system, and have a predefined default value.  You can override the default values with a new definition, however.
 

MACRO NAME DEFAULT VALUE DESCRIPTION
CCC CC name of compiler to use
CCFLAGS switches for compiler, e.g. -g
COMPILE.cc $(CCC) $(CCFLAGS) $(CPPFLAGS) -c compile only (do not link)
LINK.cc $(CCC) $(CCFLAGS) $(CPPFLAGS) $(LDFLAGS) link object files
LDFLAGS switches for linker
RM /bin/rm -f remove without prompting the user for confirmation

Here is a makefile that is based on a user-written template class.   The program is built with the Unix command

make maze
 

#  local macros, note that compatibility level 5 standards
#  are adhered to and the standard template library is being
#  used
CCFLAGS = -g -verbose=template    # <<- -verbose switch shows template compiling and linking
LDFLAGS =                                        # <<- -no special libraries needed for this program

#  target to compile  using default  rule and explicitly link
maze:   IntPoint.o maze.o utilities.o
        $(LINK.cc) -o maze IntPoint.o maze.o utilities.o

#target clean -- remove *~ files
clean:
        $(RM) *.o
        $(RM) *~

# target clean -- remove *.o files, compiled templates,
# program and then clean
cleanall:       clean
        CCadmin -clean        #  <<- CCadmin -clean is used to reset the template database
        $(RM) maze

# dependencies
IntPoint.o: IntPoint.h makefile
maze.o: maze.h makefile
utilities.o: maze.h Stack.cc makefile
maze.h: IntPoint.h Pair.h Stack.h makefile

Below is the source program maze.cc .
 

// Program to traverse a maze using a stack
// It also prints out the path through the maze

#include "maze.h"

using namespace std;           // <<- namespace is required

int main (int argc, char *argv[])
{
.
.
.

  return 0;
}
 

Here is the user header file,  maze.h :
 


#ifndef MAZE_H      //  <<- make sure header not included twice
#define MAZE_H "@(#)maze.h      1.104/11/00"

#include <iostream>  // <<- standard headers do NOT have .h suffix
#include <iomanip>

#include "IntPoint.h"   //<<- user headers do have .h suffix
#include "Pair.h"
#include "Stack.h"

using namespace std;  // <<- namespace is required

void readmaze(char * fname, char maze[100][100],
              IntPoint & start, IntPoint & size);

void traversemaze(char maze[100][100], const IntPoint start,
                  const IntPoint size);

void dumpmaze(char maze[100][100], IntPoint size);

void printPath(Stack<IntPoint> &  Path);  // <<- template parameter
 

#endif


Sun CC Compiler with Rogue Wave Standard Template Library

Part of the Sun C++ package is the Standard Template Library (STL).   STL is a library of reusable components, divided into containers (standard data structures like stack and queues), iterators, and algorithms.  A full description can be found in Deitel and Deitel's C++:  How to Program and in Horton's ******.  There is also an introduction at http://www.borland.com/borlandcpp/news/report/CR9410vilot.html.

The Standard Template Library for our Sun system is implemented by Rogue Wave.  (In fact, Rogue Wave is the company that produces STL for most systems.)  For a complete description of the contents of this package please see the Sun Workshop Compiler Answerbook on one of the Sun workstations in Calloway 6.  It is most important to realize that the Rogue Wave library can only be used with compatibility level 5 (default).

There is a compiler switch, -library=iostream,rwtool7, that allows for efficient use of the Rogue Wave Standard Template library.  This switch takes care of all compile and link paths.

Below is a makefile that compiles and links a program which uses the RW STL.
 
 

#  local macros, note that compatibility level 5 standards
#  are adhered to and the standard template library is being
#  used
CCFLAGS = -g -library=iostream,rwtools7   #<<-- -library switch included
LDFLAGS =

#  The  cc.o. rule (pronounced "c++ to oh" rule) is
#  being used implicitly -- i.e., it is assumed that xxx.o is dependent on xxx.cc. 
# The linker is explicitly called $(link.cc)
#  to force the inclusion of $(CCFLAGS).

#  target store
store:  store.o storeutilities.o
        $(LINK.cc) -o store store.o storeutilities.o

#target clean -- remove *~ files
clean:
        $(RM) *~

# target clean -- remove *.o files, the executable program, and then clean
cleanall:       clean
        $(RM) *.o
        $(RM) store

Here is a short version of the header file, store.h:
 

#ifndef STORE_H

#include <iostream>
#include <rw/cstring.h>   //<<-- Rogue Wave header file

struct MyStoreData
{
  RWCString Name;
  int StockNumber;
  double Price;
  int Quantity;
  struct MyStoreData *Next;
};

#include "storeutilities.h"

#endif


Gnu C++

This compiler is available on the Sun Unix systems.  It is found at /usr/local/bin/g++.  For more information please examine the g++ online manual page (man g++).

If you wish to use class templates, we recommend that you use CC instead of g++.


Borland Designer C++

On the MicroSoft Windows based machines the Borland Designer C++ is available for use.  It is an integrated environment for the development, debugging and testing of C++ programs, especially those that use the MicroSoft windowing system.  This integrated system is based on the notion of a project, .bpr suffix.  Source code and header files all belong to a project.  Basically the project provides the structure from which a makefile is automatically constructed to guide compiling and linking.

To create a new program named myprogram.cpp the following steps generally are followed:

To compile the source code in the project, Project->Make myprogram.  If there are errors, you will get error messages.

Once your source program is error free, the program can be run, Run->Run.  In the Run menu there are a number of helpful debugging aids.

It is rather important that the C++ source code contain the following includes:

Note that if you wish to port your program to a Unix platform, you will need to remove the above include statements.

By and large, Borland's C++ Builder is a very nice system in which to develop, debug and test code.