Microsoft Developer Studio is used as a common programming environment for
Visual C++ and J++. To start Developer Studio, click on the small icon for it
that is found on the Microsoft Office toolbar at the top of the desktop. You
can also select Start, Programs, Microsoft Visual C++,
but that takes longer.
The first time that you start Developer Studio you should create a new workspace. Then each time you write a program you will add it's project to this one workspace. This allows Developer Studio to show you a list of all of your projects and to easily select among them.
Select File, New. Click on the Workspace
tab. (Be sure to always
check that you have selected the correct tab. Otherwise you may create
some other type of new item.) Fill in a name for the new project.
For the location use
c:\fpuser\startup or your private M drive. The latter is
recommended in that no one else can read your files on M. However,
storing files on C, the local hard drive, is a little faster.
(But on C, you have to worry about removing your files before you leave
so that no one can copy them, delete them on you, etc.)
Do not use a diskette, as you will quickly run out of room.
A project groups together all of the files associated with one of your programs and stores them in the project's directory. Especially when we start creating programs with the source code stored in more than one file it will be important to group things into projects. However, even for a program with a single C++ source file, it helps a lot to use a project to keep things separate from the files for other programs.
Select File, New. Click on the Projects tab.
Click on Win32 Console Application.
Fill in a project name, which can be the same name as the main source
file (without the .cpp). For example, hw4 would be
a reasonable project name. The default location should be correct, but
check that it is set to your workspace directory.
Make sure the radio buttons are set to select
Add to current workspace. Do not use
Create a new workspace. The Dependency
check box should not be checked. Finally, click OK.
In your project you will want to have at least one C++ source file
containing the code for your program. If you have just created a new
project, that project will probably already be active. Select
Project, Set Active Project
to see what project is currently the active one. If it is
not the one to which you want to add your C++ source file, click on
the correct one.
Once you are sure that you have the correct project active,
select File, New. Click on the
Files tab and click on C++ source file. Then fill in
the desired file name. The .cpp extension will be added automatically;
there is no need to type that. The location should automatically be set
to the desired project directory, but glance at it to be sure.
The Add to Project
box should be checked and the correct project name should be
shown next to it. Finally, click on OK. You should then be in the
editor, editing a blank .cpp document with the correct name. Type in your
desired code. Select File, Save to save your file. You can also
copy in code from another file by selecting
Insert, File as Text. It is
also possible to add code to a new file by using
Cut, Copy, and Paste
(found under Edit) to bring in code from another file.
Sometimes you will want to use more than one C++ source file. It is typical
to place the main function in one source file, other stand-alone
function in another source file, and functions belonging to a user-defined
class in their own source file. All of these files that belong to
the same program must be added to the same project.
Hint: Make SURE that all of your project's .cpp and .h file (and any data files as well) are all located in your project directory. You can use MyComputer to check on the locations of these files. Many students run into problems with their projects because they have several versions of their files in various directories and don't know which ones are being used by their project.
A header file is a file with a .h extension. It is typically used to contain
constants, types, class declarations, function prototypes, etc.
Executable code is usually not placed in header files. To add a new
header file to a project, one follows almost the same steps as in
creating a C++ source file. Begin by checking that the correct project is
active. Then select File, New. Click on the
Files tab and click on
C/C++ header file. The rest proceeds much as above.
Once you have created a project and its associated C++ source and
header file(s), you are ready to compile. Actually, the best choice is
Rebuild All, found under the Build menu item.
The Compile
option would only handle a project with a single source file.
Rebuild All will see that everything is recompiled, linked, etc.
Output button in the middle of the toolbar. Double clicking on an
error message should take you to the spot in the program where the
error occurred.
To run your program, either click on the button with the red exclamation
point, or select Build, Execute. Since we are creating
console applications, the programs run in a DOS-like window
showing all input and output. (Hint: If you get an error message about
WinMain, that means that you did not set up your project to be a console
application. Make a new project of the correct type and add your files
to it.)
You can find the save commands under the file menu. In addition, always save copies of your .cpp and .h files (perhaps on a diskette) in case a drive should fail. It is suggested that you use MyComputer to copy the files to the desired location. If some of your source files for different projects have the same name, just create directories, one for each project, and place the source files into the correct directories. Remember that you only need to copy your .cpp and .h files for console applications. The workspace, project, and other associated files can be recreated if need be.
On a homework assignment it is important not to leave your .cpp and .h files behind on the lab PC. Someone else could then easily copy your work. On the other hand, files saved on your M drive are pretty safe from prying eyes!
Start Developer Studio. Select File, Open Workspace. Do
not use Open.
In the dialog window, move to your workspace directory, on your M drive
or wherever it is located, and then double click on your .dsw workspace file.
This opens your workspace and places you right where you left off within it.
(Note that an alternative is to use MyComputer to find your .dsw workspace
file and to double click on it.)
If the file that you want to work on is not already visible, you can use
File, Open to bring it into the editor. However, it is easier to
use the File View
usually shown in a window on the left. If this window
is not visible, try clicking on the Workspace button near the middle of
the toolbar at the top of the screen. At the bottom of this window
should be three tabs, labeled C, F, and I. Clicking on the F tab gives
you the File View
of you entire workspace with all of its projects.
Look for your current project. If there is a little box with a + in front of
the line for your project, click on it to have the project's files listed.
Then double click on the desired file to bring it into the editor.
To close a file that you no longer want on the screen, click the lower of the two x buttons at the top right of the screen. (The upper x button closes Developer Studio itself.) You may have several files in the editor and want to close them all before opening another one.
There are many ways to debug a program. One of the simplest is to use
cout
to output the values of key variables at various places in the
program. Another is to use the debugger. You can access it by selecting
Build, Start Debug. This brings up the choices: Go
(to execute until a breakpoint is reached), Step Into (to execute
the program line by line), Run to Cursor (run to the
current cursor location). If you select
Step Into or Run to Cursor, you will be given a
debug toolbar that has other options such as Step Over (also for
executing line by line but without going inside the code for any
function that is called). This toolbar also allows you to restart the
program, to end debugging, etc. If the debug toolbar isn't shown,
look under Tools, Customize, Toolbars and see that the debug toolbar
is checked.
When the debugger is running, one or more windows may be open for displaying values. The debug toolbar has buttons for toggling on/off a watch window, an auto variable display window etc. The former lets you enter the names of variables whose values you would like to see. The latter automatically shows the values of variables used in the line just executed. Note that to add or remove breakpoints you can use the button with the picture of the hand on it. The debugger has many more features which you can learn if you like.
In an earlier section the File View of your workspace was
mentioned. This is normally shown on the left of your screen. With
File View you can scroll through a tree showing all of your source
files, organized by project. If you click on the I tab you get
Info View which gives you access to a lot of
Microsoft documentation (also available under Help).
Clicking on the C tab gives you the Class View.
Even if your program
does not use classes, this view can be helpful in that it shows you the
names and parameter info for all of your functions. If there is a box
with a + in front of your project, click on the + to expand things to
show the classes inside of your project. Then click on the + in front of
a class name to see a list of the functions in that class. Double clicking
on the class name will take you to the class declaration. Double
clicking on a class function name will take you to the code for that
function. The data fields for the class are also shown. Under the directory
labeled Globals you will find the stand-alone functions and
global variables used by your project.
Another useful tool is the Wizard Bar, usually found a few
items to the left of the ! button on the toolbar. It has a
"magic wand" button for a default
action and a downward triangle for displaying other possible class-related
actions (such as go to function definition,
add member function,
add new class, etc.). The Wizard also uses the three windows
on the rest of the line to the left of this. Try it out as it gives a
convenient way of working with classes.
Developer Studio can be customized in various ways. Please resist the temptation to do so as it will only make things difficult for others who use it.
You may, however, change things that only affect your own project. Look under
Project, Settings. Here you can find a lot of detailed
settings. Some of these, for example, can be used to omit debugging
info, to give a faster executable, etc. Although you may change these
settings, do so at your own risk. The default settings will almost
always be appropriate.
The default printing from Developer Studio uses a small font that makes the printout hard to read. You are likely to have better results by starting Word, opening your program file in it, making any desired formatting changes, and printing the file. Do not save it in Word, of course, as the resulting file would not be usable by the compiler due to the word processing codes in it.