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.
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.
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
#target clean -- remove *~ files
# target clean -- remove *.o files, compiled templates,
# dependencies
|
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 "IntPoint.h" //<<-
user headers do have .h suffix
using namespace std; // <<- namespace is required void readmaze(char * fname, char maze[100][100],
void traversemaze(char maze[100][100], const IntPoint start,
void dumpmaze(char maze[100][100], IntPoint size); void printPath(Stack<IntPoint> & Path); //
<<-
template parameter
#endif |
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
# target store
#target clean -- remove *~ files
# target clean -- remove *.o files, the executable program, and then
clean
|
Here is a short version of the header file, store.h:
| #ifndef STORE_H
#include <iostream>
struct MyStoreData
#include "storeutilities.h" #endif |
If you wish to use class templates, we recommend that you use CC instead of g++.
To create a new program named myprogram.cpp the following steps generally are followed:
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:
By and large, Borland's C++ Builder is a very nice system in which to develop, debug and test code.