TOC PREV NEXT INDEX DOC LIST MASTER INDEX



Editing and Compiling a C++ Program (Apex/Summit)

This module guides you through the steps needed to create, edit, compile, and run a simple C++ program. All Apex components (code, documentation, etc) must be in subsystems and views.

This module has the following topics. To move to the first topic, click on it or click on the Next button.

Prerequisites

The Topics in this Module

Average Time to Complete this Module

90 minutes.


Creating Subsystems and C++ Views

Apex supports the dividing of a software-development effort into high-level components called subsystems, each containing a group of logically related objects. Subsystems let you group related files, ada packages and other compilation units together, hiding those that contain only implementation details.

Every subsystem is associated with a set of views. Working views provide areas for the development and integration of software. Release views contain the products developed within a subsystem during the development process.

The average time to complete this module is 10 minutes, but your rate of speed will depend on how many side trips you take.

Note: In UNIX, a subsystem is a directory with a .ss filename extension and a view is a subdirectory with a .wrk or .rel extension. Because subsystems and views are directories in UNIX, their names are case-sensitive. In the following exercises, you can use whatever directory names you want for parent directories, but use the subsystem and view names that are provided, typed in all-lowercase letters.


Creating a Subsystem

In this exercise, you will create a subsystem named project1.ss. If you have already created this subsystem, open a viewer with this subsystem and skip to the Creating a C++ View topic.

1. . Open a directory-viewer window.

2. . Choose the New command from the File menu. Apex displays a cascading menu.

3. . Choose the New Subsystem menu item from the New submenu. Apex displays the New Subsystem dialog box:

4. . Enter the full pathname of the subsystem you wish to create in the Subsystem Full Name field. The .ss extension is added for you automatically. For this exercise, use $HOME/project1.

5. . With the Place storage in subsystem's enclosing directory and the Visit the new subsystem check boxes selected the default settings), close the New Subsystem dialog box by clicking the OK button.

When the dialog box closes with the Visit the new subsystem check box selected, Apex opens a directory-viewer window showing you the subsystem that was created.

See File:New for more information.


Creating a C++ View

In this exercise, you will create a working view named hello_c.wrk. The hello_c.wrk view will be nested in the project1.ss subsystem you created in the previous exercise.

1. . Select the New command from the File menu in the project1.ss directory viewer window. Apex opens a cascading menu.

2. . Choose the New View menu item from the New submenu. Apex displays the New View dialog box:

3. . In the Name field, type the view name hello_c.

4. . Keep the View Type set to Working and the Language set to C/C++.

5. . If Apex is correctly installed, the text in the Host Architecture and Model fields match the characteristics of your workstation. Leave this field at its default setting.

6. . Leave the Visit the new view check box selected, its default setting.

7. . Leave all the other controls in the New View dialog box set to their default settings.

8. . Close the New View box and create your new view by clicking the OK button.

The Jobs window displays the view being created.

When the New View dialog box is executed with the Visit the new view check box selected, Apex opens a directory-viewer window showing you that a working view named hello_c.wrk has been created in the project1.ss subsystem.

9. . Choose the Close command from the File menu in the project1.ss directory-viewer window because you are going to be using the view window from now on.

The Online Reference contains additional information on the contents of an empty view.


Creating a C++ Function

Perform the following steps from the directory viewer displaying the working view (project1.ss/hello_c.wrk should be the first line in the directory viewer) you created in the previous topic. If you do not have it displayed, execute the File > Open command and enter $HOME/project1.ss/hello_c.wrk.

1. . Select the New command from the File menu in the hello_c.wrk directory-viewer window. Apex opens a cascading menu.

2. . Choose the New C++ menu item from the New submenu. Apex displays the New C++ dialog box. Notice that the pathname of the hello_c.wrk view appears in the Context field.

3. . Change the settings of the buttons labeled Create an empty C++ from Class and Header to Main and Implementation, as shown below:

4. . Enter the name hello in the Name field and hello in the Filename field, as shown in the preceding illustration. hello will be the name of the C++ file you are creating. It is not necessary to add any filename extensions. As you develop the hello application, Apex will create and use filenames based on the simple name hello, as appropriate.

Note: When you specify main in New C++ dialog box, the function name is main, regardless of what you specify in the Name field.

5. . With the Visit It option selected, execute the New C++ dialog box by clicking the OK button.

When you complete step five, Apex creates a C++ main function named hello.C and displays it in a C++ editor window as illustrated below. The empty file contents is defined by the corresponding profile file.


Editing the C++ Function

The C/C++ Editor Window

The C/C++ editor window is where you write, edit, compile, and link code. As the preceding illustration shows, the hello.C window created by Apex contains a comment line identifying the file, along with a stub that is ready for you to fill in.

If you open a directory-viewer window that lists the contents of the project1.ss/hello_c.wrk view, you will see that hello_c.wrk now contains the hello.C source file:

If you were to create other files for the hello application, Apex will automatically give all include files the .h extension and all C++ implementation files the .C extension. Apex creates and applies these extensions automatically when you create the file.

The Online Reference contains more information on the File > New command.

The display area of the editor window for hello.C looks like this:

To create the main function, follow these steps:

1. . Use the C/C++ editor window containing the hello.C file, which you created in the previous exercise.

2. . Position the cursor on the blank line after the first */ and type #include "iostream.h"

3. . Select the int argc, char* argv[] arguments and delete them.

4. . In the line after the opening brace ({), type the line:

Note: The errors in the above line are placed there on purpose; couti should be cout, and the statement needs a statement terminator (:). These errors will be corrected later in this module.

You can see an example of what hello.C looks at this point.

Basic tips for using the editor:

There are many editor key bindings.


Compiling and Linking the C++ Function

As you use different Apex commands to compile the C++ function, you progress the unit through different compilation states. The compilation states are listed below:
Compilation State
Description
Associated Command
Coded
Object code has been generated for the object.
Compile > Code
Linked
An executable has been generated for the object.
Compile > Link

If you skip the code compilation step, Apex performs the intermediate step needed to reach the compilation state associated with the command. For the purpose of this exercise, you will take each step in turn.

1. . Select Compile > Code.

This command checks the code in this object for errors and if no errors are found, object code is generated.

The Message window displays this message:

An Object viewer displays the errors.

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 error in the code. The corrected line should look like this:

Note: Identifiers in C/C++ are case-sensitive.

3. . Use Compile > Code command to code this code.

This command generates object code for the object.

The Jobs window displays the code job.

The Message window displays a log of the command. The object is promoted to the coded compilation state.

4. . Select Compile > Link to create an executable.

The Jobs window displays the link job.

The Message window displays a log of the command. The unit is promoted to the linked state.

The executable is displayed in the project1.ss/hello_c.wrk directory viewer in the form hello*.

As mentioned before, if you skip one or more compilation steps, Apex performs the intermediate steps needed to reach the compilation state associated with the command. If you have made changes to the object since the last compilation step, Apex automatically saves the changes as part of the operation.


Executing the C++ Function

1. .  Select the executable in the directory viewer (click on it with the mouse pointer).

If the directory viewer displaying the view hello_c.wrk is covered, you can raise it by selecting Navigate > Context in the language editor.

2. . Select File > Run to display the Run dialog box.

3. . Click on OK to execute the procedure.

The Job window displays the execute job.

The Output window prints Hello, Apex!

You have just created and executed a C++ program.

Close all the windows you have created during this module.


Rational Software Corporation 
http://www.rational.com
support@rational.com
techpubs@rational.com
Copyright © 1993-2001, Rational Software Corporation. All rights reserved.
TOC PREV NEXT INDEX DOC LIST MASTER INDEX TECHNOTES APEX TIPS