Yosef Mendelsohn
Please note that the web version of this document is
optimized for Microsoft’s Internet Explorer.
If you do not have IE, you may download this zipped file containing the text document (in Microsoft Word 97 format)
and associated graphics. The Word
version does not include the videos.
(Click here for a tutorial
on using Winzip)
A tourist in New York City was hopelessly lost and
late for the evening’s symphony performance at the famed Carnegie Hall. Looking about frantically he spotted a taxi
driver stopped at a red light. He ran
over to the car and yelled at the driver:
“Hey, how do you get to Carnegie Hall?”
To
which the wise driver shook his head, and replied:
“Practice, man.
Practice.”
For languages such as Java or C++, your code must be
compiled before the computer will be able to execute (run) it.
Students taking introductory courses in certain programming languages such as C++ or Java are faced with an added step to their work, namely, compilation. To compile a program means to convert the code that the programmer has typed (called source code), into information that the computer can actually understand (object code). Most people have little interest in ever seeing object code, and the more fortunate among us never will. There are a number of reasons for the added overhead of compilation some of which will be described shortly. For the moment, suffice it to say that the step of compiling your source code is a necessary evil which turns out to be quite useful.
As languages such as C++ have evolved, many of them have become more English-like in their syntax and structure. Further, they have often reached a point where a procedure that might take several hundred lines of code in machine language can be accomplished in just one or two lines of, say, C++. The compiler takes care of this “translation”. Compiling code also increases its efficiency so that the program runs faster. Lastly, every compiler you are likely to encounter will also include a component that catches errors in your code and brings them to your attention.
Because compiling is an integral part of the programming process, software publishers have produced compilers that do more than simply compile. For example, applications such as Microsoft’s Visual C++ or Borland’s Turbo C++ include (among other things), a text editor (for typing the code), a debugger (for fixing the broken parts), and of course, the compiler itself. For this reason, some people like to call these applications ‘Integrated Development Environments’ (IDEs) rather than simply ‘compilers’. For simplicity’s sake, however, the general trend is to refer to Visual C++ as ‘the compiler’.
As these development environments have become more advanced their features have been improved accordingly. For example, you will soon notice that when you enter text into the Visual C++ text editor, various key words are printed in different colors. Also, many of the indentations common to programming style are done automatically and are later un-indented when appropriate. You will notice other features as you gain more experience with the environment.
Because many other features have been built around the
compiler, Visual C++ is more aptly
(and maybe pretentiously) called an ‘integrated development environment’
(IDE) rather than a compiler.
The downside to all of this is that learning to use one of these IDEs can be a bit of a bear. The following primer will attempt to guide you through the basics of using the Microsoft Visual C++ IDE.
When you first open the application, you will be faced with the following screen (or something similar to it). The key windows have been labeled:
Now the first step is to create a new ‘workspace’ for yourself. A workspace may contain multiple ‘projects’. However, because we will not use this feature, the terms may be interchanged. For consistency, we will stick with the term ‘workspace’.
When you are confronted with the following steps for the first time, you may find the procedure a bit complex. But rest assured that after the first few times, you will whip through the procedure in seconds.
You will now be faced with the following:
Visual C++ IDE
after creating a new workspace
Notice that the name of your project is visible in the workspace window to the left.
The last procedure was preparation for your workspace. You are now ready to begin writing some code. The idea here is to create a new file, and add it to your already existing workspace.
Now you will notice that back in the IDE the window to the right (the source window) is no longer grayed-out and is available for typing in your code.
If you are a first-time C++ programmer, this is all you will need to do to get coding. However, as you begin creating and using entirely separate files in your projects, you will need to add additional files. The good news is that the process is simply a repeat of the steps outlined just above. However, be sure to give the appropriate extension to your file name (e.g. .CPP or .H or .CXX, etc.).
Be warned about the following: Once you are done working with a given program and wish to begin another, do not simply add another file to your project. Why? Because when you tell the IDE to execute the program, the first thing it does is look for a function called ‘main’, and calls (runs) that function. If you have two separate files both of which have a ‘main’ function, you will find that you only have access to the first file you typed. The idea of a ‘main’ function will be made (somewhat) more clear following your first lecture in C++. So, you must first close the project (workspace) in which you are currently working before embarking on another.
To recap: When you are done working with a program and are ready to begin another (or call it a day), you must close the workspace. To do so, click on File, Close Workspace.
To close a workspace, click on File, Close Workspace
Once you have managed to get a workspace up and open, you are ready to begin programming. Type in some code. If this is your first encounter with a C++ program, you may copy and paste the following code:
#include <iostream.h>
int main( )
{
cout <<
“Farewell to innocence” << endl;
return 0;
}
What you have just typed is called source code. However, as stated earlier, the computer
can’t interpret source code. We need to
let the IDE compile the source code into object code. To do so, click on the build* icon or alternatively, type ‘F7’. Assuming there are no errors (a patently
foolish assumption), you can then click on the execute icon
or type
CONTROL-F5. This will bring up a DOS
window where your program will run.
Now to address the no-errors fallacy: You may have noticed that when you compile your program, the output window (the long window just below the source window) jumps to life. You will quickly enter into a love/hate relationship with this window. Like an overzealous athletic trainer, it will examine every last letter of your code, refusing to rest until it has pointed out each and every error it can find. However, it does try to figure out what the cause of those errors and bring them to your attention. Initially, you will find the error messages very difficult to decipher, and may choose to simply go up to the indicated line and search from there. Given time, you will become more adept at deciphering the jargon inherent in compiler-generated error messages.
To Run a Program:
A few notes about compiler error messages:
A slick little shortcut for working with the output screen: Hitting ‘F4’ will take you to the first error message and instantly move the cursor to the culpable line of code. Hitting ‘F4’ subsequent times will cycle you through each error message in turn.
Just because you eventually get to the point where the compiler gives you the friendly ‘0 errors’ message does not mean that you have a perfect program. There are certain kinds of errors the compiler is unable to catch. Perhaps the most insidious category of errors are errors of logic. This is somewhat analogous to a spell-checker not knowing the difference between “where” and “wear”. If you mistakenly type “Wear or wear has my little dog gone”, the spell-checker will, of course, fail to flag the error.
Similarly, you may find yourself typing a line of code such as:
sum
= num1 * num2;
Even though you probably meant to add the two numbers rather than multiply them, the compiler will regard the above expression as a perfectly appropriate C++ statement.
The compiler will not catch most errors of logic.
Once you have finally appeased the compiler and are faced with the blessed “0 errors” message, you are ready to execute your program. To do so, you can click on execute icon, or type CONTROL-F5. This will bring up a DOS window and execute your program.
It won’t take long before you discover that in the compiling process, the compiler generates a whole bunch of other files, some of which are quite large. In fact, if you are working from a floppy disk, you may find that you very quickly use up the entire floppy. The good news is that unless you are planning to sell your program commercially, you won’t need any of these files. The only files of interest to you (for storing somewhere) are the ones whose filenames end with the extension .CPP, .H, or .CXX. We will refer to these as your source files. So once you are done working with a program, you can shut down Visual C++, and get rid of all but the source files (those not ending in .CPP, .H, or .CXX). This includes the ‘debug’ directory.
In fact, for those of you submitting your assignments on disk or via e-mail, the source-files are the only ones that you should turn in.
A suggestion: Because floppies are so slow and the IDE makes frequent disk accesses, I highly recommend that you do all your work on the hard-drive. If you are working from a student lab or at another computer that is not yours personally, you can then just copy the aforementioned source files to a floppy for storage or later use.
When you are done with your work, the only files you
need to save are the source files.
(Those ending in CPP, CXX,
or H).
If you have somehow struggled your way to this point in my jeremiad, and are feeling a bit overwhelmed, that’s okay. It may seem like a great deal of material, but after going through these procedures a few times, you will begin doing them automatically. Don’t get discouraged. Remember: Practice, man. Practice.
* For now we will treat ‘build’ and ‘compile’ as building the same, although this is not entirely accurate.