Lab 0: GNU C++ Instructions


A text editor allows you to type in the text of your code and save it in a file. In any operating system, UNIX included, there are many different text editors you could use; however, it's highly recommended that you use a text editor that actively supports the editing of code. (For example, notepad in Microsoft Windows would be terrible since it knows nothing about C++ code.)

The recommended text editors described here are emacs and xemacs. Both of these editors are free, built through a collaborative effort of a number of companies and universities. Xemacs is a descendent of emacs with some extensions, although they are equally powerful for writing C++ programs. In the rest of this document, we use emacs and xemacs interchangeably.

Once we have created a file containing our source program, we can use a compiler to translate a C++ program into machine language. This is the purpose of a compiler: to translate programs into machine language; graphically, we can picture it like so:

This is a bit of a simplification, but it is sufficiently accurate to give you an idea of what the compiler does.

The C++ compiler you'll use is called g++; both it and emacs are GNU software from the the Free Software Foundation. (Technically, g++ is one compiler in a suite of compilers known as GCC; each of these various compilers translates a different language, and g++ compiles C++ code.)

Editing a File

Our first program (listed in the main exercise) will input a base-10 integer and display that same value in base-10 (decimal), base-8 (octal) and base-16 (hexadecimal). We will thus name the executable program bases and name the source program bases.cpp (C++ source programs end in .cpp by convention.) We can use emacs to create the source program by entering:

% emacs bases.cpp &
On an X Window System, the ampersand starts emacs in the background in a separate so that you can use both the command-line prompt emacs at the same time. So if you are not using X, omit the ampersand (&) from the end.

Since the file bases.cpp does not exist in the working directory, emacs will create a new, blank editing window (called a buffer) named bases.cpp where you can type a program. If a file named bases.cpp existed in the working directory, then emacs would create a blank buffer, open that file and read the file into the buffer.

Emacs displays the name of the buffer in the status bar near the bottom of the window; the name of a buffer is the name of the file it is displaying.

Within the bases.cpp buffer, enter the program listed in the main exercise. If you are working from a terminal or via telnet, you will have to type in this program; but if you are using X and viewing this page in a web browser, you can instead copy-and-paste the program into emacs quite simply.

In X, a block of text can be selected by dragging (holding down the left mouse button while moving the mouse) from the beginning to the end of the desired text. Most X environments support a copy-paste short-cut:

  1. Drag the mouse over the text you want to copy, selecting it.
  2. Position the cursor where you want to paste by pointing the mouse there.
  3. Paste by pressing the middle mouse button.
That's it. Copy-and-paste with a selection and one mouse click!

If your environment does not support this short-cut, try clicking the right-mouse button with text selected, and a menu should appear providing cut, copy and paste menu choices. Emacs has an Edit menu which you can use to paste the selection.

Using one of these methods, copy the program into the emacs bases.cpp buffer.

Personalizing the Program

As given in the main exercise, bases.cpp is the work of a fictitious person (Jane Doe) on a fictitious date (February 29, 1999), in a fictitious course (CS I) at a fictitious university (City University). Edit the program's opening comment (the part between the /* and */ symbols) as appropriate to make it your work on the current date, in your course at your institution.

If you are using emacs in X, you can use the mouse to position the cursor at an arbitrary point by pointing at that point and clicking the left mouse button.

The editor command C-d (hitting the Control and d keys simultaneously) can be used to delete the character beneath the cursor; C-k can be used to delete from the cursor to the end of the line.

If you are running emacs outside of X, you can move the cursor with the arrow keys or with these editor commands:

If you mistype something, it can be erased using the delete or backspace key. Often these emacs commands can also be used at a command-line prompt, so they can be awfully useful to know.

If you make a mistake, you can always undo the effects of any emacs command, either by using the undo command: C-x u (type C-x and then hit the u key by itself), or by using the Edit -> Undo menu item. The undo command can be used to undo the effects of an emacs command that has completed.

Some emacs commands take a significant length of time, and you may wish to stop the command while it is still being performed. For this, you can use the get-out command: C-g.

Saving Your Work

When the bases.cpp buffer contains the program, you can store this program in a file by entering the editor save-files command C-x s or the File -> Save menu item. Emacs will ask you to confirm the operation in the mini-buffer at the bottom of the window:

Save file .../labs/lab0/bases.cpp? (y, n, !, ., q or C-h)
To save the contents of the buffer in a file named bases.cpp, answer 'yes' by typing y. Emacs will then confirm the save operation by displaying an appropriate message.

Translating Your Program

When your source program is entered and saved, it is time to translate it into the computer's machine language. In emacs, this can be done using the editor compile command M-x compile (press and release the Meta key (Esc usually) then press the x key, and then type the word compile) at which point emacs will echo what you type in the mini-buffer.

This will shift the cursor to the mini-buffer (at the bottom of the window) where emacs will print

Compile command: make -k

For simple programs like this one, the easiest way to translate our program is to replace the make -k with an explicit call to the GNU C++ compiler g++. Erase the make -k and replace it as follows:

Compile command: g++ bases.cpp -Wall -o bases
This changes the compile command from a call to make (a program we will learn about in a few labs) to an explicit call to the GNU C++ compiler g++, asking it to translate the program in the file bases.cpp.

Helpful hint: Always remember that the -o switch of g++ is followed by the name of the executable program. Otherwise, the order of the command-line arguments does not matter.

Once you have edited the compile command, press the Return key and emacs will split its window in two, displaying the bases.cpp buffer (containing your source program) in one half and displaying a new *compilation* buffer (containing the results of your compile command) in the other half.

If bases.cpp is free of errors, then you should see a message like this in the *compilation* buffer:

Compilation finished at Mon Sep 5 9:12:44

You might, instead, get an error listing:

bases.cpp: In function `int main()':
Compilation exited abnormally with code 1 at Mon Sep 5 9:12:44
If you get an error listing (which you are guaranteed to get many, many, many times in the future) you must have made typing errors in entering the program.

Emacs has a next-error command: C-x ` (that's a backquote, not a single quote or apostrophe). This moves the cursor to the line in your source program where it discovered the error. This is not to say that the problem is definitely there, but it should be close by. Compare the original program with your copy, and fix the error. Use the compile command again to see if you fixed the mistake. Repeat this edit-compile cycle as many times as necessary until your program compiles correctly. Once your program compiles correctly, g++ writes the resulting executable file to the working directory.

Running Your Program from the Command-Line.

Running program bases is the same as running any other program: we enter its name following an operating system prompt.

If you are using the X Window System, the emacs window is just one of many independently running windows. Locate a different terminal window in which the system prompt is displayed. Simply move the mouse into this window (or perhaps click in it) and the cursor should appear, indicating that it is ready for your command.

If you are using a terminal or telnet instead of X, use the emacs suspend command: C-z. The operating system will then suspend emacs and display the system prompt, indicating it is ready to accept a command.

Once you are at the operating system prompt, use pwd to check that your working directory is your lab0 subdirectory. (If not, change directory to make it so.) Then use ls to display the contents of 0, to make sure that both the source file bases.cpp and the binary executable bases are there. At that point, you can enter this command:

% bases
Your program should execute successfully.

Returning to Emacs

If you are using X, simply move the mouse from your xterm window back into the emacs window. If necessary, click the mouse.

If you are using a terminal or telnet and you suspended emacs using C-z, then enter this command:

% fg
This changes emacs from being suspended to being a foreground process.

Quitting emacs

To quit emacs (as opposed to suspending it), you can type the editor quit command: C-x C-c. This will ask you first if you want to save any files before it actually quits, so don't be afraid to use it.

Don't quit yet, but remember this command for later when you are finished.

Quick Reference

Keep this quick reference of emacs commands close by, especially if you don't have the luxury of the X Window System.

Back to the Main Exercise

You should go back to the main exercise now and apply the scientific method to the code that you've now compiled and executed. Remember to bookmark this page so that you can re-use these instructions throughout this lab manual.


background, buffer, compiler, emacs, g++, minibuffer, opening comment, text editor, xemacs

© 2003 by Prentice Hall. All rights reserved.
Report all errors to Jeremy D. Frens.