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 writing C++
programs within the Visual Studio .NET
programming environment; second, to convey
some possibly lesser-known but quite useful
features of the Visual Studio .NET IDE in
the context of making life easier for the
Visual C++ programmer. Suggestions for
additional material to be included on
this page are welcome.
Overview
The first thing to note is that if you are familiar
with Visual C++ from having used Visual C++ 6.0 or
an earlier version you must be prepared to climb
back on the learning curve, since many features
you are familiar with have changed. The Visual
Studio .NET IDE is an attempt to standardize the
programming environment for all programmers that
Microsoft intends to support, and since previous
versions of the Visual C++ IDE were the most different
from the Microsoft IDE norm, C++ programmers are
the ones that will need to spend the most time
getting used to the new environment.
The Visual Studio .NET 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 the latter two
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 the Visual Studio .NET
version of Visual C++, you generally do it within the
notion of a solution and a project.
A solution in Visual Studio .NET is analogous
to a workspace under Visual C++ 6.0, if you
remember what that was. Like a workspace, a solution
may contain more than one project.
Whatever projects you've been working on are noted
by Visual Studio, and you are presented with a list
of the most recent ones on the Start Page whenever
you fire up Visual Studio.
Visual Studio 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 quite a number of files
you know nothing about have been created for you.
Ignoring these extra files is usually the best thing
to do.
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 not for the forseeable future.
Getting Started: Creating, Building, and Running a New
"Win32 Console Application"
Here, in very brief form, is the sequence of steps necessary
to start a new project, enter a single source code file, and
then build and run the executable:
-
Start Visual Studio from the appropriate
submenu of your Start menu. After a short wait,
among the things you will
see on your screen will be a "Start Page" containing
a "New Project" button.
-
Click the "New Project" button.
-
In the window that opens up, choose
"Visual C++ Projects" under "Project Types:"
and "Win32 Project" under "Templates:".
Then enter the name of your project in the
"Name:" box. The "Location:" box should contain
the path you want, so just leave it, although it may
be changed if you really need to make a change
for some reason. (You may want to
make a note of this location so that you can
look there after the next step to see what
files are present.) Make sure the
"Close Solution" radio button is active and then
click "OK".
-
A "Win32 Application Wizard" window opens up.
Click on "Application Settings" in the left pane of
this window. Then choose "Console application",
make sure the "Empty project" box is checked, and
click the "Finish" button. (Now is a good time
to go to Windows Explorer and check to see what has
shown up in the location identified in the "Location:"
box in the previous step.)
-
At this point the Start Page may reappear, but you should
also see a smaller "Solution Explorer" window, probably
at the upper right of your screen. This window should now
contain, among other things, an icon labeled by the name
of your project. If so, your project has been set up
but doesn't contain any source code yet.
-
Under the "Project" menu choose "Add new item..." (or press
Ctrl+Shift+A). In the window that opens up, the left pane
should show "Visual C++" already highlighted, so click on
"C++ file (.cpp)" in the right pane and in the "Name:" box
enter a name for your file. If this is to be the file
which contains the
main
function for your
project, you may want to give it the same name as the
project, but this is by no means necessary.
-
Now click the "Open" button and an editing window will
open, into which you may enter the source code for your
program. If this is your first time through, try the
code for "Hello, world!".
-
When you are ready to try a build, go to the "Build" menu
and choose "Build Solution" (or press Ctrl+Shift+B, or click
on the "Build icon" of the "Build toolbar" if that toolbar is displayed).
Your source code file is automatically saved to disk before the build.
The "Output Window" will open and display the results of your
build, including any compile-time or other errors that have
shown up in the attempt to build your solution. Note that if
you have made an error and have to make source code changes,
the changes you made are again automatically
saved to disk when you choose to rebuild.
You can double click on any error line to show where
that error has occurred in your source code, or
press F1 when the line is highlighted in the "Output
Window" to get more information from the Help system
on the error reported in that line.
-
If the build is successful,
go to the Debug menu and choose
"Start without debugging" (or press
Ctrl+F5) to run your program.
A console window should open
and your program should run in it.
The IDE supplies its own pause at
the end of your program, but
it is generally a "best practice"
to place a pause of your
own at the end of each of
your programs. If you don't,
and you run that executable by
double clicking on it in Windows, you
may not see all of the output from
your program, since the console
window that Windows Explorer opens will
disappear the moment your program ends.
-
Once you have finished to this point,
you may create another project,
or shut down Visual Studio.
Once you have an executable, 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.
Once again, remember that without a pause
at the end of your program
you may "miss" all or part of
the output, since Windows Explorer
does not supply that "Press any key
to continue" facility that is present
in the Visual C++ IDE console window.
-
You may also open a console window yourself
(by choosing Start | Run, entering
cmd
and pressing ENTER, for example)
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 "Solution
Explorer" window to display it in the text editor window.
-
Place the cursor in the word "include" (of any #include line),
right click, and then choose "Open Document" to view that
particular header file in the text editor window. Notice how
a "name tab" appears for each "window" that opens up in the text editor,
above the text of the currently active window. Just clicking on
any such tab will make the corresponding window active.
-
Press Ctrl+F5 at any time to run the current executable.
If it has not yet been built, or if changes have been made
in the source code, you will be
prompted to (re)build before running. Note again that
files are saved to disk as part of the build process.
-
Choose "Rebuild Solution" from the Build menu at any
time if you wish to re-build everything for any reason
(whether it has changed or not).
-
Press Ctrl+F7 if you just want to compile, and not
build an executable.
-
A potentially confusing situation occurs if your program
itself reads from a data file: In this case, 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. On the other hand, if you are running
your program from Windows Explorer, or from the command
prompt in a console window, your program and the data
file from which it reads must be in the same directory,
unless you supply to your program the full path to a file
in some other location.
-
Click on any file in the "Solution Explorer"
window and press the Delete key to delete that
file from the current project (though not
from its physical location on disk).
The Text Editor: Creating and Modifying Source Code Files
The first thing to know about the Visual Studio 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, and (mercifully)
in the same way. Hence, assuming this familiarity,
we shall not give the details of those commands here. It is therefore
assumed that you are familiar with what the following keyboard commands
do for you when editing text: 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), as well as 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 Studio .NET 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.
This feature should be on by default.
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 properly formatted for you automatically
by pressing Ctrl+K, Ctrl+F.
-
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 them an additional level,
or the Shift+TAB combination to
"unindent" them 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. Ideally, your default
should be to have spaces inserted into your file when
you press the TAB key.
-
Ctrl+R, Ctrl+W 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 (>>). This is a great help when to want
to check whether in fact you have removed all the tabs
from your source code, or any other document.
-
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,
as will choosing "Split" from the "Window" menu.
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, as will choosing "Remove Split" from the "Window"
menu.
-
Ctrl+K, Ctrl+K toggles an unnamed bookmark at the cursor line,
and Ctrl+K, Ctrl+N
moves to the next bookmark, while Ctrl+K, Ctrl+P moves to the
previous bookmark. Ctrl+K, Ctrl+L 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
bracket symbol.
-
Ctrl+U/Ctrl+Shift+U converts selected text to lower/upper case.
-
To change the font size, choose Tools | Options | Environment | Fonts and Colors,
and then choose your desired size in the Size: box.
-
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 also use Shift+Alt+Enter to toggle to and from full screen mode.
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 blank area to the left of the edit window selects the
line opposite.
-
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.
-
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.
-
Ctrl+F3 will start a search for selected text.
-
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.
-
Ctrl+Shift+T interchanges the current and following words, while
Alt+Shift+T interchanges the current and following 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).
-
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.
-
Ctrl+K, Ctrl+C will "comment out" any lines you have selected,
while Ctrl+K, Ctrl+U will remove the comments from selected lines
commented out with C++ style comments.
-
Visual Studio has a number of other useful features that
we will not describe in detail here. We do mention them,
however, since you should at least know about them, and
do your best to explore their possibilities, as needed,
in the course of your program development:
-
the "Task Window", which generally pops up, along with the
"Output Window", when you build a project, and can be useful
in tracking and/or annotating problems, or things-to-do,
related to your project
-
"Outlining" of source code (see under Edit | Outlining),
which allows you to alternately collapse and expand
selected sections of code in your editing window
-
the "Clipboard Ring", which may be helpful if you have
some complicated cutting and pasting you want to do
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 run-time error or see where your program is going astray
as it executes. When you start debugging, a number of windows
will open automatically. It may be useful to explore the various
options that are available to you for viewing, and for other actions,
via these windows. Here is a brief list of some debugger commands:
-
Choose your break points, which can be toggled
on and off, either by pressing F9, or by clicking in
the grey vertical bar to the left of the code,
when the cursor is on the code line at which
you wish to break during a run.
-
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 F10 to step through your code (without going into functions,
which may be what you need most of the time), or F11 to step through
your code, including the internal code of functions.
-
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.
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. A number of editor macros
may be available for your use, and, if so,
they will be described elsewhere.
-
If you have used Visual C++ 6.0, you will find that the
C++ in Visual Studio .NET is much more fussy about the
type conversions it allows you to make. In particular,
many implicit conversions that Visual C++ 6.0 happily
permitted are now flagged as warnings by Visual C++ .NET,
a change you might find frustrating at first but which you
should realize is a change for the better.
-
If your program uses command-line parameters and you are testing it
with the IDE, in the "Solution Explorer" window select your project,
right click and choose "Properties". In the "Property Pages" window that
opens, choose Configuration Properties | Debugging and then enter your
command line arguments in the "Command Arguments:" box.
-
If your program uses RTTI (which stands for Run-Time Type
Identification, or Run-Time Type Information, depending on
where you read about it), this
feature must be "turned on". To do this, first right click on
the name of your project in the "Solution Explorer" window and
then choose "Properties". In the "Property Pages" window which
appears, choose "C/C++" and then "Language". One of the lines
in the sub-window that then appears is labeled "Run-Time Type
Info", and this option must be set to
Yes
.
-
When you start using C++ with Visual Studio .NET, you may
get some (or a lot) of "Warning" messages at compile time
that you are not used to seeing, particularly if you are using
the STL. These warnings will be numbered, and if you know that
whatever the "problem" is truly will not affect your code,
you can avoid such a message with a line like the
following placed at the beginning of your program, just
before your #include directives. If the warning is
numbered 4786 (for example), put in this line:
#pragma warning(disable:4786)
A "pragma directive" such as this one is a compiler-specific
instruction of some kind meant for the C++ pre-processor.
-
If you upgrade your compiler at any point, it is a good idea
to delete any .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,
check under "Files Created for Visual C++ Projects" in the MSDN Library.