You are on page 1of 25

MANUAL How to Run C/C++ Programs in Visual Studio 2008 Environment

Computer Science Department University of Alabama in Huntsville 2009


1

1. Basic Steps to Run C/C++ Programs in Visual Studio 2008 Environment (1.1) Launch Microsoft Visual Studio 2008 a. Start Programs Microsoft Visual Studio 2008 Microsoft Visual Studio 2008

Figure 1.1 Launching Microsoft Visual Studio 2008

Figure 1.2 Start of Microsoft Visual Studio 2008 2

(1.2) Create C/C++ Project a.1. Click File New Project on the main menu

Figure 1.3 Opening New Project

Figure 1.4 New Project Window a.2. Select Visual C++ Projects General in Project Types and Select Empty Project in Templates 3

Figure 1.5 Selecting Empty Project a.3. Specify your work directory in Location: box, Enter your project name in Name: box, and then Click OK (Solution Name will be written automatically.) E.g., Work Directory C:\Temp\jlee\GTA, Project Name Test1

Figure 1.6 Specifying Work Directory and Project Name

Figure 1.7 Creating Project (e.g., project Test1) (1.3) Write Your C/C++ Program Codes a.1. In the Solution Explorer pane, right click on Source Files, then click Add New Item

Figure 1.8 New Item Step 1

a.2. Select Visual C++ C++ File (.cpp), then type Test1 (without quotations) into the name field, and hit Add

Figure 1.9 New Item Step 2 a.3. Write the your program on the empty file (e.g., source1).

Figure 1.10 Example of a Simple Program

(1.4) Save Your Program File a.1. Save your solution by selecting File Save All (or Ctrl+Shift+s).

Figure 1.11 Saving Your Program File (1.5) Compile and Build Your Program a.1. Click Build Build Solution on the main menu.

Figure 1.19 Building Solution

Figure 1.20 After Compiling and Building Your Program (1.6) Running Your Program a.1. Select Debug Start Without Debugging (or Ctrl+F5)

Figure 1.21 Running Your Program

Figure 1.22 Result of Your Program (e.g., test1.cpp) 2. Additional Information (2.1) Opening Existing Project and Running Program (a.1. or a.2.) a.1. Double Click the Project File (i.e., file with .sln extension )

Figure 2.1 Project File

Figure 2.2 Opened Project Showing Your Program Code To run your program, do Step (1.6) on page 14. To add an additional file, do Step (a.2) on page 6.

10

a.2. Double click on test1.cpp under Source Files. Modify test1.cpp to call the function PrintSmallerNaturalNumbers

Figure 2.10 Modifying Existing File (i.e., underlined in red) a.5. Build and Run the Program using Step (1.6) on page 14.

Figure 2.11 Program Results (2.2) Saving/Running Your Project or Your Source Codes a.1. Saving Your Program: DO NOT save your program codes on the computer in the software lab (N327 or N329). All files on those computers will be regularly deleted.

11

1.1) Saving on a 3.5 Floppy Disk: Since a 3.5 floppy disk can hold only 1.33MB, you can only save your source files to the disk. For example, you should save all the files with .c, .cpp, or .h extensions, not the other files. For example, in Test1 project, you can only save test1.cpp.

Figure 2.12 .cpp Files to Be Saved on a Floppy Disk 1.2) Running Program from Files Saved on a 3.5 Floppy Disk: a. Create a New Project using Step (1.2) on page 4. b. Add Program Files into the Project using Step (1.5) on page 11. c. Build and Run Program using Step (1.6) on page 14. 2.1) Saving on a USB Memory Drive: Since you have enough space, you can save all the files in the project. a. Save Project Test1 Folder

Figure 2.13 Project Folder to Be Saved 2.2) Running Program from Files Saved on a USB Memory Drive: 12

a. Open Project using Step (2.1) on page 16. b. Build and Run Program using Step (1.6) on page 14. 3. Useful Tips (3.1) Using Help a.1. Click Help Index

Figure 3.1 Starting Help a.2. Type in commands you want to get help and Enter. E.g., type printf

13

Figure 3.2 Typing printf on Help Window

Figure 3.3 Help on printf a.3. Close the Help Window by Clicking X in the top right (where red circle mark is) in Figure 3.5. 14

Figure 3.4 Closing Help Window (Note: You can leave this window open, while working on the program.) 4. Debugging Programs using Breakpoints - Breakpoints can be very useful when you want to debug your program. Using break points, you can examine how your program is working. For example, you can watch how variables are changing. The following examples illustrate how to run programs in debugging mode. Example 1) This program adds integer numbers from 1 to 10 and prints the sum using a for loop. Using a break point, we see how the variable i, sum are changed. a.1. Create a Project (i.e., Test2) and Write the Following Program (test2.cpp) in Figure 1.1 using Steps (1.2) ~ (1.6) on pages 3~8.

15

Figure 1.1 Example Program: test2.c a.2. Move the cursor to the line where you want to insert a breakpoint.

Figure 1.2 Moving Your Cursor a.3. Right Mouse Click and select Breakpoint Insert Breakpoint (or F9).

16

Figure 1.3 Inserting a Breakpoint a.4. A breakpoint (i.e., red dot) is inserted.

Figure 1.4 A Breakpoint (a.5. Right Mouse Click and Breakpoint) Delete Breakpoint)

Figure 1.5 Deleting a Breakpoint a.6. Running your program in debugging mode: Debug Start (or F5).

17

Figure 1.6 Debugging Mode Note: If the error message in Figure 1.61 occurs, follow the Steps a.6. (1)~(5).

Figure 1.61 Debugging Error Message a.6. (1) Select Project test2 Properties (Figure 1.62)

Figure 1.62 Changing Project Property a.6. (2) Expand C/C++ and Linker by clicking +

18

a.6. (3) Select General Menu under C/C++ and change Disabled to C7 Compatible (/Z7) under Debug Information Format (Figure 1.63)

Figure 1.63 Changing Project Property a.6. (4) Select Debugging Menu under Linker a.6. (5) Change No to Yes (/DEBUG) under Generate Debug Info and click OK (Figure 1.64)

19

Figure 1.64 Changing Project Property a.7. Program stops at the breakpoint and variables in the function are shown at the bottom (e.g., i and sum).

Figure 1.7 Variable Watch 1 (i = 1, sum= 0) a.8. Continue your program: Debug Continue (or F5). Program stops at the breakpoint again and values of variables are changed.

20

Figure 1.8 Continuing Debugging

Figure 1.9 Variable Watch 2 (i = 2, sum= 1) a.9. Repeat Continue (or F5), then you see how the variables are changed.

21

Figure 1.10 Variable Watch 3 (i = 8, sum= 20) a.10. Exit debugging: Debug Stop Debugging (or Shift+F5)

Figure 1.11 Exiting Debugging Mode Example 2) This program adds even integer numbers and multiplies odd integer numbers from 0 to 10 and prints the result using a for loop. Using a break point, we see how the variable i, num are changed. a.1. Create a Project (i.e., Test6) and Write the Following Program (i.e., test6.c) in Figure 2.1. (See Steps (1.2) and (1.3) on pages 4~8 of Manual I.)

22

Figure 2.1 Example Program: test6.c a.2. Move the cursor to the line where you want to insert a breakpoint and insert a break point using Step a.2 ~ a.4 in Example 1.

Figure 2.2 A Breakpoint Inserted a.3. Run your program in debugging mode: Debug Start (or F5)

Figure 2.3 Program Stopped at the Breakpoint Note: If the program is in debugging mode, but you dont see your program variables as in Figure 2.3 (a), follow the Steps a.3. (1)~(4).

23

Figure 2.3 (a) Debugging Mode Error a.3. (1) Exit Debugging Mode: Debug Stop Debugging (see Figure 1.11) a.3. (2) Project Test6 Properties a.3. (3) Expand Configuration Properties C/C++ and Click Optimization a.3. (4) Change the Any Optimization Option to Disabled(/Od)

Figure 2.3 (b) Changing Project Properties 4 a.4. Continue Debugging using Debug Step Over (or F10)
If you continue debugging using Debug Continue (or F5), the debugging will be ended since there is only one breakpoint in the program and it is not within the for

24

loop. Using Step Over, you can debug in line-by-line fashion. (Yellow arrow indicates the current line.)

Figure 2.4 Debugging: Yellow Arrow at for loop a.5. Keep Continue Debugging using Debug Step Over (or F10) (2 times)

Figure 2.5 Debugging: Yellow Arrow at num += i; a.6. Keep Continue Debugging using Debug Step Over (or F10)

Figure 2.6 Debugging: Yellow Arrow at num *= i; a.7. Exit debugging: Debug Stop Debugging (see Figure 1.11)

25

You might also like