Microsoft Visual C++ .NET 2003 Tutorial
This short tutorial is intended to lead the reader through the basics of writing, compiling, linking and running simple console C++ programs using Microsoft Visual C++ .NET. 2003 / 2005
Microsoft Visual C++ .NET needs a project folder in order to store most of the data related to the compilation and linking (object files, executables, resources, ...). Therefore, the first thing you will want to do is create a project, even if your source code is extremely simple. Here is how you will create your project:
-
After
having started Microsoft Visual C++ .NET, Open the "File" menu, and Hover over
"New", then select "Project..." (You can also press the key combination Control
+ Shift + N instead) to bring up the "New Project" setup window, which is used
by Microsoft Visual C++ .NET to launch all the Wizards dedicated to the creation
of new projects.
-
Since we
are experimenting on console C++ programs, select "Win32 Console Application"
from the list. You might of course try the other types of projects, once you are
done with this tutorial, but it would be outside of our scope to describe them
all.
-
Give a
meaningful name to your project, then browse to the hard disk location you want
it to be stored to. Please take note that Microsoft Visual C++ .NET will take
care of creating the project directory if it does not already exist.
-
Click "OK"
to validate your input, and start the project creation
Wizard.
-
Select
"Application Settings"
-
Although
you might find interesting to try out sample applications offered by the Wizard,
we will in our present case be completely satisfied by an empty project. We will
add files to it later.
-
After
having clicked the "Finish" button, you will finally be able to start adding
files to your newly created project.
The project having been created, we will now start adding files to it. Microsoft Visual C++ .NET gives the possibility to add existing files to a project (Via the "Project" menu), but we will in our tutorial restrict ourselves to the creation of a new empty source code file. Here are the steps required to achieve this:
-
Bring up
the "Add New Item" setup window by right-clicking or "Source Files" folder on
left hand side view, then selecting "Add >" - "New Item" (or selecting the
"New..." option from the "File" menu, or by pressing Control + N key
combination)
-
From the
main panel, select the list item labeled C++ File (.cpp), then give it a
meaningful name.
-
Once you
have submitted your input by clicking the "Add" button, Microsoft Visual C++
.NET fires the source code editor, and updates the Solution Explorer (the tab on
the left hand side showing you the list of files and resources attached to the
project).
-
Finally,
type the content you wish into the file, using the editor.
-
Select
"Save all" from the "File" menu to save all the data related to the project.
Please note that this step is done automatically before compiling the project
(see next section for compiling instructions) if the project had not been saved
already.
The
following instructions work for most of Microsoft Visual C++ .NET projects,
except rare exceptions. The "Build" menu is the place where you will find
everything related to Compiling, Building and Running the program generated by
your source code. Here are the steps to go through to achieve
this:
-
Open
"Build" menu, then select "Build Solution" (Or press F7) to compile and build
the project in a single step. Please note that this action will save all the
modifications to project files before proceeding, if they have been modified
since their last save. This step is done automatically before running the
resulting executable (see next step for running the executable), if it had not
been built already.
-
If
Microsoft Visual C++ .NET did not report any building error, that means the
program has been built successfully, and you can now start it from within
Microsoft Visual C++ .NET. To do so, open "Debug" menu, and select "Start
Without Debugging" (resp. "Start Debugging" if you want a step by step
execution) (or use the key combination Control + F5 (resp. F5)). Please note
that this will build the program before proceeding, if it had not been built
already. From this point on, Microsoft Visual C++ .NET will launch a
command-line shell, and start running the program into it. You can set
additional parameters (Command line parameters for example) for the program,
using Project settings ("Project" menu, or Alt + F7).
-
After
having ran the program, Microsoft Visual C++ .NET will pause the execution until
you press a key, so that you get the possibility to view eventual results before
the execution window closes itself. Please note that Microsoft Visual C++ .NET
is the one forcing this pause. If you plan to start the built executable
independently from Microsoft Visual C++ .NET, you will have to insert this pause
if you want to prevent the application from closing itself before the user gets
the chance to see eventual output, in case the application would not be started
from the command-line.
As a short
summary for this section, simply note that it is usually enough to press Control
+ F5 to have Microsoft Visual C++ .NET sequentially save the project, try to
compile it (it will alert on errors so that you can correct them before
proceeding), try to build it (it will alert on errors), and finally run
it.
Finally,
you can close project files using the "File" menu, and selecting the option
"Close Solution".
However,
this step is not mandatory since Microsoft Visual C++ 6.0 allows you to create
new projects or to close the application while you have a project open in it.