![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
Editing, Compiling, and Executing a C++ Program
- Create a C++ file
- Edit the C++ program
- Code and Link the C++ program.
Find and correct the errors and then link the program.
- Running the C++ Program
Create a C++ fileAll code must be in a subsystem and view.
Edit the C++ program
/* * * File Name: hello.C * */ #include "iostream.h" int main () {
couti << "Hello world!" << endl } /* End of hello.C */
The errors in the code are placed there on to help illustrate the error correction process..
Code and Link the C++ program
- 1. . Select the Compile:Code command
The Jobs window for the code command (graphics) is displayed.
The Message window for the code command (graphics) is displayed indicating there are 2 errors.
An Object viewer with errors from the code command displays the errors. After reviewing the errors and correcting them, use the File:Close command to close this window.
Note: Use Compile:Code regularly as you develop an object. It is a quick way to find errors and correct them. Once you have corrected the errors, you can use Compile:Code to code the object.
- 2. . Correct the errors in the code:
- 3. . Select the Compile:Link command
The Jobs window for the link command (graphics) displays the code job.
The Message window for the link command (graphics) displays a log of the command.
The executable is displayed in the project1.ss/hello.wrk directory viewer in the form hello*.
Note: If you have made changes to the file, Apex automatically saves the changes as part of the Code or Link operation.
- 4. . Select the File:Close command to close the C/C++ editor window (text and graphics).
Running the C++ Program
Controlling VersionsApex provides Summit/CM for version control using the checking files in and out mechanism.
Please complete the Editing, Compiling, and Executing a C++ Program section before preforming these steps.
Display CM attributes
Check out, modify, and check in
Debugging C++ CodeThe Rational Apex debugger provides a variety of facilities for analyzing the behavior of programs developed using Apex. After you start the debugger, program execution is under your control. You can cause the program to execute; you can cause program execution to stop and wait for you to request further progress; you can examine source and object; and you can perform other debugging functions.
The Rational Apex debugger provides a variety of facilities for analyzing the
behavior of programs developed using Apex. After you start the debugger, program
execution is under your control. You can cause the program to execute; you can cause
program execution to stop and wait for you to request further progress; you can
examine source and object; and you can perform other debugging functions.Setting Up the Debugger Tutorial
You must complete the debugger setup in order to perform the debugger exercises. This setup consists of entering the necessary C++ file and generating an executable named "main". For this debugger tutorial, we will use a new working view and C++ file.
Starting to Debug a Program
You are now going to run the area program with the debugger.
- 1. . Select (highlight) the area* object in the directory viewer window.
- 2. . Select the File:Debug command.
The Debug dialog box (graphics) is displayed
- 3. . Click on OK in Debug dialog box.
It will take a few moments for execution to get started. When the startup is complete, the following windows are displayed:
- The Debugger Main Window (text and graphics)
- A C/C++ Editor Window (graphics)
- The Input/Output Window (graphics), which is an xterm titled "area Input/Output"
- 4. . Close the C/C++ Editor window by selecting the Close button (or the File:Close command) on that window.
In this tutorial we will be using only the Debugger Window in Show Source mode.
- 5. . In the Debugger Window, select the View:Show Source command.
This causes the Source Pane (graphics) to be displayed.
Starting Execution
You are going to execute the area program. This program calculates the area of a rectangle.
- 1. . Move the Input/Output Window (graphics) so that you can view both the Input/Output window and the Debugger window.
- 2. . In the Debugger window, select Execution:Continue to execute the program.
The Debugger window displays a message indicating that the program has started running. While the program is executing, the menubar background is greyed out.
When execution finishes, the Debugger Window (graphics) Log area and status pane are updated. In the Input/Output Window (graphics), the number 12 appears. This is the output of the area program.
Setting Breakpoints
Breakpoints define predetermined stopping places in the program. Whenever the conditions associated with a breakpoint are met, the program stops executing, allowing you to perform any debugger function including displaying or modifying objects.
To understand what this program is doing, you are going to set a breakpoint.
- 1. . Position your cursor on the line:
- 2. . Issue a Debug:Break Here command.
The Debugger window displays a message indicating a breakpoint on the selected line and a red stop sign appears to the left of the line. Note that the line number may be slightly different depending on how many blank lines you used when you entered the program
![]()
The Debug:Break Here command can be used in most situations. If you want to specify additional options, you can enter Control-Debug:Break Here in an editor window or click the Debug:Break... command in any debugger window.
- 3. . Bring up the Breakpoints Window (graphics) by selecting the Windows:Breakpoints command from the Debugger window.
Continuing Execution
Now that you have set your first breakpoint, you want the program to execute until it reaches this breakpoint location. Execution will stop before the statement at the breakpoint location is executed.
- 1. . In the main debugger window, select Execution:Continue.
The Debugger window displays a message indicating that the program has started running. While the program is executing, the menubar background is greyed out.
When the breakpoint location is reached, execution stops and the following changes are visible in the Debugger Window (graphics):
- The source line containing the breakpoint is displayed in the Source Pane. It is marked with an arrow and a red stop sign.
- The Status Line at the bottom of the Debugger window displays a message indicating the program has stopped for a break.
Deleting Breakpoints
Now that you are stopped at breakpoint 1, breakpoint 1 can be deleted.
- 1. . Select the breakpoint 1 line in the Breakpoints window.
- 2. . Select Breakpoints:Remove.
The breakpoints window updates to show no breakpoints.
The Debugger window displays a message indicating the breakpoint has been removed.
- 3. . In the Breakpoints window, select File:Close because there are no breakpoints.
Stepping Through the Program
Stepping allows you to control when the execution of a function will be stopped. The Apex debugger considers a statement to be a C++ statement or declaration which has associated assembly code. Some declarations and statements are optimized away, and thus have no associated assembly code.
In this section, you will use several types of stepping available with the Rational debugger.
- Step Over
Steps to the next source line for which the compiler generates code, stepping over function calls
- Step Into
Steps into the top-level function without stopping in functions called to evaluate parameters
- Step Statement
Steps one line of source code, stepping into called functions, regardless of their containing files.
- Executes the cout << piece.area() function
- Stops at the next statement in the main function, the call to cout << "\n"
![]()
- 3. . To single step, in the Debugger window, select Execution:Step Statement.
The area.C file is displayed with the current position now on line 36 (cout << flush)
![]()
Displaying Objects
The Apex debugger allows displaying the current value of an object in the Debugger window. This is done using the Debug:Show command.
You are going to examine the value of the piece object using the Show Data dialog box.
- 1. . In the Debugger window, select Debug:Show...
The Show Data dialog box (graphics) is displayed.
- 2. . Enter the object name piece in the Expression field. Make sure the Show Location (graphics) button is selected so that the memory location is also displayed.
- 3. . Click on OK.
The Debugger window displays the current value of the piece object. It should look like this (note that the memory address will be different).
You are now going to examine the value of the piece.area() object using the simple Debug:Show command.
- 1. . Move the cursor to the piece.area() object and highlight the entire object.
- 2. . In the Debugger window, select Debug:Show or use the Show Hot Button.
The Debugger window displays the current value of the piece.area() object.
Modifying the Value of an Object
The Debug:Modify Data... command modifies the value of an object.
You want to change the value of piece.length.
- 1. . Click on the Debug:Modify Data... command.
The Modify Data dialog box (graphics) is displayed.
- 2. . In the Expression field, enter piece.length.
- 3. . In the New Value field, enter 10.
- 4. . In the filled in Modify Data dialog box (graphics), click OK.
The Debugger window displays a message indicating the value of piece.length has been modified.
Assembly Mode
It is also possible to view disassembled text in the Source Pane. When Assembly Mode is active, breakpoint and stepping commands are interpreted at the instruction level.
- 1. . Select either the Disassembly hot button or the File:Disassembly (graphics) command.
When disassembled text is displayed, the lines of source code are highlighted blue and the disassembly text is left in the normal color.
- 2. . Set a breakpoint at the current individual instruction in one of the following ways:
- Select the Debug:Break Here command
- Select the Debug:Break... command and complete the dialog box
- Position your cursor on the desired line in the Source Pane and enter the letter "b".
A message will be displayed in the Log area of the Debugger window and a stop sign will appear to the left of the line.
![]()
Exiting the Debugger
Cleaning Up
Select Cleaning Up to delete the files you have created during this tutorial.
Rational Software Corporation http://www.rational.com support@rational.com techpubs@rational.com Copyright © 1993-2001, Rational Software Corporation. All rights reserved. |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |