This material is not complete in any way. Its purpose is twofold: first, to enable
someone who is encountering Visual C++ for the first time, but who knows some
C++, to get started quickly; second, to convey some possibly lesser-known but quite
useful features of the Visual C++ IDE. Suggestions for additional material to
be included on this page are welcome.
Overview
The Visual C++ IDE (Integrated Development Environment)
allows you to write many different kinds of software,
from simple console programs to DLLs (Dynamic Link
Libraries) and Active Template Library Components.
(No need to worry if you don't even know what these
are.) A console program is one that has a simple
(old-fashioned, if you like) text-based interface,
in which program output appears on lines that scroll
upward to the top of, and eventually off, the screen.
Whenever you do something in Visual C++, you generally
do it within Visual C++'s notion of workspaces
and projects. You can have several workspaces
and several projects within each workspace, but for
simplicity we will generally have one project per workspace.
Visual C++ does its best to help you out when you're working.
Sometimes this can be pleasant and useful.
Other times it can be downright unpleasant and frustrating.
It can also be quite overwhelming, when you
find, for example, that Visual C++ has created a dozen or
so files in all, just in the process of helping you
create a very simple program (such as a typical "Hello, world!"
program, for example).
So, it is important to have a good sense of the big picture,
and not to be overwhelmed or intimidated by
all the minutiae that need not concern you,
at least for the forseeable future.
Getting Started: Creating, Building, and Running a New
"Win32 Console Application"
-
Start Visual C++ from the appropriate submenu of the Start menu.
You will probably get an interface containing a menu bar,
one or more "toolbars", and two (or possibly three) blank subwindows.
-
In any case, select File | New (or press Ctrl+N).
A "New" window with the "Projects" tab pre-selected should
appear, and you should be presented with the opportunity to
choose what kind of project you want.
-
Select "Win32 Console Application" from the "Projects" tab.
-
Enter a pathname for your project in the "Location:" box.
The (apparently unchangeable) default entry for this box
is a part of the C: drive to which you cannot write. So,
change it to a location on your F: drive. A simple solution
is just to enter F:\, but you may want to think more about
where on your F: drive you want the project to be located.
-
Enter a name for your project in the "Project name:" box,
and note how it is automatically added to the end of your
pathname.
This is generally useful but can sometimes be
confusing and cause you to end up with a path to your
project containing two subdirectories with the same name.
Just watch what you're doing.
-
Make sure that the "Create new workspace" radio button is activated.
-
Make sure that "Win 32" is checked in the "Platforms:" box.
-
Click OK.
-
In the next window make sure that "An empty project" is chosen.
-
Click "Finish", and read what's in the "New Project Information" window.
-
Click OK.
The "ClassView" pane in the workspace window at the left (or upper left)
should become active at this point. You can switch to the FileView pane
in this window if you like, and you will want to later, but since you
don't yet have any classes or files there's not much point.
At this point you have a single workspace and a single project within
that workspace, and you are ready to begin adding files to this project.
If you have an already existing file that you wish to add to the project,
choose "Add to project | Files..." menu item from the "Project" menu. In
the dialog box that opens make sure the "Files of type:" box shows the kind
of file you're looking for. Then browse to that file and click OK.
Alternatively,
enter the full pathname to the the file in the "File name:" box and then
click OK. Now you can check in the FileView pane of the workspace window
to make sure the file is listed there and the project is thus aware of it.
The steps which follow, however, take a different approach, though they
continue on from the numbered steps above. They assume
you are going to type in a source code file, as you would now do if
you were creating a first, single-file program, such as a typical "Hello,
world!" program.
-
Again, as you did in step 2 above, select File | New or press Ctrl+N.
This time the "New" window that appears should have the "Files" tab pre-
selected.
-
Select the type of file you wish to create by clicking on it.
For console programs this will normally be either "C++ Source File"
or "C/C++ Header File".
-
Enter a name for the file in the "File name:" box.
This file will be saved in your project folder, and default extensions
(.cpp or .h as appropriate) will be supplied if you do not give one
yourself.
-
Click OK.
The text editing window becomes active and ready for text entry.
-
Type in the contents of the file.
-
Repeat steps 12-16 for each file that you wish to add to the
project in this way.
Note that files added in the other way discussed above can be
combined with files added in this way as part of the same project.
-
When all files are ready, select "Build ProjectName.exe" on
the "Build" menu, or press F7, or click the build icon
on the (unlabeled) "Build mini-bar".
Note that the menu option contains the actual name of your
project, followed by a .exe extension (and not the name of
any particular file, including the file with the main function
in it, unless this name happens to coincide with the name of
your project).
The "Output" window at the bottom of the screen will open (if
it is not already open) and any compiler or linker errors you
may have will be reported in this window. Correct any errors
and rebuild if necessary. Double clicking on the line containing
the error in the output window will place a pointer at (with luck)
or near (more likely) the error in your source code. For more
details on any particular error, select the error number and
press F1.
-
When the project "builds" successfully (i.e., when it compiles
and links without any errors), you may then "run" the resulting
executable by choosing "Execute ProjectName.exe" from the
"Build" menu, or press Ctrl+F5, or click the red exclamation
mark (!) on the "Build mini-bar".
A DOS window will then open, and your program will run. When it
finishes running, a message to "Press any key to continue" will
appear, and pressing any key will cause that window to disappear
as you are returned to the Visual C++ environment.
-
You can, if you wish, now modify your program, then re-build
and re-run. Or,
if you make no further changes, you may now close Visual C++,
and it will shut down immediately, since all of your files will
have been automatically saved as a part of the build process.
After you do shut down, open Windows Explorer and examine
the list of files created in your project directory and in a
subdirectory of your project directory called "Debug"
(in which your executable lives).
For the most part you can ignore the many additional files
created by Visual C++ as it builds your project. If you want
further details, see below under Files Used/Created by
Visual C++.
Once you have an executable produced with Visual C++ you
can run the program independently of Visual C++ in the
following two ways:
-
You can double-click on the executable file while in Windows Explorer.
When you do this, note that if the program does not read from the keyboard
you may "miss" its running since the DOS window in which it runs may open
and close very quickly. The reason for this is that Windows Explorer
does not supply that "Press any key to continue" facility that you saw
in the Visual C++ IDE.
-
You may also open a DOS window yourself (by choosing Start | Run, entering
cmd
and pressing Return) and then run the program directly
from the command line, in the "usual" way.
Working with Files in a Project
Once you have added to your project all the files the project needs,
you may perform any of the following actions.
-
Double click on the name of a source code file in the file view of
the workspace window to display it in the text editor window.
-
Place the cursor in the word "include" (of any #include),
right click, and then choose "Open Document ..." to view that
particular header file in the text editor window.
-
Press F7, click the corresponding menu item on the "Build" menu,
or click the Build icon (the one with the two downward-pointing
blue arrows) on the Build mini-toolbar to "build" the executable
for the project. The IDE is smart enough to build only what needs
to be re-built (because it has changed) if this is not the first
build and your project consists of several "pieces". Note that
files are saved to disk as part of the build process. If you get
build errors, they will show up in the Output window at the bottom,
and you can double click on an error to show where it has occurred
in your source code or highlight the error and press F1 to get
more information on the error from the Help system.
-
Choose "Rebuild All" from the Build menu if you wish to re-build
everything in any case (whether it has changed or not).
-
Press Ctrl+F7 to just compile the file named in this option
of the Build menu (which will be either the file currently
active in the text editor, or the one whose name you've
selected in the workspace window if you have in fact selected
one there). You may also click the leftmost icon (the one with
the single arrow pointing downward) on the Build mini-toolbar
to do this.
-
Choose the "Execute" option (marked with a red exclamation
mark) from the Build menu or click on the red exclamation mark
on the Build mini-toolbar to run your program, once it has
built successfully. A potentially confusing situation here
is this: If your program reads input from a file, the IDE
expects that file to be in your project directory, even
though the executable is in a subdirectory called Debug
in your project directory. Such a data file does not have
to be added to the project, so long as it is present and
in the right location.
-
Click on any file in the workspace window file view and press
Delete to delete that file from the project (though not
from its location on disk).
The Text Editor: Creating and Modifying Source Code Files
The first thing to know about the Visual C++ text editor is that
many of the "usual" commands for navigation and applying changes
to text that are familiar to users of other Microsoft products such
as Word also work here in the same way. Hence, assuming this familiarity,
we shall not give the details of those commands here. But it is
assumed that you are familiar with what the following keyboard commands
do for you when editing: the four arrow keys, Home, End, Ctrl+Home,
Ctrl+End, PageUp, PageDown (and using the Shift key with any of these
motion commands to select text), and also Ctrl+C, Ctrl+X, Ctrl+V,
Ctrl+A, and Ins.
We list below some commands and features that are either not
present in other applications, or are not as widely known or used, but
which you may find useful in the Visual C++ environment:
-
Let the editor's "smart autoindent" feature work for you.
It knows, for the most part, how your code should be formatted,
and you should let it decide, for example, things like
what the indent level
should be (4 spaces), and the proper placement of braces.
To make sure this feature is "on", choose Tools | Options ... |
Tabs and activate the "Smart" option of the "Auto indent"
group if this is not the case already.
In fact, you can select a section of code that has had its
formatting "messed up" or is not formatted quite right
and have the whole thing formatted for you automatically
by pressing Alt+F8. This action depends on the "context",
however, and assumes that the surrounding code has been
formatted properly, since it bases the new formatting on
this surrounding code.
-
The F8 key has other users: F8 by itself toggles a character
select, Ctrl+F8 toggles a line select, and Ctrl+Shift+F8
toggles a block select.
-
If a group of contiguous lines of code need to be indented or unindented
one or more levels, select those lines of code and then press the TAB
key to indent an additional level, or the Shift+TAB combination to
unindent one level.
-
It is a good idea to have no TAB characters in your file.
You can remove all TAB characters from a file at any time by selecting
your entire file with Ctrl+A and then choosing
Edit | Advanced | Untabify Selection. Even better, before beginning
text entry, choose Tools | Options and then on the Tabs pane make sure
the "Insert spaces" radio button is activated, not the
"Keep tabs" button. And by the way, this is also where you set
(or reset, if necessary) the "Tab size" and "Indent size", both
of which should be 4.
-
Ctrl+Shift+8 toggles the display of whitespace in your source code.
Blank spaces show up as a dot, while each Tab character shows up
as two greater than signs (>>).
-
Double-clicking on the little rectangular box above the up-arrow
at the top of the scroll-box region at the right of the text editor
window will split the current window into two equal parts horizontally.
This gives you two views of the same file, which can be scrolled and
edited independently. Double-clicking on the separating bar will return
you to a single window. Choosing the Split option from the Window menu
divides the current window into four, which is probably overkill.
Double-clicking on the intersection at the center returns you to a
single window.
-
Ctrl+F2 toggles an unnamed bookmark at the cursor, and F2/Shift+F2
move to the next/previous bookmark. To use named bookmarks, choose
Edit | Bookmarks ... and follow instructions. Ctrl+Shift+F2 clears
all bookmarks in the current window.
-
Ctrl+G allows you to go to various locations, including (probably
most usefully) a particular line number.
-
When the cursor is on either side of a bracket (round, square,
curly, or angle), Ctrl+] will place the cursor on the matching
symbol.
-
Ctrl+U/Ctrl+Shift+U converts selected text to lower/upper case.
-
To change the font size, choose Tools | Options ... | Format | Size:.
-
To expand a source file window to full screen, choose View | Full Screen.
Press Esc to return to the current window at the previous size.
You can use other commands
and/or menus while in full screen mode if you know their keyboard
equivalents.
-
Ctrl+Z undoes the previous change. Pressing it again undoes the change
before that, and so on. If you undo one change to many, you can "re-do"
the last undo with a Ctrl+Y.
-
Clicking in the grey area to the left of the edit window selects the
line opposite. Ctrl+Click there selects the entire contents of the
current window (visible or not). In other words, this action has the
same effect as Ctrl+A.
-
Tab/Shift+Tab indent/unindent selected text one level of indentation.
-
Ctrl+Tab cycles through the queue of open windows, much like Alt+Tab
does in Windows itself.
-
To choose what additional information you want on the page when you
print a file, as well as the margins you want, go to File | Page Setup ...
-
Ctrl+F opens a Find dialog box for entry of a search string, after
which F3/Shift+F3 find the next/previous instances. Esc gets you out
of Find and back to the source code window. Also, Ctrl+F3 will start
a search for selected text, and Ctrl+H starts a Find/Replace.
-
Ctrl+I starts and "incremental search", i.e., a search that starts
as soon as you begin typing and looks for the first instance of
as much as you have typed in so far, and Ctrl+Shift+I starts
a similar backwards search.
-
F4/Shift+F4 move to the next/previous build error or find-in-files
match.
-
Ctrl+Shift+T interchanges the current and previous words, while
Alt+Shift+T interchanges the current and previous lines.
-
Ctrl+Delete deletes to the beginning of the next word, Ctrl+Backspace
to the beginning of the previous word.
-
Ctrl+L deletes the current line and places it on the clipboard,
Ctrl+Shift+L deletes it without placing on the clipboard (i.e., its gone).
Alt+Shift+L deletes to the end of the line.
-
Ctrl with the arrow keys provides some useful commands: Ctrl+RightArrow
moves the cursor to the beginning of the next word, while Ctrl+LeftArrow
moves the cursor to the beginning of the previous word. Ctrl+UpArrow
scrolls the window down one line, while Ctrl+DownArrow scrolls the
window up one line.
Debugging
As your programs get more complex, you may from time to time
want (or need) to use the Visual C++ debugger to help you track
down a runtime error or see where your program is going astray
as it executes. Here is a brief list of some debugger commands:
-
Choose your break points, which can be toggled on and off
with F9 or by clicking the hand icon on the Build mini-toolbar,
when the cursor is on the line where you wish to break.
-
Start debugging by choosing Build | Start Debugging | Go, or by
simply pressing F5, or by clicking the icon on the Build mini-toolbar
that shows some lines of code with a down-arrow to the right.
-
Press F11 to step through your code.
-
To skip over some code place the cursor at the point to which
you would like to go without stopping and then press Ctrl+F10
to "run to the cursor".
-
Press Shift+F5 to stop debugging and Ctrl+Shift+F5 to restart.
-
When debugging, it is useful to have the debug window displayed.
It generally pops up automatically, and gives you a number of
additional options during the debugging process.
Useful Miscellaneous Information
-
An editor macro is like a C++ function (or C++ macro, for that matter)
that can be called upon to perform a specific task. It would be well
worth your while to create some macros and associate them with buttons
on a toolbar or special key sequences. For example, a macro that enters
most of the code for a for-loop for you can save you a lot of typing time
over several months.
-
If your program uses command-line parameters and you are testing it
with the IDE, choose Project | Settings | Debug and enter them in the
"Program arguments:" box you will find there.
-
When you start using the STL (Standard Template Library) it is not
long before you get a warning numbered 4786 (or lots of them) telling
you that some identifiers are too long for the system to handle.
You can get rid of these rather unsettling warnings with the
following line placed at the beginning of your program, just
before your #include directives:
#pragma warning(disable:4786)
A "pragma directive" such as this one is a compiler-specific
instruction of some kind.
-
If you upgrade your compiler at any point, it is a good idea
to delete the .pdb and .pch files associated with each of
your projects, to avoid build errors with the upgraded compiler.
Files Used/Created by Visual C++
If you are curious about what's in the many different
kinds of files that Visual C++ produces
in the course of its meanderings toward your final executable,
go to the MSDN Library (i.e., Visual C++ Help) and search for
"File Types Created"
and then also
"Glossary - Nonalphabetic Terms"
and in both cases be sure to type
the phrase exactly as given, including the spaces and quotation
marks. In each case, display the first item in the title list
obtained as a result of the search.
These two sections of the on-line help will tell you something
about most, if not all, of the files you will see when working
with Visual C++, and also something about many files that you
may never see.