Programming tools

Back Up Next

horizontal rule

Home
Chapter 1
Chapter 2
Chapter 3
The Shell
Parameters and Variables
The Bourne Again Shell
Grep and AWK
Control Structures
Shell Program examples
TC Shell
Programming tools
Networking and the Internet

How to edit, compile, and run your
 c++ program.

An object-oriented version of C created by Bjarne Stroustrup. 
C++ has become popular because it combines traditional 
C programming with OOP capability
.

g++ - GNU project C++ Compiler

To obtain extra help type:

$ man g++

To establish that your system has the correct C++ compiler 
installed, at the command prompt type this:

            $ g++ -v

1.    Open vi text editor associated with a file name, remember
 to use the extension .cpp

       $ vi programname.cpp (program.cpp will be the
 name of your program. After 
you type your program save it. 

2.    At the shell prompt type:

       $ g++ programname.cpp



to compile your program. If there are error messages generated 
by the compiler such as syntax errors on your program, go back 
and reopen the file. Save the file and re-compile it.  If successful 
it will create an executable file called a.out

3.    Run your program by typing the full path for

       $ ~/a.out          Note: if you do not wish by 
default create the file a.out, you can 
tell the compiler to create a name that 
you specify for the executable file by 
using the command:

          $ g++ programname.cpp �o desiredname


How to compile and execute C++ files on HP

%CC filename.cpp (compiling)

%a.out (executing)

If you want to compile more C++ files, you can do

%CC filename1.cpp  filename2.cpp(compiling)

%a.out (executing)

 

How to compile and execute C++ files on IBM-UNIX

%xlC filename.C (compiling)

%a.out (executing)

where, C++ file extension name on IBM is .C file.

If you want to compile more C++ files, you can do

%xlC filename1.C  filename2.C (compiling)

%a.out (executing)

 

If your system is set up correctly, this command will launch the 
compiler executable and print its version. If an error message 
is printed instead, you will have to consult your documentation 
to make sure the compiler is installed and set up correctly.

Create a file named hello.cpp with this content:

#include <iostream>

 using namespace std;

 int main()

{
	cout << "Hello World!, this is my 
	first C++ program" << endl;

  	return 0;
}

 

Using Makefiles

make is useful when maintaining or updating groups of 
programs or other kinds of files. If a program consists of 
more than one module then
make will be useful. make uses 
a makefile which lists the dependencies of a final file upon 
other files. To make a new version of the final file (or target) 

make
will create or update only those files that have changed 
since the last version of the target was made.  The makefile 
must be in the directory with the program.

Creating a makefile

1.    Using a text editor such as vi, create and open a file called 
makefile
.

2.    For every two files that must be linked together, you must 
create an object file. The object file is an intermediate 
compiled source file. Now, to compile and create an object 
file from two source files (for example, a C/C++ header file 
and a C/C++ source file),

o        Specify the target. The target is the object file, 
which must have a .o extension.

o        Use the cc command with the -c flag as described 
above to compile the two files together.

The format in which this must be typed is:
target: files
cc source_files -c

3.    Once you've created the object files,

o        Compile and link them together with the cc command 
with the -o flag as described above.

o        Specify the name of the target (default is a.out). 
The target is the executable file.

The format for this is much the same:
target: files
cc source_files -o target

4.    The catch is that these parts must be typed into the makefile 
in reverse order: starting at the top with the executable as the
 target, with the object files as targets on the lines below.

5.    Save the file as makefile and exit.

6.    To compile and link the files, and create an executable, 
simply type make at the command prompt.

Example of a makefile

Let us consider an example where we have three files:

bullet

class.h; a header file containing a class definition.

bullet

class.C; a source file containing the implementation of the 
class. This file is linked to class.h with an include statement.

bullet

file.C; the main program. This file is linked to file.C with an 
include statement as well.

The contents of the makefile should be:

executable:  class.h class.o file.o


     cc class.o file.o -o list

class.o:  class.h class.C


     cc -c class.C

file.o:  class.h file.C


     cc -c file.C

The process starts from the bottom up.

1.    The object file file.o is created by linking the source 
file file.C with the header file class.h. The source file is 
compiled with the -c flag, which allows the object file to 
be created. The header file is not compiled be cause it does 
not contain source code.

2.    The object file class.o is created by linking the source 
file class.C with the header file class.h. The source file 
is again compiled with the -c flag, creating the object file. 
The header file is not compiled.

3.    The executable file executable is created by linking the 
object files class.o and file.o with the header file class.h
The object files are compiled with the -o flag, which allows
 you to specify the name of the output file.

 

 

 

horizontal rule

Back to CS140U Homepage
This page was last modified September 26, 2004
wmorales@pcc.edu