CS 14 - Lab 4

In this lab you will have an opportunity to get caught up with labs 2 and 3 if you did not finish them. Then, you will compile and execute a program under Linux, a UNIX operating system that can run on IBM-PCs.

Labs 2 and 3

It has come to my attention that several students have been unable to complete these labs. If so, you may complete them and have them graded by the TA in this week's lab, during the first hour only. In particular, you should make sure and complete lab 3, as you will need it for this week's lab.

Note that this offer does not include making up attendance. Each lab (except for lab 1) is worth 20 points; 15 for completing the lab and 5 for attendance. If you did not attend lab 2 or lab 3, you will only be able to make up the 15 non-attendance points.

Lab 4

For this week's lab, you are going to compile and run your program from lab 3 under the Linux operating system.

Booting Into Linux and Opening a Shell

The computer you are using is almost certainly booted into (i.e. running) MS-Windows (especially if you had to finish up lab 2 or 3). If so, you need to re-boot the system to use Linux. To do this, click on Start - Shut Down, select Restart in the dialog box, and click on OK.

The computer will shutdown and then restart. At the boot prompt, you need to indicate you want the computer to boot Linux instead of Windows. Your TA will tell you what to type to do this.

Once the computer is booted into Linux, you will need to login using the same user name and password as you would for Windows. You will notice that Linux uses a GUI (graphical user interface), though it is not as nice as Windows (at least in my opinion). However, in order to compile your program, you will need to use a command-line interface, so you will need to open a shell. This is simply a window where you can type in commands (similar to opening up a MS-DOS window under Windows). Your TA will tell you how to do this.

Shell Commands

Using a shell means that in order to do things, you need to type in commands. For example, type ls and hit return. You should see all of the files and directories in your home directory listed.

Like MS-DOS, the cd command is used to change to another directory. For example, you should have a directory called lab3 (or something similar), in which you have last week's lab files. To change to this directory, type in cd lab3 and hit return. (Note that if the directory name contains a space, such as lab 3, you must use double-quotes when typing the name; i.e. cd "lab 3".) Type in ls again; this time you should see your lab 3 C++ file, along with any other files and directories within this directory. Finally, type cd by itself to return to your home directory.

Creating a New Directory and Copying Files

In order to do this week's lab, first create a directory called lab4. To do this, first make sure you are in your home directory (type cd if you are not sure), then type mkdir lab4 and hit return. Next, change into this new directory by typing cd lab4. If you now type ls, you should see nothing listed (because the directory is empty).

Next, copy your C++ program file from lab3 into this directory. To do this, type cp ../lab3/lab3.cpp lab4.cpp and hit return. Note that this command assumes your lab 3 directory is called lab3 and that your lab 3 C++ program file is called lab3.cpp. If this is not the case, modify the this command accordingly. For example, if your C++ file is called main.cpp, you would type in cp ../lab3/main.cpp lab4.cpp instead.

Compiling the Program and Editing the Program File

Type ls and you should see lab4.cpp listed. This file contains a copy of your lab 3 C++ program file. To compile this, type in g++ lab4.cpp and hit return. The g++ command invokes the GNU C++ compiler. If there are any syntax errors in your program, they will be listed. If there are not, the compiler will create an executable file called a.out (a stupid name; this is a leftover from old-style UNIX compilers). Verify this by typing ls; you should see a.out listed.

If there are any errors listed, you will need to fix them. Most likely you will not have any, since you have already compiled this program under Visual C++. However, there are some differences between these compilers, so it is possible that something in your code that did not cause an error under Visual C++ will be reported as an error under g++.

To fix any errors reported, will need to edit lab4.cpp. You have (at least) 3 choices for editing files under Linux: emacs, vi, and pico. emacs is the most powerful of the 3, vi is also pretty powerful, but pico is the easiest to use, so I recommend (for this week at least) you simply use pico.

To edit your file using pico, type in pico lab4.cpp and hit return. You should see your program loaded into the editor, along with 2 bars of commands listed at the bottom. The most important command is ^X, which stands for CONTROL-x. This command allows you to exit the editor. If you have made any changes, you will be asked if you want to save them or not.

If you did have any compilation errors, use pico to edit lab4.cpp to correct the problem, then compile the file again (by typing g++ lab4.cpp). Do this until you have no compilation errors and the a.out file is created.

Even if you did not have any compilation errors, edit your program with pico just to get a feel for it. Don't worry about making unwanted changes since when you exit pico, it will ask you whether you want to save your changes; if you do not, simply say no.

Running the Program

To execute your program, simply type a.out and hit return.

Grading

To get credit for this week's lab, you need to show your TA that you have created a lab4 directory and copied the lab 3 C++ program file into this directory. You will also need to run the program for the TA to verify that you have compiled it with g++.

More Information

You will be using Linux in lab for much (if not all) of the remainder of the quarter, so if you would like to become more familiar with it, go to http://www.cs.ucr.edu/~klick/tutorials.htm.