A coder's home for Marc "Foddex" Oude Kotte, who used to be located in Enschede, The Netherlands, but now in Stockholm, Sweden!

C++ Lesson 1: Hello World in Windows using Microsoft Visual Studio Express 2008

In this lesson we investigate how to setup a very, very simplistic console based application in Windows. No fancy windows GUI, just outputting the string "Hello world!" and exit. If you're unfamiliar with including header files, don't worry, we'll get there later. This lesson is simply meant to get you started quickly, and not yet focus on technical details too much. Again this lesson is Windows only. Lesson 2 will be about the exact same application, but then getting it work in a Unix based environment.

A requirement for this lesson is that you have Microsoft Visual Studio Express 2008 installed. Older versions probably work just as well, but I'm using the 2008 version. You can download it for free on Microsoft's website. Even commercial use is free, pretty cool of Microsoft aye! When you install Visual Studio, all you need is the C++ component. All other SQL related stuff you can skip during installation.

On with the lesson. The code for our application is extremely simplistic:

#include <stdio.h> int main( int arg, char** argv ) { printf( "Hello world!\n" ); return 0; }

Setting up the project in Visual Studio is slightly more complicated. It's important to know that Microsoft uses a distinct set of terms with Visual Studio, unseen anywhere in C++-land. In Microsoft terms, a "solution" is a set of applications, static and dynamic libraries. In other words all it is, is a collection of proects. These "projects" can be of various types: C++, C, C#, and can result in .exe files, .dll files, etc. Visual Studio always works with one and only one solution. However you cannot create just a solution in Visual Studio, you create a project and get a solution for free. Now take the following steps:

  • open Visual Studio
  • select File | New | Project
  • under Project Types, select "Win32"
  • then under Templates, select "Win32 Console Application"
  • for the Name, enter "helloworld"
  • for Location specify the location in which a new directory named "helloworld" will be created
  • uncheck "Create directory for solution"
  • hit OK

As a result, the dialog will close, and the Win32 Application Wizard will appear. As quite often with Microsoft, the default settings are not what you want. So don't hit Finish just yet, but take the following steps:

  • hit the Next button
  • under "Additional options" check "Empty project"
  • leave "Application type" to "Console application"
  • hit Finish

So what have you done up till now? You have told Visual Studio you are going to build a console application. An application, not a library. So the default compiler and linker settings (more on this in another lesson) are set to produce an .exe file. Visual Studio has gratuitiusly created a solution for your new project. You have indicated you want an empty project: if you hadn't, Visual Studio would have created a bunch of useless files for you, which only would have confused you. Let's continue the lesson by adding code.

  • right click the "Source files" treenode on the left
  • select Add | New item in the popup menu that opened
  • under Categories select "Code"
  • under Templates select "C++ File (.cpp)"
  • leave the Location to what it defauls to
  • set Name to "main" (without the quotes obviously)
  • hit Add

Since you selected the template type "C++ File (.cpp)", Visual Studio appends .cpp to the name "main" you entered, and create the empty file main.cpp for you. By default it's placed in the same directory as your solution and project files. Also, the file automatically opens in the editor. Now copy paste the code as written down at the top of the lesson. You're almost done!

  • hit Build | Build Solution
  • the output window will open, showing you the progress of the compiler

If all went well, your output will look something like this:

1>------ Build started: Project: helloworld, Configuration: Debug Win32 ------ 1>Compiling... 1>main.cpp 1>Compiling manifest to resources... 1>Microsoft (R) Windows (R) Resource Compiler Version 6.1.6723.1 1>Copyright (C) Microsoft Corporation. All rights reserved. 1>Linking... 1>Embedding manifest... 1>Microsoft (R) Windows (R) Resource Compiler Version 6.1.6723.1 1>Copyright (C) Microsoft Corporation. All rights reserved. 1>Build log was saved at "file://c:\a\helloworld\Debug\BuildLog.htm" 1>helloworld - 0 error(s), 0 warning(s) ========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========

You're done! All that remains is executing your application. To do so:

  • hit Ctrl-F5
  • or hit Debug | Start Without Debugging

Before we finish this lesson, it's important to know what files are important to your application, and files are simple transient files used only locally by Visual Studio. In other words, if you were using e.g. SubVersion to share your code, what files would need to be added to the SVN repository, and what files could be ignored. I'll summarize the available files and directories for you:

First off, the files you need:
  • helloworld/helloworld.sln
    This file is the solution file (hence the extension sln). As said all it is, is a collection of projects.
  • helloworld/helloworld.vcprojThis is the project file. It is referenced by filename in the solution file. It contains the compiler configuration, and references to the source files in use by your project.
  • helloworld/main.cppThis is the main source file of this lesson. It's obviously required.

Then, files and directories you don't need, and can ignore in SVN (using the svn:ignore property, or use TortoiseSVN to ignore them).
  • helloworld/Debug
    This directory contains the executable compiled for your helloworld project, in Debug configuration. Configurations will be discussed in a later lesson. For now just know that the .exe file as generated by the compiler resides in that directory. This entire directory can be deleted completely at all times, and is not required to build the application.
  • helloworld/helloworld.vcproj.YOURHOSTNAME.YOURUSERNAME.userAll *.user files can be ignored for SVN. They contain host specific options like debug arguments and execution path.
  • helloworld/helloworld.ncb
  • helloworld/helloworld.suoThe latter is a hidden file, while the first is not. Both are transient files in use (and therefor locked) by Visual Studio when you have the solution opened. What they exactly contain is not known to me, but I believe they have something to do with Visual Studio's automatic code declaration and definition lookup functionality. Both files can be safely deleted at all times and are not required to build.

1 comment(s)

Click to write your own comment

On Sun 02-10-2011 02:24 Keenan wrote: Great article. Pretty easy to run that example.
URL: (optional!)
Write your comment:
Answer this question to prove you're human:
Is the top of this site's background color mostly orange or green?