CSC 101 Lab Notes Week 2

CSC 101 Lab Notes Week 2
Text Editing, Compiling, and
Basic Program Development


Overview

The purpose of this lab is three-fold:

  1. To learn the very basic text editing features of the Emacs text editor.
  2. To learn how to compile a C++ program.
  3. To practice editing and compiling by writing some simple programs presented in the Dale lab book.

Exercise 1: Introduction to the Emacs Text Editor

Before getting started with Emacs, we must do a simple clerical task of copying an Emacs startup file. To do this, copy the file ~gfisher/classes/101/.emacs into your home directory. You need not worry at all about the contents of this file for now.

To run the emacs editor, use the following UNIX command from inside a QVT/Term window connected to polylog1:

emacs [<filename>]
where <filename> is any file, new or existing, that you want to edit. The square brackets mean that the filename is optional. If you leave it out, Emacs will start with an editing buffer named "*scratch*", which is just a simple scratch area for typing.

Emacs is a very large and very powerful system. In the beginning, you should use it as just a basic text editor, similar to how you use a program like Notepad on a PC. The major difference between Emacs and editors like Notepad is that in our version of emacs, the mouse and scrollbars do not work and there is no pulldown menu of commands. Hence, you must enter all Emacs commands from the keyboard.

The best way to get started with Emacs is to run its own tutorial. To do this, run Emacs as described above, with no filename argument. Once Emacs starts up, type the command "control-h t". This command is typed by holding down the control key, typing "h", and then typing "t" after the control key is released.

Once you have typed control-h t, Emacs will start its tutorial, which you should read and follow through. During lab, Kristin and I will walk around to help you.

Since Emacs is a very widely used editor, there are lots of reference materials for it. A handy online site with a number of useful links is

http://www.geek-girl.com/emacs/emacs.html
Please, no comments to me about the political incorrectness of the URL name -- it's not my site :) .

Exercise 2: Compiling a C++ Program

Compiling a C++ program is basically a simple task. The command to perform compiling is like other UNIX commands -- a bit cryptic looking.

Recall in Lab 1 that you copied a program named example1 into your csc101 directory. The program that you copied, named just "example1," is an executable object program. The C++ source program from which the executable was built is named "example1.cpp".

To begin this exercise, copy the program

~gfisher/classes/labs/101/examples/example1.cpp
into your csc101 directory. Then compile the program as follows:

This command runs the C++ compiler named "CC" on the C++ source program file "example1.cpp" and puts the executable object program in the file "example1". This is the standard form of the CC command that you should use for compiling all of your programs.

Note: if you do not supply the "-o" argument to CC, it will put the executable program in a file named "a.out". The problem with this is that if you have more than one program, every time you run CC on a different C++ file, you will overwrite any a.out executable of a different program. Therefore, the -o argument is always a good idea.

Exercise 3: Practice Programming

Do the lab work in Chapter 2 of the Dale lab book. Enter the sample lab programs for Lessons 2-1 through 2-4 using Emacs. Compile the sample programs using the CC compile command as described in Exercise 2. The file containing the ounces program referred to in Lesson 2-5 is in the polylog1 file ~csc/sample/DaleLabBook/Chap2/ounces.cpp. The ounces2 program is in that same directory.

Note: The sample programs for lessons 2-1 through 2-4 are in the same directory as the ounces programs, so you could just copy these programs instead of entering them with Emacs. HOWEVER, you should in fact type in the programs yourself from the lab book, so you get some real practice using Emacs.



index | lectures | labs | handouts | assignments | solutions | grades | help