Microsoft Visual C/C++  6.0  Summary

 

 

Entering Microsoft C++ 6.0 on the La Salle Network

Logon to the network and enter Windows 95.  Put the cursor on the Start button, hold the left mouse button down, and select:  Programs, Microsoft Visual C++ 6.0, Microsoft Visual C++ 6.0.

 

Exiting Microsoft C++ 6.0

Close the current project:  from the main menu, select File, then Close Workspace, choosing the option to close all open documents.  Close Microsoft C++.

 

Using the Main Menu

The main menu of Microsoft C/C++ is displayed at the top of the screen:

                        File   Edit   View   Insert   Project   Build   Tools   Window   Help

Selecting any main menu command reveals a pull-down submenu showing several options for that command.  Submenu options which are valid appear in black.  Options which are not permitted at the time appear in gray.  Press ESC or click in another window to exit from a menu.

 

Some menu options also have an associated button on the toolbar or key sequence that provides a shortcut to executing the main menu command.  As you gain familiarity with Microsoft C/C++, you may choose to use buttons or "hot keys" instead of the main menu method.  In the beginning, it is important to be comfortable with one means of executing important commands.

 

Entering and Saving a New Program

1.            To open a new editor window for your program, the easiest way is simply to click on the New button, the leftmost button.  (An alternative is to select File from the main menu, then New, then select the Files tab, then select C++ source program.)  You can hide the docked window below the editor window by closing it (click on the small x in the upper left hand corner).

2.            Type the program line by line.

3.            To save the program on your floppy disk, select File, then Save As, and a Save as window will appear:

in the Save in box, click on the t and select a:

in the File name box, type the filename and extension; e.g. type:  hello.c

                        or   lab1.cpp

click the Save button

 

Opening a Previously Saved Program

From the main menu, select File, then Open, or simply click on the Open button, the second button from the left.  NOTE:  if you have previously created a project for this program, open the workspace (or project) instead, then open the program from within the workspace as described in the next paragraph.

 

If you have already opened a previously created project, you can open the main program by opening the dropdown list of modules (click on the t in the module list above the editor window) and double clicking on main.

 

Creating a Project

A project can be viewed as a list of files dependent on each other together with a list of execution options and information (for debugging).  The purpose of a project is to tell the system which files must be compiled and under which options.  In order to execute a program, a project must be created for the program and the project (or workspace) must be open.  Once a project has been created for a particular program, it can be re-used simply by opening it.  It should NOT be re-created.

   1.      From the main menu select File, then New.

2.            In the New window that opens:

¨                  select the Projects tab.

¨                  in the list of execution options on the left, select Win32 console application

¨                  in the Project name box, type the filename of the program but NOT its extension; for example, to create a project for the program hello.c, type the filename hello; do NOT type the .c extension

¨                  in the Location box, type a: or make sure the box has a:\filename

¨                  make sure Create a new workspace is selected

¨                  OK that screen and you will be presented with a screen for Win32 Console application, step 1 of 1, that asks what kind of console application.  Select (what is likely the default) an empty project.  Click on Finish, and OK the next New Project Information window.

If the program was open, the system will close it.  The system creates a project in a folder with the same name as the project. 

 

Saving Space

If you are working on a floppy disk, change the default debugging setting.  Select Project, then Settings.   Select the Link tab.  Set the toggle Generate Debug Info to off.  If you fail to do this, the project will likely not fit on a floppy disk.

 

Adding the Necessary File(s) to the Project

Standard libraries (those included in a program within angle brackets; e.g., #include <stdio.h>) are automatically located and compiled, but user-defined libraries (those included in a program within double quotes; e.g., #include “simpio.h”) are not.  Thus, both header (.h) and implementation files (.c or .cpp) of user-defined libraries included in any file in the project as well as in the main program file must be added to the project.

   1.      From the main menu, select Project, then Add to Project, then Files.

The Insert Files into Project window will open.

¨                  in the Look in box, select a:

¨                  in the list of files which appears, select (in any order and holding down the Ctrl key after selection of the first) the main program file and the .h and .c/.cpp files of all user-defined libraries included in the main program or any file in the project.

   2.      Make sure that the filename of the main program (NO extension) appears in the File name box and make sure that the Insert into box has this same filename, which is also the name of the project.

   3.      OK the insertion.

 

Opening a Previously Created Project

From the main menu, select File, then Open Workspace.  The Open Workspace window will open.

¨                  in the Look in box, select a:

¨                  in the list of projects which appears, double click on the project folder you want to open

¨                  double click on the corresponding .dsw file

You can open the main program by opening the dropdown list of modules (click on the t in the module list above the editor window) and double clicking on main.

 

Closing the Current Project

From the main menu, select File, then Close Workspace.  Choose the option to close all document windows.  Always close the current workspace and all its document windows before opening a new program and creating a new workspace for it.

 

Compiling the Source Program and Any User-Defined Libraries

Compilation translates your C/C++ source program and the libraries it includes into binary machine code; that is, code that the computer can execute. 

 

From the main menu, select Build, then Build  ***.exe, where *** is the filename of the current program.

 

If the compiler detects syntax errors, it will list each error with its line number in a Message window below the editor window.  If you select a compiler error message and double click on it, the editor places the cursor at the place in your code where the error was detected.  The syntax error occurred at or before the place where it was detected.  Be careful to read the full error message.  It will provide a clue (sometimes helpful, sometimes not) to what is wrong.  Armed with the clue and the approximate location, you will need to backtrack from the location to find the problem, then fix it.  The compiler may also list warnings, but warnings can and should be ignored.

 

All syntax errors must be corrected before the program can be successfully compiled.  When your program has successfully compiled, you are ready to run (or execute) it.

 

If you make changes and attempt to run again, you will get a message that one or more files are out of date or do not exist.  In response to the question:  Would you like to build them?, select yes.

 


Executing the Program

Before actually running a program, the system links the compiled version of all libraries with the compiled version of the main program.  To run, simply click on the Run (!) button or from the main menu select  ! Execute  ***.exe, where *** is the filename of the current program.  The system may provide a message that the .exe doesn’t exist and ask if you want to create it; choose Yes.  If the linker finds no errors, the system opens a window in which the program runs. 

 

Linker errors must be dealt with differently than syntax errors.  A syntax error is detected and reported at a particular line in the code.  If you select a syntax error message and double click on it, the editor puts the cursor at the place on the line where the error was detected.  In effect, you can “go to” the line of code which produced the syntax error message.  By contrast, you cannot go to the code which produces a linker error.  You need to learn to understand linker error messages and how to deduce the appropriate solution to the problem.  Some linker error messages are explained at the end of this handout.

 

Note that you can omit the specific Build command and simply execute.  The system will ask you if to want to build first.  Select yes.

 

There are compile, build, and execute buttons to the upper right of the editor window.  Remember that, before compiling, building, or executing a program, a project must be created for it and open in the current workspace.

 

Deleting Unnecessary Files

When your program has run successfully and you do not expect to re-compile it, you can delete some files to save space.  Use Microsoft Explorer to view the project folder on your disk.  Within it is a folder named Debug.  In this Debug folder, you can delete the .ilk, .idb, .pdb, and .pch files.  You will reclaim a lot of space, and the program will still run.  If you later open the workspace and re-compile the program these files will be created again.

 

Terminating Execution

Particularly if you are in an infinite loop or your program “hangs” (possibly because it is in an infinite loop that is invisible to you during execution), you need to terminate execution.  Click on the X in the upper right corner of the execution window.  The system will open a window that asks if you want to terminate the program now and lose any unsaved information.  Choose Yes.

 

Be careful not to “accumulate" execution windows.  The system cannot recompile if an execution window is open.

 

 


Common Error Messages

 

 

Use Help to get a description of the meaning of an error message:  select Help from the main menu, then Search, then Index, enter the number of the error message (e.g., C2666), and select Display.  You may be prompted to insert the CD-ROM for Microsoft Visual C++, in order for the information to be loaded.  Once loaded, the information does not have to be re-loaded.

 

 

Compiler Errors

C2146:  syntax error:  missing  ;  before identifier 

problem:  missing a semicolon at the end of a statement

solution:  double click on the error message.  The system will display an arrow pointing to the statement where it discovered the error.  Backtrack from there to find the point of omission.

 

C2666

problem:  overloaded function or operator is ambiguous

solution:  this version of this compiler enforces type checking more rigorously than, say, the Borland compilers.  You may need to state an identifier type more specifically; for example, instead of int, you may need unsigned int

 

Linker Errors

LNK1168

problem:  cannot open Debug/program_name  for writing

solution:  an execution window is open—in other words, the program is still running so the system cannot replace the current .exe with a new version.  Look in the task bar to find a task with program_name, switch to that task and end it either by following the program’s directions or clicking on the X in the upper right corner of the execution window.

 

LNK1201

problem:  probably insufficient disk space on floppy

solution:  see the section Saving Space above

 

LNK2001:  unresolved external symbol  _main

problem:  did not add main program file to project

solution:  select Project, then Add to project, then Files, and add the main program file

 

LNK2001:  unresolved external symbol  _othersymbolname

problem:  did not add one or more user-defined library files to project

solution:  check the user-defined libraries included in the main program and other included files; then select Project, then Add to project, then Files, and add the .h and .c/.cpp of each library.  The system won’t let you add a file twice.

 


LNK6004:  cannot open the .exe file

problem:  at least one execution window is open.  Look at the task bar and note how many tasks relate to MSVC

solution:  switch to any open execution windows and terminate execution in each by clicking on the X in the upper right corner. The system will open a window that asks if you want to terminate the program now and lose any unsaved information.  Choose Yes.

 

 

Run-Time Errors

An Error window will open and display the message:  “This program has performed an illegal operation and will be shut down.  If the problem persists, contact the program vendor.”  Three options (close, debug, and details) will be offered.  Choose the close option; then, make sure you terminate execution by clicking on the X in the upper right of the execution window. 

 

Next comes the task of trying to figure out what caused the error.  Your program has performed an illegal operation of some sort; e.g.,

ü      a scanf statement where the variables lack the &

ü      a switch statement where the cases lack break statements

ü      attempting to assign a string to a char variable

You will need to isolate the part of the program that caused the error message either by using the debugger to step through the program till the error occurs, or by putting printf or cout statements at appropriate intervals to find where the program “blows up”.  Then carefully examine the code in the area of the program that caused the problem.

 

 

Debugging in Microsoft Visual C/C++

 

Introductory Notes on Debugging

1.        You must compile and link your program so that debugging information is generated and stored in your program’s .obj and .exe files.  Debugging information may require significant space, so your project may not fit on a floppy disk.  A good rule of thumb is to develop your program either in a temporary directory on the hard disk or in your server space.

     2.    The basic unit of execution in this debugger is a line, rather than a statement.  All executable commands, including single stepping and breakpointing, are based on lines.

     3.    During the debugging process, the next line to be executed is identified by an arrow pointing to it from the left.

 

Debugger Menu Items (from Help)

Commands for debugging can be found on the Build menu, the Debug menu, the View menu, and the Edit menu.

 

The Build menu contains a command called Start Debug, which contains a subset of the commands on the full Debug menu. These commands start the debugging process (Go, Step Into, and Run To Cursor). The Debug menu appears in the menu bar while the debugger is running (even if it is stopped at a breakpoint).  From the Debug menu, you can control program execution and access the QuickWatch window. When the debugger is not running, the Debug menu is replaced by the Build menu.

 

The View menu contains commands that display the various debugger windows, such as the Variables window and the Call Stack window.

 

From the Edit menu, you can access the Breakpoints dialog box, from which you can insert, remove, enable, or disable breakpoint

 

Beginning Debugging

One way to begin debugging is to select Build, then Start Debug, and choose of the options on the Start Debug menu. 

 

Stepping Through Execution

 

                            __

Stepping Into   { ¯ }            or F11

The Step Into button’s description is:  execute the current statement, stopping within any functions called.  The arrow identifies the next line to be executed.  Selecting Step Into steps into that next line.  If the current line contains a function call, stepping into the code moves the execution point into the code in the function.  If the current line contains no function calls, stepping into executes the current line.

 

                             ___

Stepping Over   {}¯            or F10

The Step Over button’s description is:  execute the current statement, running any functions called at full speed.  If the next line to be executed contains a function call, selecting Step Over executes the call, stepping over and executing to completion the function call.  The transfer of control to the function body is not shown.  If the next line to be executed has no function calls, the effects of stepping into and stepping over are identical:  the current line is executed.

 

                             __

Stepping Out   {}¯                    or Shift+F11

The Step Out button’s description is:  complete execution of the current function.

 

Run to Cursor   ®{}            or Ctrl+F10

Sometimes you don’t want to step through each line of code to get to the part of the program that is causing problems.  Instead, you may want to step over large amounts of code and re-gain control at the point where you want to begin executing line-by-line.  You can instruct the debugger to execute your program normally (not step-by-step) until a certain location in your code is reached:

    1.     Put the cursor on the line where you want to resume debugging control.

    2.     Select the Run to Cursor button or Debug, then Run To Cursor.

 

Examining Values

Setting a watch on a variable displays the current value of that variable in a watch window, at the bottom of the screen.  It is helpful if the user is aware of what the value of the variable should be.  The value of each watched variable is updated as execution proceeds.

 

If the execution point steps out of the scope of a watched variable, the value of the watched variable is undefined.  Once the execution point enters the scope of the watched variable again, the window displays the current value of the variable once again.

 

When two identifiers from different scopes (including recursive calls) are identical, the identifier in the current or “most local” scope takes precedence.

 

Adding a Watch

Watching a variable enables you to examine its value and to note when that value changes.

You have 2 choices to add a watch:

    1.         a.            Position the cursor within the variable name to be added.

                b.            Select  Debug/Quick Watch.

                        The Quick Watch window will display the Expression name; the Name and current Value are displayed below.  You can choose the Add Watch option to add this identifier to the Watch window

OR

    2.            [regardless of the cursor position]

                a.            Select  Debug/QuickWatch.

                        The Quick Watch window will display the Expression name box; type in the identifier and continue as above.

 

Microsoft uses the same convention in the Watch window for a structured type as it does for folders in a directory structure:  + to left of the identifier means that the fields can be expanded, while – means the fields are expanded.  Click once on the + to expand; click once on the – to contract.

 

Deleting from the Watch Window

In the watch window, select the variable to be deleted.  Press the Delete key.

 

Stepping over/into Input Lines

If you are stepping through statements and current statement contains an input function call, the debugger commands will appear to “gray out”.  Use the task bar at the bottom to switch from the code to the execution window.  Provide the input so that stepping can continue.

 

Debugger Infinite Loops

Be careful to explicitly stop debugging before doing an action like fixing an error and recompiling.  Select Build, then Stop Debugging.

 

Sometimes the debugger will not let you break out of it; i.e., selecting Debug/Stop Debug has no effect.  You will have to use Ctrl+Alt+Delete to end the task.  When you then launch Microsoft C/C++, open the workspace you were using when the problem occurred, and attempt to execute the program, you will likely get an error message:   source program not found.  It appears that either the infinite looping problem or ending the task wipes out the Project/Add to Project/Files information you provided when you first created the workspace.  You will need to re-do this step to add the driver program and the .h and .c or .cpp files of all user-defined libraries.