2.2 Creating a Real Program

I l @ ve RuBoard

Before you can actually start creating your own programs, you need to know how to use the basic programming tools. This section will take you step by step through the process of entering, compiling, and running a simple program.

This section describes how to use two different types of compilers. The first type is the standalone or command-line compiler. This type of compiler is operated from the command line. You type a command, and the compiler turns your source code into an executable program. The other type of compiler is contained in an IDE.

Most Unix systems use command-line compilers. A few IDE-type compilers are available for Unix, but they are rare. On the other hand, almost all the compilers used with Microsoft Windows are part of an IDE. For command-line die-hards, these IDEs contain command-line compilers as well.

2.2.1 Creating a Program Using a Command-Line Compiler

In this section you'll go through the step-by-step process needed to create a program using a command-line compiler. The program you're going to create will display the message "Hello World" on the screen. Instruction is given for using a generic Unix compiler, the Free Software Foundation's g++ compiler, Borland C++, and Microsoft Visual C++.

However, if you are using a Borland or Microsoft compiler, you might want to skip ahead to Section 2.2.2.

Note that, because compilers are continually being improved, the information in this section may not be accurate by the time you read it. As new compilers come out, we'll update this section and post the update on the O'Reilly web site at http://www.oreilly.com/catalog/cplus2.

2.2.1.1 Step 1: Create a place for your program

It is easier to manage things if you create a separate directory for each program you are working on. In this case you'll create a directory called hello to hold your hello program.

In Unix, type:

 % mkdir hello % cd hello 

In MS-DOS, type:

 C:> MKDIR HELLO C:> CD HELLO 
2.2.1.2 Step 2: Create the program

A program starts out as a text file. Example 2-1 shows the hello program in source form.

Example 2-1. Source for the hello.cpp program
 #include <iostream> int main(  ) {     std::cout << "Hello World\n";     return (0); } 

Use your favorite text editor to enter the program. Your file should be named hello.cpp .

Do not use a word-processing program such as Microsoft Word or WordPerfect to write your programs. Word-processing programs add formatting codes to files that confuse the compiler. You must use a text editor, such as the notepad program, that is capable of editing ASCII files.

2.2.1.3 Step 3: Run the compiler

The compiler changes the source file you just created into an executable program. Each compiler has a different command line. The commands for the most popular compilers are listed below.

2.2.1.3.1 Unix CC Compiler (Generic Unix)

Most Unix-based compilers follow the same generic standard. The C++ compiler is named CC. To compile your hello program, you need the following command:

 %  CC -g -ohello hello.cpp  

The -g option enables debugging. (The compiler adds extra information to the program to make it easier to debug.) The switch -ohello tells the compiler that the program is to be called hello , and the final hello.cpp is the name of the source file. See your compiler manual for details on all the possible options. There are several different C++ compilers for Unix, so your command line may be slightly different than is shown here.

2.2.1.3.2 Free Software Foundation's g++ Compiler

The Free Software Foundation, the GNU people, publishes a number of high-quality programs. (See the glossary entry "Free Software Foundation" for information on how to get their software.) Among their offerings is a C++ compiler called g++.

To compile the hello program using the g++ compiler, use the following command line:

 %  g++ -g -Wall -ohello hello.cpp  

The additional switch -Wall turns on all the warnings. When warnings are turned on, the compiler will warn you when it sees questionable code.

2.2.1.3.3 Borland's Turbo C++

Borland International makes a free Microsoft Windows C++ compiler called Borland-C++. This compiler is ideal for learning. The command line for Borland-C++ is:

 C:>  bcc32 -v -N -w -tWC -ehello hello.cpp  

The -v switch tells Borland-C++ to put debugging information in the program. Warnings are turned on by -w and stack checking by -N . The -tWC option tells Borland-C++ to output a "Console Application." That's a program that uses the standard C++ API (as opposed to the Windows API) and uses a MS-DOS console window for its input and output. Finally, -ehello tells Borland-C++ to create a program named hello, and hello.cpp is the name of the source file. See the Borland-C++ reference manual for a complete list of options.

2.2.1.3.4 Microsoft Visual C++ .NET

Microsoft Visual C++ .NET is another C++ compiler for Microsoft Windows. To compile the HELLO program, use the following command line:

 C:>  cl /FeHELLO /GZ /RTCsuc /Zi /Wall hello.cpp  

The /FeHELLO option tells the program to generate a program named HELLO.exe . Runtime checking is enabled by /GZ and /RTCsuc , and debugging is turned on with the /Zi option. All warning messages are enabled by /Wall . [1]

[1] In the prerelease of Microsoft Visual Studio .NET, compilation with /Wall generated a large number of warnings caused by minor problems in Microsoft's own libraries. I expect these problems to be fixed in the production release of this code.

2.2.1.4 Step 4: Execute the program

Now, run the program by typing the following at the command prompt. (This works for both Unix and MS-DOS.)

  hello  

The message:

 Hello World 

will appear on the screen.

2.2.2 Creating a Program Using an Integrated Development Environment

The IDE provides a one-stop shop when it comes to programming. It take a compiler, editor, and debugger and wraps them into one neat package for the programmer. This package is presented inside a unified graphical interface that allows you to perform most program development operations with a few clicks of the mouse.

Since development environments tend to change, the particular version you use may operate slightly differently than is described in this chapter.

Each IDE is a little different, so we've included separate instructions for each one. (Note that compilers change much faster than books, so the information presented in these sections may be outdated . Check this book's page at the O'Reilly web site, http://www.oreilly.com/catalog/cplus2, for the latest information on compilation environments.)

2.2.2.1 Borland C++
  1. Create a directory called HELLO to hold the files for our hello program. You can create a directory using the Windows desktop tools or by typing the following command at the MS-DOS prompt:

     mkdir \HELLO 
  2. From Windows, double-click on the Borland C++ icon to start the IDE, or start the IDE using the "Start" menu. The program begins execution and displays a blank workspace, as shown in Figure 2-3.

    Figure 2-3. Borland C++ initial screen
    figs/c++2_0203.gif
  3. Select the File figs/u2192.gif New item to create a project for our program. Select Console Wizard as shown in Figure 2-4 and click OK.

    Figure 2-4. New Items selector
    figs/c++2_0204.gif
  4. The Console Wizard dialog appears as shown in Figure 2-5. Select C++ for Source Type and click OK.

    Figure 2-5. Project Options dialog box
    figs/c++2_0205.gif
  5. The initial editing window appears as shown in Figure 2-6.

    Figure 2-6. Initial editing window
    figs/c++2_0206.gif
  6. Add your code to the file Unit1.cpp . The resulting code should look like:

     #include <iostream> int main(  ) {     std::cout << "Hello World\n";     return (0); } 

    You can ignore the #pragma statements and comments that Borland-C++ has added. When you have finished, your screen will look like Figure 2-7.

    Figure 2-7. Hello program
    figs/c++2_0207.gif
  7. Compile and run the program by selecting the Debug figs/u2192.gif Run menu item. The program will run and display "Hello World" in a window, as shown in Figure 2-8.

    Figure 2-8. Hello program
    figs/c++2_0208.gif
2.2.2.2 Microsoft Visual C++
  1. From Windows, start Microsoft Visual Studio .NET. A start screen will be displayed, as shown in Figure 2-9.

    Figure 2-9. Microsoft Visual C++ initial screen
    figs/c++2_0209.gif
  2. Click on File figs/u2192.gif New figs/u2192.gif Project to bring up the New Project dialog shown in Figure 2-10.

    Figure 2-10. New Project dialog
    figs/c++2_0210.gif

    In the Template pane, select "Manage C++ Empty Project." Fill in the Name field with hello . Change the Location field to the directory in which you wish to build the project. Click OK. The project screen now appears as shown in Figure 2-11.

    Figure 2-11. Initial project screen
    figs/c++2_0211.gif
  3. In the Solution Explorer tab, select Source Files. Now create the program using the File figs/u2192.gif Add New Item menu. The Add New Item dialog appears as shown in Figure 2-12.

    Figure 2-12. Add New Item dialog
    figs/c++2_0212.gif
  4. Select C++ file as the file type and put hello.cpp in the name field. Click Done to bring up the editing window shown in Figure 2-13.

    Figure 2-13. Editing window
    figs/c++2_0213.gif
  5. Type the following lines into the hello.cpp window.

     #include <iostream> int main(  ) {     std::cout << "Hello World\n";     return (0); } 

    Your results should look like Figure 2-14.

    Figure 2-14. Completed program
    figs/c++2_0214.gif
  6. Compile the program by selecting Build figs/u2192.gif Build Hello. You may have to resize the windows to view the messages. If everything works, the screen should look something like Figure 2-15.

    Figure 2-15. Result of the build
    figs/c++2_0215.gif
  7. Run the program using the Debug figs/u2192.gif Start Program without Debugging window menu item. An MS-DOS window appears and the program is run within it. The results are shown in Figure 2-16.

    Figure 2-16. Sample run
    figs/c++2_0216.gif
I l @ ve RuBoard


Practical C++ Programming
Practical C Programming, 3rd Edition
ISBN: 1565923065
EAN: 2147483647
Year: 2003
Pages: 364

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net