cpp is the C language preprocessor. It is invoked as the first pass of any C compilation started with the cc(1B) command; however, cpp can also be used as a first-pass preprocessor for other Sun compilers.
Although cpp can be used as a macro processor, this is not normally recommended, as its output is geared toward that which would be acceptable as input to a compiler’s second pass. Thus, the preferred way to invoke cpp is through the cc(1B) command, or some other compilation command. For general-purpose macro-processing, see m4(1) , and the chapter on m4 in
cpp optionally accepts two filenames as arguments. input-file and output-file are, respectively, the input and output files for the preprocessor. They default to the standard input and the standard output.
All cpp directives start with a hash symbol (#) as the first character on a line. White space (SPACE or TAB characters) can appear after the initial # for proper indentation.
- The constant-expression in the preceding #if directive evaluated to zero, the name in the preceding #ifdef is not defined, or the name in the preceding #ifndef directive was defined.
- The constant-expression in all intervening #elif directives evaluated to zero.
- The current constant-expression evaluates to non-zero.
Formal parameters for macros are recognized in #define directive bodies, even when they occur inside character constants and quoted strings. For instance, the output from:
#define abc(a) |‘|a|abc(xyz)
is:
# 1 "" |‘|xyz|
The second line is a NEWLINE . The last seven characters are ‘‘|‘|xyz|’’ (vertical-bar, backquote, vertical-bar, x, y, z, vertical-bar). Macro names are not recognized within character constants or quoted strings during the regular scan. Thus:
#define abc xyzprintf("abc");
does not expand abc in the second line, since it is inside a quoted string that is not part of a #define macro definition.
Macros are not expanded while processing a #define or #undef. Thus:
#define abc zingo#define xyz abc#undef abcxyz
produces abc. The token appearing immediately after an #ifdef or #ifndef is not expanded.
Macros are not expanded during the scan which determines the actual parameters to another macro call. Thus:
#define reverse(first,second)second first#define greeting helloreverse(greeting,#define greeting goodbye)
produces ‘‘ #define hello goodbye hello’’.
Output consists of a copy of the input file, with modifications, plus lines of the form:
indicating the original source line number and filename of the following output line and whether this is the first such line after an include file has been entered (level=1), the first such line after an include file has been exited (level=2), or any other such line (level is empty).
#include files are searched for in the following order:
.
- The directory of the file that contains the #include request (that is, #include is relative to the file being scanned when the request is made).
.- The directories specified by -I options, in left-to-right order.
.- The standard directory(s) (/usr/include on UNIX systems).
Two special names are understood by cpp. The name __LINE __ is defined as the current line number (a decimal integer) as known by cpp, and __FILE __ is defined as the current filename (a C string) as known by cpp. They can be used anywhere (including in macros) just as any other defined name.
A NEWLINE character terminates a character constant or quoted string. An escaped NEWLINE (that is, a backslash immediately followed by a NEWLINE ) may be used in the body of a #define statement to continue the definition onto the next line. The escaped NEWLINE is not included in the macro value.
Comments are removed (unless the -C option is used on the command line). Comments are also ignored, except that a comment terminates a token.
The error messages produced by cpp are intended to be self-explanatory. The line number and filename where the error occurred are printed along with the diagnostic.
When NEWLINE
characters were found in argument
lists for macros to be expanded, some previous versions of cpp put out
the NEWLINE
characters as they were found and expanded. The current version
of cpp replaces them with SPACE
characters.
Because the standard directory for included files may be different in different environments, this form of #include directive:
should be used, rather than one with an absolute path, like:
cpp warns about the use of the absolute pathname.
While the compiler allows 8-bit strings and comments, 8-bits are not allowed anywhere else.