HPlogo HP C++ Programmer's Guide: HP 9000 Series Workstations and Servers > Chapter 2 The HP C++ Preprocessor

Preprocessing Directives

» 

Technical documentation

Complete book in PDF

 » Table of Contents

 » Index

This chapter presents information about the HP C++ preprocessor. If you are familiar with the HP C preprocessor described in the HP C/HP-UX Reference Manual, you may already be acquainted with some of this chapter's contents.

Overview

A preprocessor is a text processing program that manipulates the text within your source file. You enter preprocessing directives into your source file to direct the preprocessor to perform certain actions on the source file. For example, the preprocessor can replace tokens in the text, insert the contents of other files into the source file, or suppress the compilation of part of the file by conditionally removing sections of text. It also expands preprocessor macros and conditionally strips out comments.

The preprocessor program, Cpp.ansi, is invoked automatically when you compile your C++ source code. (You can use the -Ac option to invoke the compatibility mode preprocessor, Cpp.)

When the preprocessor is finished, your preprocessed C++ code is passed to the HP C++ compiler. For more information on the phases of the compiler see Chapter 3 “Compiling and Executing HP C++ Programs”, "Compiling and Executing HP C++ Programs."

HP C++ provides two modes of preprocessor operation: ANSI C mode and compatibility mode. ANSI C mode is the default. If you want the compatibility mode preprocessor, use the -Ac option of the CC command. Refer to "Compiling HP C Programs" in the HP C/HP-UX Reference Manual for further information on compatibility and ANSI C modes. Refer to "Compiling and Executing HP C++ Programs" in Chapter 3 “Compiling and Executing HP C++ Programs” of this manual for further information on CC options.

Syntax

preprocessor-directive ::=
     include-directive newline
     macro-directive newline
     conditional-directive newline
     line-directive newline
     pragma-directive newline
     error-directive newline

Preprocessing directives control the following general functions, each of which is discussed in subsequent sections:

  • source file inclusion

    You can direct HP C++ to include other source files at a given point. This is normally used to centralize declarations or to access standard system headers such as iostream.h.

  • macro replacement

    You can direct HP C++ to replace token sequences with other token sequences. In C, this is frequently used to define names for constants rather than explicitly putting the constant value into the source file. In C++ you can also use the keyword const to define constants.

  • conditional compilation

    You can direct HP C++ to check values and flags and to compile or skip source code based on the outcome of a comparison. This feature is useful in writing a single source that will be used for several different configurations.

  • line control

    You can direct HP C++ to set the line number and file name of the next line.

  • pragma directives

    You can direct HP C++ to give implementation-dependent instructions, called pragmas, to the compiler. Because they are system-dependent, pragmas are not portable.

  • error directives

    You can create diagnostic messages that will be produced by HP C++.

Using Preprocessor Directives

The following lists rules and guidelines for using preprocessor directives:

  • All preprocessing directives must begin with a pound sign (#) as the first character on a line of your source file. (However, if you are in ANSI C mode only, white-space characters may precede the # character.)

  • The # character is followed by any number of spaces and horizontal tab characters and the preprocessing directive.

  • The preprocessing directive is terminated by a newline character.

  • Preprocessing directives, as well as normal source lines, can be continued over several lines. End the lines that are to be continued with a backslash (\).

  • Some directives can take actual arguments or values.

  • Comments in the source file that are not passed through the preprocessor are replaced with a single white space character (ASCII character number decimal 32).

The following are examples of preprocessing directives:

include-directive:

#include <iostream.h>

macro-directive:

#define MAC x+y

conditional-directive:

#ifdef MAC

# define x 25

# endif

line-directive:

#line 5 "myfile"

pragma-directive:

#pragma OPTIMIZE ON

error-directive:

#error "FLAG not defined!"

© Hewlett-Packard Development Company, L.P.