CSCI 1300 - Exercise 1
Using GNU Emacs and G++

When and Where to Start this First Exercise
...and What To Do If You Missed the First Recitation

PC Labs in the
Engineering Center
CR 235    CR 239
CR 244    CR 252 (24 hours)
CH 107    ME 107
Other campus sites are
listed at
It is best to start a lab exercise during your assigned recitation period. If you run into any immediate problems, one of the instructors will be able to help. However, some of you might have missed your assigned time during the first week. If so, then you'll need to work on your own home machine or go to one of the PC labs on your own to get started.

Required Completion Date

This exercise must be completed as soon as possible after your first recitation. Otherwise you won't have time to complete the first homework (which is due the next week).

Set up Your University Account and Identikey

  • Now, whenever you use a university PC, your first task is to login through the Identikey system. Do this by pressing the return key to get the login screen, then type your login name and IdentiKey password. This will take you to the Windows work screen.

    Installing the CS1300 Software

    Create a Working Directory

    At the end of the installation step, you should have a DOS window open where you will carry out all your work.

    This exercise assumes that you're already familiar with MS Windows and that you can create directories or "folders", and move files from one directory to another. For this exercise, you should start by creating a separate directory for this work. I put my working directory on my D: drive and called it D:\michael. From the DOS command line, the commands to create and move into this directory are:
      cd \
      mkdir michael
      cd michael

    Always put your work in a subdirectory (rather than at the top of the disk drive). There are certain parts of the debugger that won't work correctly at the top of a disk drive.

    Copying and Listing Files

    Now you'll copy some files into your working directory. Let's assume that the cs1300 tools are on the H: drive, and that you've just completed the four commands in the previous step. You can copy the necessary files to your working directory with these commands from the DOS command line:
            copy H:\lab\*.cxx
            copy H:\lab\*.h
    If your cs1300 software is not on H:, then type the correct drive letter rather than H:. After you've done this copying, list the working directory (with the dir command) and you should see at least these files (though the sizes and dates might be slightly different).
        HEATWAVE CXX         2,135  07-19-99  2:30p heatwave.cxx
        INTARRAY CXX         3,018  07-19-99  2:27p intarray.cxx
        SINEWAVE CXX           704  07-19-99  2:27p sinewave.cxx
        INTARRAY H           2,522  07-19-99  2:27p intarray.cxx
    Compiling and Running a C++ Program

  • In your compilation command, the -Wall option instructs the compiler to list all warning messages. These warning messages usually indicate programming errors, and we'll forbid most warning messages in your programs. The -gstabs option puts extra debugging information into your program. At the end of the compilation line, the arguments -o heatwave indicate that the compiler should put its result (the "object code") in a file called heatwave.exe. If you don't specify a location for the object code, then the compiler places its result in a file named a.exe, which is not a particularly useful name.

    Anyway, compile the heatwave program now (as shown above), and then list your files again. You should see these files:

        HEATWAVE CXX         2,135  07-19-99  2:30p heatwave.cxx
        INTARRAY CXX         3,018  07-19-99  2:27p intarray.cxx
        SINEWAVE CXX           704  07-19-99  2:27p sinewave.cxx
        INTARRAY H           2,522  07-19-99  2:27p intarray.h
        HEATWAVE EXE       145,448  07-19-99  2:58p heatwave.exe

    Running a Program
    The important thing now is that heatwave.exe is an executable file, meaning that you can run it by typing its name at the DOS command line. Do this now. Type heatwave, press return, and then interact with the program.
    Using GNU emacs
    In order to modify any of your files, you'll need to use one of the text editors. Some of your have probably used a text editor before, such as edit, vi, pico, or even emacs. In the past, we have let students use whichever editor they wanted. But this semester I'd like everyone to use an editor that has a good degree of integration with a compiler and other tools. The editor is called GNU emacs (yes, the G in GNU is pronounced!).

    You'll first use emacs to edit heatwave.cxx. In order to start emacs and edit the heatwave.cxx file, give this command from the command line:

    emacs heatwave.cxx

    The editor will open, and display the current contents of your heatwave.cxx file. Spend one minute using the terminal's arrow keys to browse through the file. You can also use the keys to page down or page up. Each line of the file is part of a small C++ program. The lines near the top are comments indicating what the program does. One of these comments says that the program is an exercise for Irving Forbush. Use the backspace key to erase Irving's name and put your name instead.

    After you make this change, you may write out the new heatwave.cxx file, by typing Ctrl-x followed by Ctrl-s, and answering y to the prompt at the bottom of the screen.

    Six Pieces of C++
    The next step of this lab is to browse through heatwave.cxx a bit. Using emacs, open the file heatwave.cxx. You might notice that emacs recognized the program as a C++ program, and put the characters (C++) in the status line at the bottom. Move through the program and find the following aspects:

    1. Comments in C++ are indicated by two slashes in a row. The entire line, after the // is a comment.

    2. The program has two include directives that include two C++ libraries:

      #include <iostream> // Provides cin, cout
      #include <cstdlib> // Provides EXIT_SUCCESS

      These directive will appear in most every C++ program. The library iostream provides standard input and output functions and devices such as the standard input. The library cstdlib provides several other standard items such as a constant named EXIT_SUCCESS that our program uses.

    3. After the two include directives, the program declares a double number named PI. But PI is no ordinary variable; it is a constant variable, declared as shown here:

      const double PI = 3.14159;

      A constant declaration, such as this, is a new feature of C and C++. They are like ordinary variable declarations that are given an initial value, but because they are declared as a const, the compiler will prevent you from writing any statements that change the value of this variable. In this class, our programming style requires the names of all constants to be written in capital letters, allowing constants to be easily spotted.

    4. Search through the code and find the heading of the main program, which begins with the words "int ". The word int indicates that the main program computes an integer value and returns this value to the Windows operating system. This may be different than your textbook--many programmers simply declare the main program as a void function (which means that it does not give any final value back to the operating system). But we suggest that your main program returns an int value, since this integer can be used to tell the operating system whether the program was successful or not. For our compilers, a main program returns the number 0 to indicate successful termination. For our g++ compiler, this "successful return value" is defined in cstdlib as a constant called EXIT_SUCCESS. Can you find the return statement in heatwave's main program, which indicates successful termination? (Be warned: older compilers on our DEC machines don't define EXIT_SUCCESS. If you are using an older machine some day, you can still compile by adding the string -DEXIT_SUCCESS=0 immediately after the g++).

    5. Some output is quite simple in C++. A basic output statement looks like this:

      cout << "How big is your tree?" << endl;

      The name cout is the "console output device"--the screen. The operator << is the output operator. So this statement sends the string "How big is your tree?" to the console output device, and then the special object, endl, is sent to the console output device. The object endl is an end-of-line, and it also serves to "flush" the output (meaning that the output will be written then and there, rather than waiting for some output buffer to fill up).

    6. Some input is also quite simple in C++. A basic input statement looks like this:

      cin >> radius;

      The name cin is the "console input device"--the keyboard. The operator >> is the input operator. So this statement inputs a real number into the variable radius. You'll learn a lot more about input/output in other labs, but this will do for now.

    Emacs Indenting
    When emacs knows that a file is a C++ program, it can help you to make sensible indentation. To demonstrate this ability, go down into the body of heatwave.cxx, and mess up the indentation on a few lines. Get rid of some spaces or add some extra spaces at the start of a line. Just mess it up some way. Then press the Esc key followed by p. The letter p stands for "pretty-print," and the command will nicely indent your entire program. This command is one of the things that I set up in your emacs initialization file.

    Here are the indentation items that I have set up in the initialization file:

    • The TAB key: When you press TAB, emacs will attempt to correctly indent the current line.

    • The RETURN key: When you press the RETURN key, emacs will indent the current line, insert a blank line, and move the cursor to the right spot to start typing. If the first character of the newline is a bracket, then its indentation will be corrected after you type the bracket. If you find that you need to turn off the annoying behavior of the RETURN key, then go into your .emacs file and put three semi-colons at the front of the lines that look like this:

      (local-set-key [13] 'tab-return-tab)

    • The Pretty-Printer: When you press the Esc key and then press p, emacs will nicely indent the entire file.
    Now that you know about pretty-printing, you can move to the next step.
    Compiling from within Emacs
    At the moment you are looking at heatwave.cxx from within emacs. You can actually compile heatwave.cxx without stopping the editor. To compile from within the editor, press the escape key once, press x, type the word "compile", and press return. The "mini-window" at the bottom of the screen will change to this message:

    Compile command: make -k

    Use the backspace key to erase "make -k", and instead type your usual compilation command, so that the mini-window looks like this:

    Compile command: g++ -Wall heatwave.cxx -o heatwave

    Then press return. When you press return, the screen will split into two pieces. The top piece is still your heatwave program. The bottom piece is a new window that will contain any error messages from the compilation. During the compilation, there will also be a message near the bottom of the screen indicating "Compilation: run Compiling". When the compilation finishes, the message changes to "Compilation: exit[0]". If there were no compilation errors, you can get rid of the compile window by giving the "one window" command (Ctrl-x followed by typing the digit 1).

    Finding Compilation Errors
    Compiling from within emacs is a big help when there are compilation errors. For example, let's put a compilation error into the heatwave.cxx file. Go down to the input line "cin >> height;" and delete the semicolon at the end of the line. This is certainly an error! Move the cursor away from this error before you proceed to the next paragraph.

    Now, type the compile command again (ESCAPE x compile RETURN). Notice that when you press return, the miniwindow still remembers your compilation command, so just press return once more to get the compilation underway. When the new compilation starts, emacs will ask whether you want to save the changes to heatwave.cxx before compiling. You should answer y (otherwise the compilation would compile the old version of heatwave.cxx, which did not have an error). The compilation will find your syntax error pretty quickly, and display some error message such as "heatwave.cxx:42: parse error before `<'".

    With the error message in the compilation window, you can ask emacs to move the cursor to the location of the error. The emacs "find error" command is Ctrl-x followed by the "backquote" key. (The backquote key is the single quote mark that seems to go backward.) Give this command now. Emacs will move the error message to the top of the compilation window, and move the cursor in the program to line 42. Line 42 is actually one line after the real error. (Remember that usually you must move backward from the error line to find the location of the actual syntax error.) Go ahead and fix this syntax error now, and save the file before moving to the next step.

    Modifying the Heatwave Program

    Printing Your Program

    You can print your program from within emacs (from the Tools menu), but many of the Engineering PCs aren't correctly set up to do this. An alternative way to print a program such as heatwave.cxx is to give this command from the DOS command line:
        copy heatwave.cxx lpt1
    Note that the last word in this command starts with the lowercase letter l and ends with the number 1 (it stands for "line printer number one").

    More About Emacs

    Copying your File to a Floppy Disk Copying a File to or from a University Machine