CPS 108: Getting Started
Spring 1999
Due before your next class meeting
This document will show how to:
-
Login to and logout from the Sun Ultra-SPARCstations found on campus
-
Use basic UNIX tools, including:
-
Copying, moving, deleting files
-
Traversing and moving between directories
-
Invoking programs including editors and compilers
-
Edit, compile, debug, and run a C++ program
-
Use Netscape to access web pages including the page for the course
This lab comes in several parts. You will login, reconfigure certain files
to facilitate working in CPS 108, practice some UNIX commands, write a
simple C++ program, and use netscape. This document is only a brief
introduction to many tools. You will need to spend some time on your
own experimenting further to become comfortable with the tools. You should
consult the OIT
online help page for more information.
In this document, commands that you type will be in a font that
looks like this, sometimes commands may be quoted --- "like this"!
--- do not type the quotes, just type what is between them. When
instructed to type something, press the return key after typing it.
If you view this lab using a web browser like Netscape you will be able
to jump between sections by clicking on highlighted words, i.e.,
links. An explanation about how to use netscape is given below.
The pieces of this document include:
Table of Contents
[ Login | X windows
| UNIX | Directories | Programming
| CVS | Logout ]
Logging in
Log in to a workstation. If you have problems logging in ask a TA for help.
(You may need to go to the OIT Help desk in 101 North Building, although
engineering students may need to check with the engineering school office
or the dean's office.) To log in:
-
Find a free workstation.
Workstations are always on, do NOT turn them off. Wiggle the
mouse if the screen is blank.
-
Type your login-id, hit return, type your password (your password is not
displayed).
Note: passwords are case sensitive, Banana is different from banana.
-
Wait until two windows labeled xterm appear, this may happen quickly
or take a while.
Note, sometimes a message of the day window (MOTD) pops up (this window
is labeled with xmessage at the top) with important information. If this
window has appeared, then you can click the left mouse button on
the bar that says "click here with the Left Mouse Button to continue" and
this window will disappear. When this message does appear, you should read
it as it usually contains important announcements.
Managing Windows in X
The two rectangles labeled xterm are called windows. The
row of small rectangles containing pictures in the top left position of
the screen is a menubar which contains some icons. Icons
represent windows that are temporarily hidden. Click the mouse (left button)
on the picture of the calculator (also labeled xcalc) and wait until a
calculator appears on the screen.
-
The green with black dots covering most of the screen is called the background.
Move the mouse to any area of the background and try clicking the three
mouse buttons each at a different time. Each button brings up a different
menu of applications. Do not try any of these now, you can try them
on your own later.
-
Most windows have title bars across the top of the window. Move the mouse
to the title bar for the calculator window. Click and hold the left
mouse button on the word Calculator. Move the mouse and the window should
follow. Let go and the window will be in its new position. (Note: You can
also move windows using the Window Ops commands that appear when
you click and hold the left mouse button when the mouse points to the background.)
-
Another useful window command is resizing the window. Move the mouse cursor
to the Calculator title bar and click and hold the right mouse button.
An outline of the window appears. Move the mouse outside of the window
boundary and you will see the outline grow (and shrink if you move in the
other direction). When you release the mouse button, the Calculator window
is a different size than before. (Note: This command can also be found
in the Window Ops menu.)
-
To get rid of windows you no longer want, click once on the kill
icon along the top of the screen. The cursor becomes a skull and cross
bones. Move the mouse to any position inside the Calculator window and
click. The window should disappear.
Getting Started with UNIX
An operating system is a large program that coordinates all the resources
for the execution of programs. UNIX is the operating system on the Sun
Ultra-SPARCstations. UNIX contains lots of commands, a few important ones
are introduced here. To enter a command, type after the prompt in one of
the xterm windows. You should see a prompt that may look similar to
[1] rcduvall@teer8%
except rcduvall should be replaced by your login-id and the number
may be different. If you make a mistake when typing a UNIX command, use
the delete key or the arrow keys to correct it.
To make the font bigger and easier to read you can hold
the control key down while pressing and holding the right mouse
button while the cursor is in an xterm window. Choose large for an easier-to-read
window.
If this is a new account, you should change your password to something
that is easier to remember, but not easy for someone to guess. Do not use
your name, or an English word, but rather compose a password formed with
numbers, upper and lower case letters, and punctuation marks. To change
your password, type the UNIX command passwd and follow the
directions it prints.
Next, from within an xterm window, at the prompt type ~rcduvall/cps108/bin/setup.
This runs a program that will set up some files in your directory to make
it easier to use the programming tools we provide. Additionally, it creates
a directory in your account, called cps108, for you to put
your CPS 108 work. Any files changed by this program are saved with a .old
extension, e.g., .cshrc.old. If you do not get a prompt back
after typing the command, consult a TA. If you do not want to run
this program because you have made your own changes to your accounts setup,
please consult this page for more
information on exactly what this command does.
In order for changes made by the setup command to take effect, you will
need to logout and then log back in. To logout of your Sun workstation,
click the logout icon at the top left of the window. Now, please
log out, then log back in.
Creating and Traversing Directories
To do the work for the remainder of this lab, you will need to create a
directory (i.e., folder) to contain the files for your program.
In this course, we will follow a convention of creating a separate directory
for each project within your cps108 directory.
When you login, your session begins in your main directory. To see the
name of your directory, type the UNIX command pwd (print working
directory). The complete name it prints in your terminal window should
look similar to /afs/acpub.duke.edu/users/r/c/rcduvall except your
login-id should be the rightmost word.
To create a directory called bowling for storing new files related
to this project, you will need to follow these steps:
-
Change into your cps108 directory by typing cd cps108. This
command should not produce any output, but should move you from your home
directory to the cps108 directory within your home directory.
You can verify this by typing pwd again.
-
Create a new directory called bowling by typing mkdir bowling
-
To list your files and directories, type ls (think of this as
shorthand "list"). You should see the directory you just created printed.
-
Change into your bowling directory by typing cd bowling
You are now ready to start working on a C++ program. The command to change
directories, cd, lets you change directories relative to your
current directory or absolutely to a specific directory. For example you
can type cd ~rcduvall to change into Professor Duvall's directory.
Type ls to see the files and directories contained there.
At any time, you can type cd (with no arguments) to return
to your home directory. Now type cd cps108/bowling to return
to your new project directory. Now when you type ls you
will see there are no files in the directory, because you have not created
any yet. Instructions for editing, compiling, and printing are given
below. First, you will need to copy some files to your bowling
directory. The copy command is cp so from within your bowling
directory type (do not forget the trailing dot: "."):
cp ~rcduvall/cps108/code/bowling/* .
Type ls to see that the files were copied. You should see a new
file called Makefile and other files noted in the C++ Critique assignment.
We will use the Makefile to compile programs in this course. This Makefile
will automatically link to libraries that will be used in this course.
You can use it without modification for any C++ program you write as long
as the complete program is in one file. If you write a program in multiple
files, you will need to modify the Makefile slightly to tell it which files
to combine together to make your program. We will go over the Makefile
in a later course.
Programs: Edit, Compile, Debug
You will edit, compile, and run a C++ program in this section.
Starting the editor emacs
To type in your program you need to use an editor. To use the editor
emacs to create a new file for your program click on the emacs icon.
An emacs window should appear (please be patient!). You can use the arrow
keys and the mouse to move around in the emacs window.
Loading, Editing, Saving Files
-
Use the file menu to load a file (even one that does not exist yet).
Click and hold the menu and choose Open File before releasing the
mouse button.
-
The prompt for Find file: appears in the small minibuffer
window at the bottom of the emacs window. You should also see a path which
might be ~/ or ~/cps108/bowling/. If you do not see this,
consult with a TA because you may not be in the correct directory. You
can edit what appears in the minibuffer with arrow/delete keys.
-
You will create a file named seuss.cc. To do so complete the path
in the minibuffer so that it reads ~/cps108/bowling/seuss.cc and
press return.
-
Enter the C++ program shown previously. Be careful that you type it just
as it appears. You will notice sometimes after you type something, the
cursor will "automagically" move to another position to indent properly.
When you type a "}", the cursor always jumps back to the "{" for a second
so you can see if the "}" matches with the correct "{". These are
features of the emacs editor meant to make it easier for you to complete
your programs.
-
If you make a mistake, use the arrow keys or mouse to move around and edit
in the window. You can use the mouse to position the cursor within emacs
by clicking in the window where you want to type. You can also use the
arrow keys and the scroll bar to move around.
In general, you should always have an emacs window open. Unless you are
logging off, there is seldom a reason to quit emacs. You can, however iconify
(make small) the window by clicking the mouse on the dot-button in the
upper right of the window. This works for any window. Double-clicking on
an icon opens that application.
Until you save your file, it is stored in a temporary buffer
(the region in emacs in which you are typing). When you believe the C++
program is correct, save it by clicking the File button at the top
of the emacs window, and then selecting Save Buffer. A message that
the buffer has been written should appear in the minibuffer. You
should save your program often to ensure none of your changes are lost
inadvertently. You will not be granted any leniency if you lose
your work because you did not save it properly.
Compiling a C++ program
A compiler transforms your C++ code into machine readable code the
computer can understand. Programs can be compiled from within emacs as
well. To compile from within emacs:
-
Use the Compile option under the Tools menu.
-
The command make -k, which appears in the minibuffer, is the default
compile command. For most C++ programs, this will be sufficient. Go ahead
and hit return. It may take a while for the program to compile, but you
should see an emacs sub-window with an indication that something is happening.
-
If there is a syntax error in the program, a message will appear in the
*compilation* buffer. To get to the error you can click on the error
with the middle mouse button. This will take your cursor to the line on
which the error appears, so you can use it to cycle through multiple errors,
fix them, and then recompile.
If there are multiple error messages, fix the first one
only and then recompile. Sometimes one error causes the compiler to list
additional errors that are not really errors.
An example of an error message is:
seuss.cc:5: unterminated string or character constant
seuss.cc:5: possible real start of unterminated constant
This message tells you the name of the file (seuss.cc), the line
number the error occurred near (line 5) and a guess as to what
is wrong. You do not need to worry about where line 5 is since clicking
on the error message takes you there.
If you do not have any errors with the first compile, you
might want to modify the program so that there is an error. For example,
remove a ";" and then compile the program.
When the program has been successfully compiled continue with the next
section.
Execute your program
Compiling your program creates a new file with the same name as your source
file (in this case seuss.cc), but without the .cc extension.
This new file is executable, meaning it can now be used as a UNIX
command.
-
In an xterm window, type ls to see the files in you bowling
directory.
You should see all the original files and a new file: bowling.
The file bowling is an executable program. You might also see
a file with the extension ~. This is a backup copy of your program
that emacs automatically creates. It is not an exact copy, but rather a
copy before you made your most recent changes.
-
To run any executable program, type its name in your terminal window, so
type bowling to execute this program.
-
You can type ls -l to see how big the files are and when
they were created.
To conserve the limited space in your account, you may want to get into
the habit of removing the executable program (which is really big) when
you are finished with the assignment (since you can always re-create it
by recompiling the source code). To do this, type rm bowling
to remove the executable.
Print a copy of your program
To print a file, just type print filename . Thus to print the file
game.cc, type print game.cc.
The print command sends your file to the printer in the room you are
currently in. If several people try to print at the same time on the same
printer, the print jobs are queued and printed one at a time. To see where
in the queue your job is type lpq followed by -Pname where
name name is replaced by the printer name. In Teer 106, the printers
are in the back of the room.
If you want to specify a specific printer, use the lpr command.
There are two printers in Teer 106, named teerlp1 (the last three symbols
are lowercase L, p, and the number 1) and teerlp2. To print any text file
use the lpr command, specify the printername (prefixing it with
-P , specify the file is a text file by -Fl (with lowercase
L here) and type the file name. To print your program game.cc
on the printer teerlp1, type
lpr -Pteerlp1 -Fl game.cc
Note you can print your code in two columns (as in the C++ Critique handout)
by giving print the extra argument -2.
Using CVS
For your projects in this course, you will want to consider using a revision
control system like CVS. CVS is a program that manages your source
code allowing you to "back out" of changes that you have made, or have
several versions of your program available quickly and easily. CVS works
by storing only the differences between versions of your program in a separate
directory, called a repository. When you want to commit your changes
to this repository, you issue the command cvs commit. You can
also update your code or add new files to the project. One nice thing about
CVS is that it easily scales to allow groups of people to share a common
set of source code.
A more complete tutorial is available here.
You are strongly urged to work your way through that tutorial, by making
a CVS repository for your bowling project.
Logging out
To exit emacs, choose Exit from the File menu.
If you have not saved all your files, you will be asked at the bottom
of the window whether or not to save a particular file. Type y
for each file you want to save. When you are asked "Subprocesses are executing;
kill them and exit?'' type yes and press Return. At this point,
the emacs window will disappear. You should always exit emacs before logging
out.
To logout of your Sun Ultra-SPARCstation, click on the logout icon
at the top left of the screen.
You will see all the windows and icons disappear and you will be logged
off. Never turn off the workstation!
Remember this document contains only a brief summary of useful commands
to get you started. Refer to the other documents here
to learn more.