Skip to content

Howto: Create your first project in Visual C++

April 13, 2012

Introduction

Many people learning C++ from generic sources, such as the excellent Accelerated C++, seem to have a problem creating their first project and getting it to build. This is due to a number of factors such as choosing the wrong project type and/or using precompiled headers incorrectly. This brief howto illustrates how to create, build and run that difficult first project. I assume you have Visual C++ installed correctly – the screenshots are from VC++ 2010 Express, but other recent versions of VC++ should work in the same way.

Step 1

Start VC++. You should be presented with a screen that looks like this:

howto1

Step 2

Like most modern IDEs, VC++ is project-based. You can’t really just compile single C++ source files – you need to put them in a project. To do that, select the New Project… link on the opening page:

howt2

This will produce a dialog giving you a number of possible project types.

Step 3

When learning C++, you will mostly be messing with text-based interfaces. To create a program that uses such an interface, you want to create a console application. From the list of possible project types, select Win32 Console Application:

howto3

You also need to give a name for your project and (optionally) say where you want it to be stored:

howto4

Click on the OK button when you have done this.

Step 4

The Console Application wizard will now start. The first dialog of this wizard is a bit pointless:

howto5

Just click Next>  to move on to the next dialog (don’t click Finish!). In the next dialog, click the Empty Project checkbox to make it checked; this is important – you want an empty project:

howto6

Don’t touch any of the other options. Then click Finish. This will create your new, empty project.

Step 5

You now need to add some C++ code to the project. To add a .cpp source file, right-click with the mouse on the Source Files entry in the Solution Explorer:

howto7

This will pop up a context menu – select Add and New Item… from it:

howto9

You will now be presented with a list of file types – choose C++ File;

howto10

and enter a name for it. The name is not significant, but the extension must be .cpp:

howto11

Hit the Add button to add this new, empty file to your project. You should see that the file has been added to the Source Files in the Solution Explorer, and that a new editor tab has been opened for the file.

Step 6

You now need to add some code to the project and build it.Copy and paste the following into the new edit tab:

#include <iostream>
using namespace std;
int main() {
    cout << "Hello World!\n";
}

Now build your code.  As your solution only contains one project, the simplest method is to choose Build | Build Solution from the main menu, or simply to hit the F7 function key:

howt12

This will compile and link your code, with progress and any errors being shown in the Output tab:

howto13

Step 7

The final step is to run your code. The obvious thing to do is to hit the green arrow on the toolbar, but for reasons known only to some nitwit at Microsoft this does not do what you might think it does – it starts the debugger. To run your code normally, you need to hit the Ctrl-F5 keys which will (for  the above code) to produce:

howto14

Congratulations! You have created, built and run your first VC++ console application.

About these ads
Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: