Hints and Tricks to using Visual C++

Preface

This is not a set of notes to teach you how to program but rather how to move around in the Visual C++ environment. I am trying to keep this general enough for everybody including first year students who have not programmed before.

Starting up

Click on the Start button and select ?? then 'Microsoft Visual C++ ?.0' then finally 'Microsoft Visual C++ ?.0'. After a few seconds (or minutes) you will be in the Microsoft Developer Studio.

Parts of the Environment

Please try to locate each of the following:

Creating a project

Visual C++ manages all projects as Workspaces. A workspace records information about the location of the windows and the files required to compile the program.

To create a new project pull down the 'File' menu and select 'New'. You will be given a dialog box with a lot of options. If the projects tab is not already selected do so now. If you are writing a non-windows based program (which you will be if you are in CS1711) then select 'Win32 Console Application', if you are writing a windows program in CS2611 then select 'Win32 Application'.

In the project name, type the name of your project with no extensions. For example HelloWorld would be fine (don't press enter yet!). You can modify the location of the project if you wish. Make sure the option to 'Create new workspace' is chosen. Finally click the Ok button.

The workspace window should have changed slightly. You will now see three tabs, we will be interested in just the FileView tab. The ClassView tab is very handy if you are proficient in programming, but I suggest that you avoid it for the time being. Click on the FileView tab.

To write a program, you need to have a source file. Pull down the File menu and select New again. This time the Files tab should be active. Click on 'C++ Source File' and give the file a name with no extension or add the .cpp extension. If you selected the wrong type (I've done it a time or two) just repeat the process and when it asks you if you want to overwrite the existing file say Yes.

Type your program. For example lets use:

#include 

main()
{
   cout << "Hello world" << endl;

   return(0);
}

Compiling and Running

Once you have writen the program, you should save it! Click on the Disk on the tool bar or select File then Save.

You have some options here. You can either compile then run the program, or you can try to run the program. If the program needs to be compiled then the environment will compile it for you. I suggest trying to compile (build) the program first. If there are warnings then you can fix them (most warnings are trying to tell you something).

To build the program pull down the Build menu and select Build. You can can also build the program by pressing F7 (as seen in the menu) or by pressing the Build button on the toolbar.

When you instruct the environment to build an output window will appear along the bottom of the screen. This will contain error message when the 'Build' tab is selected. If you typed everything correctly then you should see on the last line: 0 error(s), 0 warning(s). This is what you hope for. Now you can run the program by selecting Build|Execute or by pressing the exclamation mark on the toolbar. Doing so will open a dos window with the output followed by the instruction to 'Press any key to continue'.

Syntax Errors

To see how syntax errors look, go to the end of the cout statement and remove the semicolon. If you try to build the program now, you will get the message that 1 error occured. If you scroll back in the output window you should find a message like: "filename.cpp(8) : error C2143 syntax error : missing ';' before 'return'". Sometimes the error messages will be this clear sometimes they will be very cryptic. If you double click on the error you will be brought directly to the line containing the error (or somewhere close).

Saving your work

The files and workspaces are stored independently. When you try to exit the developer studio the workspace will be stored automatically.

To go back and work on a project you closed before you should pull down the File menu and see if the workspace is listed in 'Recent Workspaces'. If the workspace is not listed there then select Open Workspace and change to the directory where the project was stored looking for a file with the extension dsw.

Recovering from a crash

In the unlikely event of a developer studio crash in which your files were saved and the workspace was not (this also applies to students who are reusing existing code they have written) you should create a new workspace and select the FileView in the workspace window. Right click on the name of the project (should be the second line from the top) and select 'Add Files to Project...'.

FAQs

I'll add answers to questions regarding the visual c++ programming environment here. There will be no programming specific information unless you need to do something to the environment.

Why is the compiler complaining about unknown symbols during the link phase? I know the function I write is there and correct! I think there may be a bug with Microsoft's Incremental Linker. I think the linker can sometimes become confused and will not add the latest symbols to the link file. I've found that performing a 'Build All' will usually cure this problem.