In the following instructions, words and commands that you must type
into the computer are in boldface.
To get into UNIX, bring up the Jade menu from which you choose m for mail. Instead of m you type x for Exit to Unix Shell. You should get the UNIX $ prompt. (It may be that you will get the Unix $ prompt when you first bring up Jade and you may need to type pine to get into mail). Unix files are maintained in a hierarchical tree-like structure of directories with home as the highest directory available to us. On the next level are the faculty and students directories, and on the third level are the individual home directories for the faculty and students. E.g, my directory is /home/faculty1/psmith. We can store files in our home directories with the usual PC-type filenames or we can create subdirectories using the command mkdir followed by the directory name (e.g. subdir). We all have a subdirectory named WWW in which all our web page files are stored. I would refer to files in my WWW subdirectory as /home/faculty1/psmith/WWW/filename. (Note that UNIX is case-sensitive, so you cannot refer to your web directory as www).
To access the files in the WWW subdirectory, we can use the command cd WWW . To get back up to the home directory type cd with no directory name after it. If you have subdirectories in your subdirectories you can get all the way back to your home directory with the command cd ~. To find out where you are in the directory tree, type pwd (for present working directory). To list the files in your pwd, give the command ls. ls *.c will list all the filenames ending in .c. ls game* will list all the filenames starting with the word game.
Some other file-handling commands you will find helpful in UNIX are:
You can also cut the contents of a UNIX file and paste them into a word processing document or into a UNIX editor screen or an email message using the usual Windows Edit functions.
Note that the delete key does not work inside UNIX. You will have to BackSpace over everything you want to delete.
In the following instructions, words and commands that you must type
into the computer are in boldface.
There are at least three editors which are in common use for preparing files while in the UNIX environment: pico, vi and emacs. Some of you may be familiar with pico which is the editor used in the pine email system. The available options are listed at the bottom of the pico screen. To get into the pico editor, type pico followed by the filename you want to edit. If it is a new file, pico will create it for you. If you do not name the file at the start, pico will ask you for a name when you try to exit with C-x. (Note C-x means hold down the Ctrl key and type x)
I am not very familiar with vi, but it does not have the commands at the bottom of the screen. You have to memorize them. It has significantly more functionality than pico.
If you are not already familiar with emacs (the name stands for "editing macros"), I would like you to learn to use it during this course. It comes the closest to providing an IDE (integrated development environment) in UNIX for C, C++, and Assembly Language programming and debugging. You are familiar with a PC IDE with Visual C++ or Forte for Java where you typed in, ran, and debugged your program in the same environment.
Before using emacs for the first time, you should execute the following command:
cp /home/faculty1/psmith/.emacs .emacs
To start to prepare a file with emacs, you type emacs filename and the following window appears:
Buffers Files Tools Edit Search Mule Help ----:---F1 filename (Fundamental)--L1--All------------------ (New file)
The text you type goes in the window above the F1 line. Below that line (called the status line) is a one line region where the commands you give the editor will appear. Unlike pico, you have to memorize or look up these commands. I have listed the more common ones below.
One of the best features of emacs is that you can split the screen and work on two or more files at the same time. This may be helpful in the first two labs when you have to convert C programs to C++ programs in a process called reengineering. The command to split the screen is C-x 2. To switch between windows use C-x o. To delete the current window use C-x 0. (Note that the character in the switch command is lower-case oh. The character in the delete command is zero). Some of the other common commands are given below:
The GNU C compiler is gcc. You should type in and save your C source file(s) with the .c suffix, e.g. game.c, world.c, etc. To compile your program and link up the necessary libraries, give the command
gcc -o progname -g source1.c source2.c ... -Wall -ansi
The source1.c, source2.c, etc. contain the source code for your program. "progname" will hold the executable version and it can be any name you choose. To run the program, simply type its name, e.g. progname, after a $ UNIX prompt.
If your program has syntax errors, these will list at the bottom of the screen after the gcc command line. If there are too many to fit on one screen (and you are not compiling inside emacs), you can save them to a file by redirecting the standard output and standard error files using the > operator as follows:
gcc -o progname -g source.c -Wall -ansi > listfile 2>&1
since standard output is file number 1 and standard error is file number 2, 2>&1 tells the computer to send everything going to standard error (2) to the same place that results going to standard output (1) are going, i.e., listfile. You can then edit listfile and/or print it. The errors are given by line number. If you are in pico and not emacs when correcting your code, you can use the command C-c to find out which line number the cursor is on. Emacs automatically displays the line that needs correcting.
Once the syntax errors are all corrected and you are able to run the program, you can identify runtime errors using the gdb debugger. To activate the debugger, give the command:
gdb progname
Progname is the name of the executable file you produced through the -o option of the gcc compilation command. If you forget to include the executable name when you activate the debugger, you can bring it up with the (gdb) command file.
Once you get into the debugger with its (gdb) cursor, you usually set a breakpoint at function main with the command break main. (Note: you can also set a breakpoint at a specific line number.) Then give the command run. The program will start to run and stop at the line after int main(). You can then give commands like next or step to execute the line indicated and move to the next line. The difference between next and step is only apparent when a function call is to be executed. next moves right past the function call in the calling program whereas step begins to step through the commands in the function being called. To stop the run of the program, give the command C-c.
You can see the value of any active data object (variable) by
typing the command print name, where name is the data object whose
value we want to know. The command continue will cause the rest of
the program to execute. The command quit gets you out of the
debugger. There is a fairly decent help system accessible by typing
help followed by the topic, e.g. help breakpoints. Typing help alone
gives you instructions for using help. Most of the gdb commands can be activated by typing
their first letter. The following table describes the most common ones:
Abbreviation Command Meaning
file file Loads an executable file into the current window
b break Sets a breakpoint to pause program execution
r run Starts a program running up to the first breakpoint
n next Step through program
s step See above for difference between step and next
l list Lists 10 lines centered on current line
l line1 line2 Lists the lines between line numbers line1 and line2
p print Prints the value of the given variable
i r info registers Lists the values of the hardware registers on the Sun system
i b info breakpoints Prints a list of the current breakpoints
c continue Restarts program execution after a pause at a breakpoint
de delete Deletes all breakpoint settings. de 1 deletes the first
breakpoint, etc.
fin finish Completes the execution of the current function and stops.
It causes the value returned by the function to print