How to Install and Get Started with Visual C++ 2010 Express

Visual C++ 2010 Express

How to Install and Get Started

Introduction

Visual C++ is part of the Visual Studio Programming Suite. A light express version is freely available.

Visual Studio is an Integrated Development Environment (IDE) for developing web applications, client applications, and Windows Phone mobile applications. It supports C/C++, C#, Visual Basic. The versions are:

  • Visual Studio 97: First version bundling many programming tools.
  • Visual Studio Version 6 (1998): Compiled into native executable machine codes (Win32) only.
  • Visual Studio .net 2002/2003 (Version 7): Introduced the so-called managed codes on Common Language Runtime (CLR), and C#. Managed codes run over a virtual machine known as .NET Framework. It uses a portable virtual machine code called Common Intermediate Language (CIL). CLR, .NET Framework, CIL and C# are similar to Java Runtime, Java Virtual Machine, Java portable byte codes, and Java language.
  • Visual Studio 2005 (Version 8): .NET Framework 2.0, ASP.NET 2.0, ADO.NET 2.0 (for database support), etc.
  • Visual Studio 2008 (Version 9): .NET Framework 3.5.
  • Visual Studio 2010 (Version 10): .NET Framework 4.0.
  • Visual Studio 11 (Version 11): upcoming.

How to Install Visual C++ 2010/2008 Express

Simply download and run the VC++ Express online installer @ http://www.microsoft.com/visualstudio/en-us/products/2010-editions/visual-cpp-express. Choose the “Express” instead of “Professional Trial” version.

Write Your First C++ Program in VC++ 2010/2008

Step 0: Launch Visual C++

The “Start Page” appears. Take a good look at the “Start Page” before you proceed.

(For Visual C++ 2010 Express) Switch to “Expert Settings”

From “Tools” menu ⇒ “Settings” ⇒ Check “Expert Settings”.

Step 1: Create a New C++ Project and Solution

Visual studio applications are organized in solutions and projects. A solution consists of one or more related projects (e.g., an exeutable program and its related DLL). A C++ project consists of program codes, headers, and relevant resources.

  1. Select “File” menu ⇒ “New” ⇒ “Project…”.
  2. The “New Project” dialog appears:
    1. In the “Installed Templates” (or “Project Types” in VC2008) pane, expand “Visual C++” node and select “Win32”.
    2. In the middle pane (or “Templates” in VC2008), select “Win32 Console Application”.
    3. In the “Name” field, enter “Hello” as the project name.
    4. In the “Location” field, choose your working directory, for example, “d:\myproject“.
    5. In “Solution” (if there is one), select “Create New Solution”.
      As mentioned, a solution consists of many related projects. By default, the solution name is the same as the first project in the solution. Accept the default. You can add more projects into this solution later.
    6. Check “Create directory for solution”.
    7. Click “OK”.
  3. The “Win32 Application Wizard” appears:
    1. Click “Next”.
    2. In “Application type”, select “Console application” (the default).
    3. In “Additional options”, select “Empty project”.
    4. Click “Finish”.
Step 2: Write a C++ Program
  1. In the “Solution Explorer” pane (the leftmost pane), expand the “hello” project node ⇒ Right-click on “Sources Files” ⇒ “Add” ⇒ “New Item…”.
  2. The “Add New Item” dialog appears.
    1. In the “Installed Templates” pane, expand the “Visual C++” node and select “Code”.
    2. Select “C++ File(.cpp)”.
    3. In the “Name” field, enter “hello” as the filename
    4. Click “Add”.
  3. In the editor pane for “hello.cpp“, enter the following C++ codes:
    #include <iostream>
    using namespace std;
       
    int main() {
        cout << "Hello, world!" << endl;
    }
Step 3: Build (Compile and Link) the Project

From the “Build” menu, choose “Build Solution” (or press F7) to compile and link the program.

Step 4: Run the Project

From the “Debug” menu, select “Start Without Debugging” (or press Ctrl-F5) to run your project. You should see the “Hello, world!” message displayed on the console.

Next:
  • Each C++ program has to be kept in a separate project. For toy programs, you could put the new project under an existing solution. For production, a solution shall only contain related projects.
  • To add another project under this solution “Hello”, right click on the solution (on the Solution Explorer) ⇒ Add ⇒ New Project. Follow the above steps to create the project.
  • To build and run the new project:
    1. right-click on the project ⇒ Set as StartUp Project.
    2. Right-click on the project ⇒ Build.
    3. To run the project: from the “Build” menu, select “Start Without Debugging”.

Read the Documentation

  • At a minimum, you should spend sometimes to browse through the “Getting Started” section on the “START PAGE” (the initial page when the Visual Studio is launched). There are video links as well as tutorials. This will save you many hours of struggling later.
  • “Visual C++ Guided Tour” @ http://msdn.microsoft.com/en-us/library/ms235630.aspx.

Debugging C++ Program in VC++ 2010/2008

Able to use a graphics debugger to debug program is crucial in programming. It could save you countless of hours guessing on what went wrong.

Step 0: Write a C++ Program

Follow the steps in “Writing C++ Program” to write the following C++ program, to be used for the debugging practice. This program computes and prints the factorial of n (=1*2*3*...*n). The program, however, has a logical error and produce a wrong answer for n=20. (It outputs “The Factorial of 20 is -2102132736” – a negative number?!).

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
using namespace std;
 
int main() {
   int n = 20;
   int factorial = 1;
 
    
   for (int i = 1; i <= n; i++) {
      factorial *= i;
   }
   cout << "The Factorial of " << n << " is " << factorial << endl;
}

Run the program and observe the output produced:

The Factorial of 20 is -2102132736

Let’s use the graphic debugger to debug the program.

Step 1: Set an Initial Breakpoint

Set an initial breakpoint at main() function by clicking on the “left-margin” of the line containing main(). (Alternatively, you can place the cursor on the line containing main(), and select the “Toggle Breakpoint (F9)” from the “Debug” menu). A red circle appears indicating a breakpoint has been set at that line. A breakpoint suspends program execution for you to examine the internal states.

Step 2: Start Debugging

From “Debug” menu, select “Start Debugging”. The program begins execution but suspends its execution at the breakpoint, i.e., main(). An yellow arrow (as shown in the diagram) appears and points at the main(), indicating this is the next statement to be executed.

Step 3: Single-Step and Watch the Variables and Outputs


Click the “Step Over” button on the “Debug” toolbar (or select “Step Over (F10)” in “Debug” menu) to single-step thru your program. At each of the step, you could examine the internal state of your program, such as the value of the variables (in the “Auto” or “Locals” pane), the outputs produced by your program (in the console), etc.

Single-stepping thru the program and watching the values of internal variables and the outputs produced is the ultimate mean in debugging programs – because it is exactly how the computer runs your program!

Step 4: Breakpoint, Run-To-Cursor, Continue and Stop

As mentioned, a breakpoint suspends program execution and let you examine the internal states of the program. To set a breakpoint on a particular statement, click the left-margin of that line (or select “Toggle Breakpoint (F9)” from “Debug” menu).

The “Continue (F5)” resumes the program execution, up to the next breakpoint, or till the end of the program.

Single-stepping thru a loop with a large count is time-consuming. You could set a breakpoint at the statement immediately outside the loop (e.g., Line 12 of the above program), and issue “Continue” to complete the loop.

Alternatively, you can place the cursor on a particular statement, right-click and select “Run-To-Cursor (Ctrl-F10)” to resume execution up to this line.

The “Stop Debugging” ends the debugging session. Always terminate your current debugging session using “Stop” or “Continue” till the end of the program.

Important: I can’s stress more that mastering the use of debugger is crucial in programming. Explore the features provided by the debuggers.

Other Debugging Features

Modifying the Value of a Variable: You can modify the value of an internal variable by entering a new value in the “Locals” or “Autos” panes. This technique is handy for modifying the behavior of a program, without changing the source code and rebuild.

Step-Into and Step-Out: To debug a function, you need to use “Step-Into (F11)” to step into the first statement of the function. (“Step-Over” runs the function in a single step without stepping through the statements within the function.) You could use “Step-Out (Shift-F11)” to return to the caller, anywhere within the function. Alternatively, you can set a breakpoint inside a function.

Notes on Visual Studio IDE

  1. Visual Studio organized your work in solution and project.
    1. A project is a single built-target, e.g., a executable program, a DLL (Dynamic Link Library), a static library, etc. A project contains 3 folders: source files, header files, resource files (icons, cursors, bitmaps, etc).
    2. A solution can contain many related projects, that are meant to run together to provide a complete solution (e.g., a client-server based application). By keeping the related projects in a solution, the IDE can help to keep track of the dependencies between projects, and re-build the entire solution if certain part of a project changes.

    In Visual studio, you do not have to create a solution explicitly (there is no way to create a solution anywhere?!). A solution will be automatically created when you create a new project, i.e., the first project under the solution.

  2. To create a new project (and implicitly create a new solution): select “File” menu ⇒ “New” ⇒ “Project”, select “Create New Solution” and check “Create directory for solution”. By default, the name of the solution folder is the same as the first project created under the solution. You may choose a different name for the solution instead. You can add more projects into a solution later (by right-clicking on solution’s name ⇒ “Add” ⇒ “New Project…”). You can choose to build only a particular project instead of the entire solution (right-click on the project ⇒ Project Only ⇒ Build project). If there are more than one project in a solution, you need to set a “Startup” project (right-click the project ⇒ “Set as startup project” – the start-up project will be shown in bold). Running the solution begins at the startup project.

Tips and Tweaks

  1. “Auto-hide” all the panels (Solution Explorer, Output, etc), while you are editing your program (so that you have a bigger screen to work on.) (Visual Studio does not allow you to switch the editor panel to full screen like Eclipse or NetBeans?!)
  2. It is very complicated to rename a project directory and the project name. The steps are:
    1. Close the Visual Studio.
    2. Rename the project directory (using Windows Explorer).
    3. Start the Visual Studio, and ignore all the warning messages.
    4. In the Solution Explorer, select a “unavailable project”. Click the “Property Manager” icon (on the top-left corner of the Solution Explorer) to display the “Property” panel.
    5. Set the “File Path” to the new directory location.
    6. Select the “unavailable project” again, choose “Reload”.
    7. Go back to the “Property” panel, rename the “project name” and “namespace”.

    Alternatively, you can remove the project from Visual Studio, rename the project, and add the project into the Visual Studio again.

    Another alternatively, you can rename the project directory, and edit the “.sol” configuration file.

    Read http://www.platinumbay.com/blogs/dotneticated/archive/2008/01/01/renaming-and-copying-projects-and-solutions.aspx on many other tasks such as renaming and copy project/solution. ( I am still wondering why is it so difficult to rename a project directory in Visual Studio, compared to Eclipse or NetBeans refactoring).

  3. You can choose the compile the code without linking via “Build” menu ⇒ Compile (Ctrl-F7). You can choose to compile/link a project (instead of the entire solution) via “Build” menu ⇒ “Build project-name”.
  4. Figure out the various options in “find/replace” will save you many hours of typing. (Is there a refactoring facility in Visual Studio for renaming entities?!)

File IO in VC++

Suppose that your program receives input from a text file called “xxxx.in“, and writes its output to a text file called “xxxx.out” (e.g., the USACO training site, where “xxxx” is the name of the problem).

File Input is a little tricky under VC++ due to:

  1. When you create a text file in Notepad and saved it as “xxxx.in“, Notepad will append the “.txt” to your file and it becomes “xxxx.in.txt“. Worse still, the Windows’ Explorer, by default, will not show the “.txt” extension (instead it shows a txt file icon). (The first thing I always do to an alien Windows is to change this setting. From “Tools” menu ⇒ Folder Options… ⇒ View ⇒ Uncheck “Hide extensions for known file types”.) You need to put a pair of double quotes around xxxx.in to override the default “.txt” extension. This is one good reason not to use Notepad for programming at all. You should use VC++ to create the input text files directly.
  2. Which directory to keep the input file “xxxx.in” in VC++?

    When you create a new project, VC++ creates a few sub-folders, such as “debug”, “release”, under the base directory. BUT you need to put your input file “xxxx.in” in the base directory of the project.

NOTES:

  • You can create you input file from VC++ directly via ⇒ Add ⇒ New Item… ⇒ Visual C++ ⇒ Utility ⇒ Text File (.txt).
  • Remember to add a newline to the end of your input file to avoid unexpected behavior.
  • You can include existing input files into the project via: File ⇒ Existing item… ⇒ Select the file.

This is a sample C++ program for File input/output. Try it out.

#include <iostream>
#include <fstream>
using namespace std;
   
int main() {
   ifstream in ("test.in");     
   ofstream out ("test.out");   
   
   int a, b;
   in >> a >> b;          
   out << a+b << endl;    
   return 0;
}

Create a text file called “test.in” with the following contents and terminated with a newline:

55 66 

Sample C++ Programs

The following samples are extracted from “Visual C++ Tour”.

Win32 Application

 #include <windows.h>
#include <stdlib.h>
#include <string.h>
#include <tchar.h>
   
 
static TCHAR szWindowClass[] = _T("win32app");            
static TCHAR szTitle[] = _T("Sample Win32 Application");  
HINSTANCE hInst;
   
 
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
   
 
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, 
				   LPSTR lpCmdLine, int nCmdShow) {
   
     
    WNDCLASSEX wcex;
    wcex.cbSize = sizeof(WNDCLASSEX);                 
    wcex.style          = CS_HREDRAW | CS_VREDRAW;    
    wcex.lpfnWndProc    = WndProc;                    
    wcex.cbClsExtra     = 0;          
    wcex.cbWndExtra     = 0;          
    wcex.hInstance      = hInstance;  
    wcex.hIcon          = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APPLICATION));   
    wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);   
    wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);      
    wcex.lpszMenuName   = NULL;            
    wcex.lpszClassName  = szWindowClass;   
    wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_APPLICATION));   
   
	// Reigster the window class structure
	if (!RegisterClassEx(&wcex)) {
        MessageBox(NULL,                            
            _T("Call to RegisterClassEx failed!"),  
            _T("Sample Win32 Application"),         
            NULL);                                  
        return 1;
    }
   
	hInst = hInstance;          
   
	 
    HWND hWnd = CreateWindow(   
		szWindowClass,          
		szTitle,                
		WS_OVERLAPPEDWINDOW,    
		CW_USEDEFAULT, CW_USEDEFAULT,   
		500, 100,   
		NULL,       
		NULL,       
		hInstance,  
		NULL        
	);
   
	if (!hWnd) {
		MessageBox(NULL,
	        _T("Call to CreateWindow failed!"),
		    _T("Sample Win32 Application"),
            NULL);
	    return 1;
	}
   
	 
	ShowWindow(
		hWnd,      
		nCmdShow   
	);
   
	UpdateWindow(hWnd);
   
	 
	MSG msg;
    while (GetMessage(&msg, NULL, 0, 0)) {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
   
    return (int) msg.wParam;
}
   
 
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
	PAINTSTRUCT ps;
	HDC hdc;
	TCHAR greeting[] = _T("Hello, World!");
   
	switch (message) {
		case WM_PAINT:
			hdc = BeginPaint(hWnd, &ps);   
   
			 
			TextOut(hdc,
				5, 5,
				greeting,
				_tcslen(greeting));
			 
   
			EndPaint(hWnd, &ps);  
			break;
		case WM_DESTROY:
			PostQuitMessage(0);
			break;
		default:
			return DefWindowProc(hWnd, message, wParam, lParam);
			break;
	}
	return 0;
}

A C++ Class

Header File:

 
#pragma once
   
class Cardgame {
private:
	int numPlayers;
	static int totalParticipants;
   
public:
	Cardgame(int numPlayer);
	~Cardgame(void);
};

Source File:

 
#include "Cardgame.h"
#include <iostream>
using namespace std;
   
 
Cardgame::Cardgame(int p) {
    numPlayers = p;
    totalParticipants += p;
    cout << p << " players have started a new game.  There are now "
         << totalParticipants << " players in total." << endl;
}
   
 
Cardgame::~Cardgame(void) {
	totalParticipants -= numPlayers;
	cout << numPlayers << " players have finished their game.  There are now "
		 << totalParticipants << " players in total." << endl;
}

Test Program:

 
#include "Cardgame.h"
   
int Cardgame::totalParticipants = 0;
   
int main() {
    Cardgame *bridge = 0;
    Cardgame *blackjack = 0;
    Cardgame *solitaire = 0;
    Cardgame *poker = 0;
   
    bridge = new Cardgame(4);     
    blackjack = new Cardgame(8);
    solitaire = new Cardgame(1);
    delete blackjack;             
    delete bridge;
    poker = new Cardgame(5);
    delete solitaire;
    delete poker;
   
    return 0;
}

Compiling C++ Programs from Command-Line with VC++

To use VC++ compiler to compile a C++ program in the Command Shell (CMD), you need to set the environment variables properly by first running the batch file “vcvars32.bat” in “C:\Program Files\Microsoft Visual Studio 10.0\VC\bin”.

> "c:\Program Files\Microsoft Visual Studio 10.0\VC\bin\vcvars32"
 
> [set current directory pointing to the source file]
> cl /EHsc Hello.cpp
 
> Hello
Set environment variables for VC++,
such as PATH, INCLUDE, LIB.
 
Compile: generate Hello.obj and Hello.exe,
/EHsc to enable Exception Handling for C++
Run Hello.exe

Alternatively, you could use a special Visual Studio 200X Command Prompt, which pre-sets all the environment variables, to compile and execute C++ program. To start the “Visual Studio 200X Command Prompt”: From the “Start” button ⇒ Choose “Microsoft Visual Studio 200X” ⇒ Visual Studio Tools ⇒ Visual Studio 200X Command Prompt.

> [set the current working]
> cl /EHsc Hello.cpp
> Hello
 
> cl /EHsc /c Hello.cpp
> link Hello.obj
> Hello
 
Compile and Link Hello.cpp into Hello.exe (/EHsc to enable Exception Handling for C++)
Run Hello.exe
 
Compile Hello.cpp into Hello.obj (/c means compile only)
Link Hello.obj into Hello.exe
Run Hello.exe

Environment Variables

cl.exe” invokes the preprocessor and compiler, and linker. “link.exe” invokes the linker. The path for “cl.exe” and “link.exe” must be included the the PATH environment variable.

cl.exe” uses the following environment variables:

  • INCLUDE: contains the search paths for include header such as “iostream“, “stdio.h“.
  • CL: if defined, contains the command-line arguments for the “cl.exe“.

link.exe” uses the following environment variables:

  • LIB: contains the search paths for link library. LIB provides only the paths, you need to specify the desired libraries (e.g., user32.lib) as part of the source input.
  • LIBPATH: contains search paths used for #using directive.
  • TMP: temporary file location.

Commonly-used Command-line Options

  • /c: Compile only (int “.obj“), without linking.
  • /Fe <file>: Specifies the executable filename, default is “input-filename.exe“. (gcc uses “-o <file>“).
  • /Fo <file>: Specifies the object filename, default is “input-filename.obj“.
  • /I <dir>: Specifies the Include directories for header files.
  • /L <dir>: Specifies the LIB directories.
  • /link <name>: Link with these lib‘s.
  • /D <macro>: Define macro
  • /Wall: Show all the warning messages.

REFERENCES & RESOURCES

  1. Microsoft Visual Studio Express @ http://www.microsoft.com/express/download.
  2. Visual C++ Developer Center (with Learning Tutorials and Videos) @ http://msdn.microsoft.com/en-us/visualc/default.aspx.
  3. “MSDN Library – Visual C++”, accessed via the “Help” menu.