Part 1. Configuring a New Application
Before you begin writing code, you need to provide some information so that VisualAge
C++ can configure your program's build environment properly. In this section, you will use
the VisualAge C++ Integrated Development Environment (IDE) to do the following:
- Create a project for your program. A VisualAge C++ project groups together the
source files and actions needed to produce an application or part of an application. To
work with your application in the IDE, it must be defined as a project.
- Add a target file, source files, and class library files to your project.
- Create a source file with a simple main function.
- Build and run your program.
This section should take you approximately 30 minutes to complete. You will learn to
perform the following IDE tasks:
- Use the three SmartGuides that lead you through creating the configuration for your
project.
- Perform an initial build of your project.
- Find error information after an unsuccessful build.
- Open a view of an existing source file.
- Add a main function to your project.
- Rebuild your project.
Create a Project and Add Files to It
Follow these steps to create your project:
- Start the VisualAge C++ IDE.
- Choose to create a new project in one of the following two ways:
- From the VisualAge C++ startup screen, select the Create a New Project
radio button and click OK.
- If no startup screen appears, or if you cancelled the startup screen, select Project
Workbook - Create Project... from the menu bar.
- The Project SmartGuide will guide you through creating the project.
- In the Project SmartGuide, click the Next button if you are not already
at the Project Configuration screen of the SmartGuide.
- Specify review.icc as the project name. If you do not enter the file-name
extension .icc, the SmartGuide will add it for you.
The .icc file is the
configuration file for a project. It is similar to a makefile in that it contains a list
of the source files and target executables for a project, along with options used to build
the application.
- Specify a directory in which to store the project files. For example, if you want
to store the tutorial project files on drive F:, you might enter F:\vatutor
as the directory. If the directory you specify does not exist, it will be created
for you.
For the remainder of this tutorial, wherever you see the path F:\vatutor, you
can substitute the path you specify in this step.
- Click Next. The Target Type screen appears. On this screen you add
targets to your project. Make sure Executable(EXE) file is the current
selection in the New target field, and click Add target.
- The Target SmartGuide appears. The Target SmartGuide guides you through defining your
target. Click the Next button until you get to the Target Name screen.
- On the Target Name screen, enter the name and destination directory for your target. For
the CGI example, name your target review.exe and specify the target directory
as the directory from which CGIs run on your web server.
- If you are using the VisualAge Help Server to try out the tutorial, this directory is on
the same drive where VisualAge was installed, and is normally \IMNNQ_NT on Windows NT, or
:\NETQOS2 on OS/2.
- If you are using Peer Web Services on Windows NT, this directory is normally
\inetpub\scripts.
- You can also select a program type for your application, which predefines certain
compiler and linker options. In the Program Type column, select WIN
IOC or OS/2 IOC depending on your operating system. These two
choices set project options so that your project can use IBM Open Class Library classes
and member functions.
- Click Next to go to the Parts Type screen. Do not add any parts,
because you will not be creating any visual or data access parts for this tutorial. (A
separate tutorial section, Develop a Graphical User Interface from
a Visual Part, teaches you how to work with visual parts. Information on data access
parts is available in the online help starting at Data
Access Builder.) Click Next again to go to the Source Files screen.
- The Source Files screen helps you create the part of the configuration file that
specifies which source files are used to build the target. Enter the source file name main.cpp
in the Files to Add or Create text entry field (remove any current path
information from that field). Select the Relative Path check box, so that
the files you add are placed in the default directory for your project. Then click Add>>.
- The SmartGuide asks if you want the file to be created. Click Yes. An
empty main.cpp file is created.
- Click OK to close the Target SmartGuide and return to the Target Type
screen of the Project SmartGuide.
- You will see the target you just defined in the list at the bottom of the Target Type
screen. Click Next. The last Project SmartGuide screen lets you specify
help files. Since you will not add help files for this exercise you can close the Project
SmartGuide by clicking the Finish button.
- When the IDE asks if you want to open the project, click Yes.
- When the IDE asks if you want to build the project, click Yes.
You will encounter a build error, because you have not yet coded a main function for
your program. A Help Tips error dialog will appear if you have not encountered a build
error before. If the dialog appears, click OK to go to the Messages pane.
If no dialog appears and you are not automatically taken to the Message pane, click the
tab for the Project section and open the Messages page.
Add a Simple main Function to main.cpp
- Make sure that the Project section of the workbook is selected.
- Select the Source Files page.
- Select the
main.cpp object for the main.cpp source
file in the Source Files view in the upper left pane. Notice that when you select this
file, the source view underneath changes to display the content of main.cpp, which is
currently empty.
- Click anywhere inside the Source view. Enter the following code for main into
it. If you are viewing the tutorial online, copy the code from your browser window and
paste it into the Source view.
Note that all comments shown in this and other source
code in the tutorial are for your information; you do not have to type them in. Also, code
is colored in this tutorial to reflect the colors you will see in Source views within the
IDE.
#include <fstream.h> // for use of cout statement (and file i/o later)
int main() {
cout << "Content-Type: text/plain\n\nHello world!" << endl;
return 0;
}
| Implementation Details: The output begins with a header ("Content-Type:
text/plain\n\n"), which is the CGI header that instructs a user's browser to
display the subsequent text as plain unformatted text. This header will display when you
run the program from a command shell, but will not display when you start the program from
a browser. |
Your project is now ready to build.
Build and Run Your Program
Now that you have added content to your source file, you can build and run the project.
- To start the build, click the Build button (
) on the
toolbar or press Ctrl+Shift+B. The IDE automatically saves any files you
have opened and added to the project, then starts building the project.
- If any errors occurred during the build, the Messages page appears. (If it does not
appear, but the status area indicates build errors, go to the Messages page in the Project
section.) Select the error in the Messages pane; the view below this pane shows the source
code for the object containing the error, with the cursor on the line containing the
error. Correct any source code errors, then build again.
- You can run your project from a command shell or from a web browser on your workstation:
- In the command shell, change to the directory you specified as the target directory (the
directory from which your CGIs run) and type the executable file name, review.exe (OS/2 or
Windows) or review.cgi (AIX).
- From a browser, assuming you are using the VisualAge Help Server as your web server,
enter the following location:
http://localhost:49213/cgi-bin/review.exe
If you are browsing this tutorial online, click the link, then use the Back button on the
browser to return to this page. If there is no response, the help server may not be
running. Select any of the choices from the Help menu in the IDE to start
the VisualAge Help Server. Once the help server is running you can try the link above.
- If you ran the program from the command shell, you will see:
Content-Type: text/plain
Hello world!
If you ran the program from a web browser, the browser interprets the Content-Type
header, and displays only the text Hello world!
You can now proceed to the following parts of the tutorial:
- Part 2. Modifying Configuration Options, in which you will learn
about your project configuration and how to change the options that apply to your project,
source files, or target file. In this part you will change the target CPU architecture for
your executable file. This provides a quick illustration of how easily you can change
options in the Configuration section of the workbook. (15 minutes.)
- Part 3. Declaring and Implementing a Class, in which you will
define a request class for use with CGIs, and will learn more about defining classes and
member functions and using pages and views in the Project section of the workbook. (90
minutes.)
You may also want to explore some of the IDE concepts in more detail. You can read the
following topics in Tour the VisualAge C++ IDE if you have not done so already. Each topic
should take you five minutes or less.