How to edit, compile, and execute a "hello-world" program with C++
This page describes how to edit, compile, and execute a simple C++
program that prints "hello world!" for the first-time users. It is
very simple (for me), and all you need to do is just follow the
following steps in the given order. Your program, which you are going
to write, will be stored in your own floppy disk. Have your disk
ready in the disk drive, and format your disk if necessary. Before
you start, you need to login to our Klamath server. If you don't know
how to login, read this page for more
help. In the example which follows, I assume your disk is formatted
and named "bullwinkle."
I. Launching C++
First, we launch THINK Project Manager (a Macintosh application
program designed for people who develop C++ programs) as follows.
- Open Software folder in Klamath Software.
- Open Symantec C++ folder.
- Open Symantec C++ for Macintosh folder.
- Double-click THINK Project Manager.
II. Creating your project
A project is a folder for your C++ program and anything you need
in order to run your program. Now, you will create a new project (which is
actually a folder) in your own floppy disk (not in the hard disk) as follows.
- After launching THINK Project Manager, a dialog box asks you to open
a project. Here click New button.
- Now a New Project dialog box asks you to select the type of project.
With the mouse, select C++ IOStreams Project, and click Create.
- Now a dialog box asks you to Name New Project. Use the mouse to choose
where
it will be stored by selecting your diskette. Name the project
Hello++
and click Save button. (Note: to indicate that this is a
project file, you can give it a ".pi" extension by typing "." followed
by typing option-p on the Mac keyboard. The project file would then
have the name Hello++.pi, with the actual greek char for pi displayed.
- A project folder (hello++ f) is automatically created on your disk,
and the project should be saved in that folder. The folder also
contains a C++ program file, main.cp.
- A project window (hello++.pi) is displayed on your screen. If the
project window gets hidden by another window, pull down the Windows
menu and select Hello++.pi.
In general, each time you create a new C++ program, you will begin by
creating a new project. As a result, a new folder is automatically created
on your diskette, and that folder contains the project and a program file,
main.cp. It is possible to reuse an earlier project but,
until you learn the system, just begin each new program by creating a new
project on your datadisk.
III. Creating the source file
Now, you are ready to write your C++ program. Begin by opening
bullwinkle::hello++ f::main.cp.
- Choose Open from the File menu, and select
bullwinkle::hello++ f::main.cp. An editor window opens, and it
contains a C++ main function. (A faster way to do this: double-click
on main.cp in the project window.)
- Add this C++ comment as the first line in the file:
//FILE: hello++.cp
- Next add this line as the first statement in the body of
function main: cout << "Hi world!";
Note that the file main.cp is listed in the project window (if
the project window is not visible, choose
hello++.pi from the Windows menu).
For this tutorial, if your project lists ANY OTHER .cp files other than
main.cp,
remove them as follows: 1) select the filename in the project window;
2) choose the Source Remove command.
IV. Adding the libraries [Optional Step]
If your selected the project type C++ IOStreams Project, you do not have
to add any libraries. You may skip this step (go to Compiling the
Source File, below).
If you selected the project type C++ Project, then you must add
a library to your project. Do the following:
Libraries
contain routines that your program calls at run time, e.g., the C++
output operator << is in the IOStreams run-time library.
The CPlusLib
library is required for all C++ projects that produce applications. (The
ANSI++ library is required only for non-Macintosh applications.)
These two libraries are already listed in your project window
(CplusLib is in Segment 2, and Ansi++ is in the Ansii++ segment. If these
libraries are somehow absent from your project, you will have to add them;
see the instructions which follow.)
The IOStream library contains all the standard Input/Output
routines, and it must now be added to your project.
- Choose Add Files from the Source menu.
- Open the folder called Standard Libraries in the Symantec C++
for Macintosh folder in the dialog box. (To locate these folders,
you may have to start from the top-level Desktop and open the Klamath
Software folder, etc.)
- Select IOStreams and click the Add button. The IOStreams
library is added to your project window. Now click DONE.
(If necessary, these steps may be repeated for
CPlusLib and ANSI++
libraries, but they should already be in your project automatically.)
If you added another library accidentally, you may delete it from your
project by selecting it (by clicking) and choosing the Source Remove command.
V. Compiling the source file
Now, you are ready to compile your source file. Choose Compile from
the Source menu.
If a dialog box appears asking whether you want to Update or not,
click Update.
As your program compiles (i.e., is translated from C++ into machine
language), a Progess dialog box shows how many lines have been
compiled. Errors may appear if you have any. If you find errors, go back
to Step III to fix your source program and repeat Steps IV and V.
VI. Running the project
Finally, everything is set to run your program. The source file is in the
project window along with the libraries you selected.
- Choose Run from the Project menu.
- A dialog box may appear asking if you want to bring the project up to
date. If you want to update, click Update. Doing so reloads
your source file and the libraries you selected.
- A console window appears on the screen with the output "hello world!"
Again, errors may appear if you have any. If you find errors, go back
to Step III to fix your source program and repeat Steps IV, V, and VI.
VII. The Edit/Compile/Execute Cycle.
This tutorial is an example of the standard software development cycle used
by programmers to turn an algorithm in to a working computer program:
Edit/Compile/and Execute.
Typically, you will solve a problem in CIS 210 by writing and testing
a solution on paper (i.e., the algorithm). When you're sure it's correct,
you then Edit the program (i.e., type it in, build the project, etc.),
Compile, and Execute.
The tutorial is a demonstration of the Edit/Compile/Execute cycle you
will follow for each new programming assignment in 210.
Good luck and have fun!
Takunari Miyazaki
Office: 230 Deschutes Hall
Phone: (503) 346-1383
E-mail:
miyazaki@cs.uoregon.edu
Michael Hennessy
E-mail:
michaelh@cs.uoregon.edu
10/2/95