Programming: Principles and Practice Using C++ (2014)

Part V: Appendices

C. Getting Started with Visual Studio

“The universe is not only queerer
than we imagine,
it’s queerer than we can imagine.”

—J. B. S. Haldane

This appendix explains the steps you have to go through to enter a program, compile it, and have it run using Microsoft Visual Studio.


C.1 Getting a program to run

C.2 Installing Visual Studio

C.3 Creating and running a program

C.3.1 Create a new project

C.3.2 Use the std_lib_facilities.h header file

C.3.3 Add a C++ source file to the project

C.3.4 Enter your source code

C.3.5 Build an executable program

C.3.6 Execute the program

C.3.7 Save the program

C.4 Later


C.1 Getting a program to run

To get a program to run, you need to somehow place the files together (so that when a file refers to another — e.g., your source file refers to a header file — it finds it). You then need to invoke the compiler and the linker (if nothing else, then to link to the C++ standard library), and finally you need to run (execute) the program. There are several ways of doing that, and different systems (e.g., Windows and Linux) have different conventions and tool sets. However, you can run all of the examples from this book on all major systems using any of the major tool sets. This appendix explains how to do it for one popular system, Microsoft’s Visual Studio.

Personally, we find few exercises as frustrating as getting a first program to work on a new and strange system. This is a task for which it makes sense to ask for help. However, if you do get help, be sure that the helper teaches you how to do it, rather than just doing it for you.

C.2 Installing Visual Studio

Visual Studio is an interactive development environment (IDE) for Windows. If Visual Studio is not installed on your computer, you may purchase a copy and follow the instructions that come with it, or download and install the free Visual C++ Express fromwww.microsoft.com/express/download. The description here is based on Visual Studio 2005. Other versions may differ slightly.

C.3 Creating and running a program

The steps are:

1. Create a new project.

2. Add a C++ source file to the project.

3. Enter your source code.

4. Build an executable file.

5. Execute the program.

6. Save the program.

C.3.1 Create a new project

In Visual Studio, a “project” is a collection of files that together provide what it takes to create and run a program (also called an application) under Windows.

1. Open the Visual C++ IDE by clicking the Microsoft Visual Studio 2005 icon, or select it from Start > Programs > Microsoft Visual Studio 2005 > Microsoft Visual Studio 2005.

2. Open the File menu, point to New, and click Project.

3. Under Project Types, select Visual C++.

4. In the Templates section, select Win32 Console Application.

5. In the Name text box type the name of your project, for example, Hello,World!.

6. Choose a directory for your project. The default, C:\Documents and Settings\Your Name\My Documents\Visual Studio 2005\Projects, is usually a good choice.

7. Click OK.

8. The WIN32 Application Wizard should appear.

9. Select Application Settings on the left side of the dialog box.

10. Under Additional Options select Empty Project.

11. Click Finish. All compiler settings should now be initialized for your console project.

C.3.2 Use the std_lib_facilities.h header file

For your first programs, we strongly suggest that you use the custom header file std_lib_facilities.h from www.stroustrup.com/Programming/std_lib_facilities.h. Place a copy of it in the directory you chose in §C.3.1, step 6. (Note: Save as text, not HTML.) To use it, you need the line

#include "../../std_lib_facilities.h"

in your program. The “../../” tells the compiler that you placed the header in C:\Documents and Settings\Your Name\My Documents\Visual Studio 2005\Projects where it can be used by all of your projects, rather than right next to your source file in a project where you would have to copy it for each project.

C.3.3 Add a C++ source file to the project

You need at least one source file in your program (and often many):

1. Click the Add New Item icon on the menu bar (usually the second icon from the left). That will open the Add New Item dialog box. Select Code under the Visual C++ category.

2. Select the C++ File (.cpp) icon in the template window. Type the name of your program file (Hello,World!) in the Name text box and click Add.

You have created an empty source code file. You are now ready to type your source code program.

C.3.4 Enter your source code

At this point you can either enter the source code by typing it directly into the IDE, or you can copy and paste it from another source.

C.3.5 Build an executable program

When you believe you have properly entered the source code for your program, go to the Build menu and select Build Solution or hit the triangular icon pointing to the right on the list of icons near the top of the IDE window. The IDE will try to compile and link your program. If it is successful, the message

Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped

should appear in the Output window. Otherwise a number of error messages will appear. Debug the program to correct the errors and Build Solution again.

If you used the triangular icon, the program will automatically start running (executing) if there were no errors. If you used the Build Solution menu item, you have to explicitly start the program, as described in §C.3.6.

C.3.6 Execute the program

Once all errors have been eliminated, execute the program by going to the Debug menu and selecting Start Without Debugging.

C.3.7 Save the program

Under the File menu, click Save All. If you forget and try to close the IDE, the IDE will remind you.

C.4 Later

The IDE has an apparent infinity of features and options. Don’t worry about those early on — or you’ll get completely lost. If you manage to mess up a project so that it “behaves oddly,” ask an experienced friend for help or build a new project from scratch. Over time, slowly experiment with new features and options.