Manual HowToRunVS2008
-
Upload
divyanshu-sinha -
Category
Documents
-
view
217 -
download
0
Transcript of Manual HowToRunVS2008
-
8/4/2019 Manual HowToRunVS2008
1/25
MANUAL
How to
Run C/C++ Programs in
Visual Studio 2008
Environment
Computer Science Department
University of Alabama in Huntsville
2009
1
-
8/4/2019 Manual HowToRunVS2008
2/25
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
-
8/4/2019 Manual HowToRunVS2008
3/25
(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
-
8/4/2019 Manual HowToRunVS2008
4/25
-
8/4/2019 Manual HowToRunVS2008
5/25
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
5
-
8/4/2019 Manual HowToRunVS2008
6/25
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
6
-
8/4/2019 Manual HowToRunVS2008
7/25
(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.
7
-
8/4/2019 Manual HowToRunVS2008
8/25
-
8/4/2019 Manual HowToRunVS2008
9/25
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
9
-
8/4/2019 Manual HowToRunVS2008
10/25
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
-
8/4/2019 Manual HowToRunVS2008
11/25
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
-
8/4/2019 Manual HowToRunVS2008
12/25
-
8/4/2019 Manual HowToRunVS2008
13/25
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
-
8/4/2019 Manual HowToRunVS2008
14/25
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
-
8/4/2019 Manual HowToRunVS2008
15/25
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
-
8/4/2019 Manual HowToRunVS2008
16/25
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
-
8/4/2019 Manual HowToRunVS2008
17/25
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
-
8/4/2019 Manual HowToRunVS2008
18/25
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
-
8/4/2019 Manual HowToRunVS2008
19/25
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
-
8/4/2019 Manual HowToRunVS2008
20/25
Figure 1.64 Changing Project Property
a.7. Program stops at the breakpoint andvariables 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
-
8/4/2019 Manual HowToRunVS2008
21/25
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
-
8/4/2019 Manual HowToRunVS2008
22/25
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
-
8/4/2019 Manual HowToRunVS2008
23/25
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
-
8/4/2019 Manual HowToRunVS2008
24/25
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
-
8/4/2019 Manual HowToRunVS2008
25/25
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)