|
|
1.1 ! root 1: \input texinfo ! 2: @setfilename cpp.info ! 3: @settitle The C Preprocessor ! 4: ! 5: @ignore ! 6: @ifinfo ! 7: @format ! 8: START-INFO-DIR-ENTRY ! 9: * Cpp: (cpp). The C preprocessor. ! 10: END-INFO-DIR-ENTRY ! 11: @end format ! 12: @end ifinfo ! 13: @end ignore ! 14: ! 15: @c @smallbook ! 16: @c @cropmarks ! 17: @c @finalout ! 18: @setchapternewpage odd ! 19: @ifinfo ! 20: This file documents the GNU C Preprocessor. ! 21: ! 22: Copyright (C) 1987, 1989, 1991 Free Software Foundation, Inc. ! 23: ! 24: Permission is granted to make and distribute verbatim copies of ! 25: this manual provided the copyright notice and this permission notice ! 26: are preserved on all copies. ! 27: ! 28: @ignore ! 29: Permission is granted to process this file through Tex and print the ! 30: results, provided the printed document carries copying permission ! 31: notice identical to this one except for the removal of this paragraph ! 32: (this paragraph not being relevant to the printed manual). ! 33: ! 34: @end ignore ! 35: Permission is granted to copy and distribute modified versions of this ! 36: manual under the conditions for verbatim copying, provided also that ! 37: the entire resulting derived work is distributed under the terms of a ! 38: permission notice identical to this one. ! 39: ! 40: Permission is granted to copy and distribute translations of this manual ! 41: into another language, under the above conditions for modified versions. ! 42: @end ifinfo ! 43: ! 44: @titlepage ! 45: @c @finalout ! 46: @title{The C Preprocessor} ! 47: @subtitle{Last revised July 1990} ! 48: @subtitle{for GCC version 2} ! 49: @author{Richard M. Stallman} ! 50: @page ! 51: @vskip 2pc ! 52: This booklet is eventually intended to form the first chapter of a GNU ! 53: C Language manual. ! 54: ! 55: @vskip 0pt plus 1filll ! 56: Copyright @copyright{} 1987, 1989, 1991 Free Software Foundation, Inc. ! 57: ! 58: Permission is granted to make and distribute verbatim copies of ! 59: this manual provided the copyright notice and this permission notice ! 60: are preserved on all copies. ! 61: ! 62: Permission is granted to copy and distribute modified versions of this ! 63: manual under the conditions for verbatim copying, provided also that ! 64: the entire resulting derived work is distributed under the terms of a ! 65: permission notice identical to this one. ! 66: ! 67: Permission is granted to copy and distribute translations of this manual ! 68: into another language, under the above conditions for modified versions. ! 69: @end titlepage ! 70: @page ! 71: ! 72: @node Top, Global Actions,, (DIR) ! 73: @chapter The C Preprocessor ! 74: ! 75: The C preprocessor is a @dfn{macro processor} that is used automatically by ! 76: the C compiler to transform your program before actual compilation. It is ! 77: called a macro processor because it allows you to define @dfn{macros}, ! 78: which are brief abbreviations for longer constructs. ! 79: ! 80: The C preprocessor provides four separate facilities that you can use as ! 81: you see fit: ! 82: ! 83: @itemize @bullet ! 84: @item ! 85: Inclusion of header files. These are files of declarations that can be ! 86: substituted into your program. ! 87: ! 88: @item ! 89: Macro expansion. You can define @dfn{macros}, which are abbreviations ! 90: for arbitrary fragments of C code, and then the C preprocessor will ! 91: replace the macros with their definitions throughout the program. ! 92: ! 93: @item ! 94: Conditional compilation. Using special preprocessor commands, you ! 95: can include or exclude parts of the program according to various ! 96: conditions. ! 97: ! 98: @item ! 99: Line control. If you use a program to combine or rearrange source files into ! 100: an intermediate file which is then compiled, you can use line control ! 101: to inform the compiler of where each source line originally came from. ! 102: @end itemize ! 103: ! 104: C preprocessors vary in some details. This manual discusses the GNU C ! 105: preprocessor, the C Compatible Compiler Preprocessor. The GNU C ! 106: preprocessor provides a superset of the features of ANSI Standard C. ! 107: ! 108: ANSI Standard C requires the rejection of many harmless constructs commonly ! 109: used by today's C programs. Such incompatibility would be inconvenient for ! 110: users, so the GNU C preprocessor is configured to accept these constructs ! 111: by default. Strictly speaking, to get ANSI Standard C, you must use the ! 112: options @samp{-trigraphs}, @samp{-undef} and @samp{-pedantic}, but in ! 113: practice the consequences of having strict ANSI Standard C make it ! 114: undesirable to do this. @xref{Invocation}. ! 115: ! 116: @menu ! 117: * Global Actions:: Actions made uniformly on all input files. ! 118: * Commands:: General syntax of preprocessor commands. ! 119: * Header Files:: How and why to use header files. ! 120: * Macros:: How and why to use macros. ! 121: * Conditionals:: How and why to use conditionals. ! 122: * Combining Sources:: Use of line control when you combine source files. ! 123: * Other Commands:: Miscellaneous preprocessor commands. ! 124: * Output:: Format of output from the C preprocessor. ! 125: * Invocation:: How to invoke the preprocessor; command options. ! 126: * Concept Index:: Index of concepts and terms. ! 127: * Index:: Index of commands, predefined macros and options. ! 128: @end menu ! 129: ! 130: @node Global Actions, Commands, Top, Top ! 131: @section Transformations Made Globally ! 132: ! 133: Most C preprocessor features are inactive unless you give specific commands ! 134: to request their use. (Preprocessor commands are lines starting with ! 135: @samp{#}; @pxref{Commands}). But there are three transformations that the ! 136: preprocessor always makes on all the input it receives, even in the absence ! 137: of commands. ! 138: ! 139: @itemize @bullet ! 140: @item ! 141: All C comments are replaced with single spaces. ! 142: ! 143: @item ! 144: Backslash-Newline sequences are deleted, no matter where. This ! 145: feature allows you to break long lines for cosmetic purposes without ! 146: changing their meaning. ! 147: ! 148: @item ! 149: Predefined macro names are replaced with their expansions ! 150: (@pxref{Predefined}). ! 151: @end itemize ! 152: ! 153: The first two transformations are done @emph{before} nearly all other parsing ! 154: and before preprocessor commands are recognized. Thus, for example, you ! 155: can split a line cosmetically with Backslash-Newline anywhere (except ! 156: when trigraphs are in use; see below). ! 157: ! 158: @example ! 159: /* ! 160: */ # /* ! 161: */ defi\ ! 162: ne FO\ ! 163: O 10\ ! 164: 20 ! 165: @end example ! 166: ! 167: @noindent ! 168: is equivalent into @samp{#define FOO 1020}. You can split even an escape ! 169: sequence with Backslash-Newline. For example, you can split @code{"foo\bar"} ! 170: between the @samp{\} and the @samp{b} to get ! 171: ! 172: @example ! 173: "foo\\ ! 174: bar" ! 175: @end example ! 176: ! 177: @noindent ! 178: This behavior is unclean: in all other contexts, a Backslash can be ! 179: inserted in a string constant as an ordinary character by writing a double ! 180: Backslash, and this creates an exception. But the ANSI C standard requires ! 181: it. (Strict ANSI C does not allow Newlines in string constants, so they ! 182: do not consider this a problem.) ! 183: ! 184: But there are a few exceptions to all three transformations. ! 185: ! 186: @itemize @bullet ! 187: @item ! 188: C comments and predefined macro names are not recognized inside a ! 189: @samp{#include} command in which the file name is delimited with ! 190: @samp{<} and @samp{>}. ! 191: ! 192: @item ! 193: C comments and predefined macro names are never recognized within a ! 194: character or string constant. (Strictly speaking, this is the rule, ! 195: not an exception, but it is worth noting here anyway.) ! 196: ! 197: @item ! 198: Backslash-Newline may not safely be used within an ANSI ``trigraph''. ! 199: Trigraphs are converted before Backslash-Newline is deleted. If you ! 200: write what looks like a trigraph with a Backslash-Newline inside, the ! 201: Backslash-Newline is deleted as usual, but it is then too late to ! 202: recognize the trigraph. ! 203: ! 204: This exception is relevant only if you use the @samp{-trigraphs} ! 205: option to enable trigraph processing. @xref{Invocation}. ! 206: @end itemize ! 207: ! 208: @node Commands, Header Files, Global Actions, Top ! 209: @section Preprocessor Commands ! 210: ! 211: @cindex preprocessor commands ! 212: @cindex commands ! 213: Most preprocessor features are active only if you use preprocessor commands ! 214: to request their use. ! 215: ! 216: Preprocessor commands are lines in your program that start with @samp{#}. ! 217: The @samp{#} is followed by an identifier that is the @dfn{command name}. ! 218: For example, @samp{#define} is the command that defines a macro. ! 219: Whitespace is also allowed before and after the @samp{#}. ! 220: ! 221: The set of valid command names is fixed. Programs cannot define new ! 222: preprocessor commands. ! 223: ! 224: Some command names require arguments; these make up the rest of the command ! 225: line and must be separated from the command name by whitespace. For example, ! 226: @samp{#define} must be followed by a macro name and the intended expansion ! 227: of the macro. ! 228: ! 229: A preprocessor command cannot be more than one line in normal circumstances. ! 230: It may be split cosmetically with Backslash-Newline, but that has no effect ! 231: on its meaning. Comments containing Newlines can also divide the command into ! 232: multiple lines, but the comments are changed to Spaces before the command ! 233: is interpreted. The only way a significant Newline can occur in a preprocessor ! 234: command is within a string constant or character constant. Note that ! 235: most C compilers that might be applied to the output from the preprocessor ! 236: do not accept string or character constants containing Newlines. ! 237: ! 238: The @samp{#} and the command name cannot come from a macro expansion. For ! 239: example, if @samp{foo} is defined as a macro expanding to @samp{define}, ! 240: that does not make @samp{#foo} a valid preprocessor command. ! 241: ! 242: @node Header Files, Macros, Commands, Top ! 243: @section Header Files ! 244: ! 245: @cindex header file ! 246: A header file is a file containing C declarations and macro definitions ! 247: (@pxref{Macros}) to be shared between several source files. You request ! 248: the use of a header file in your program with the C preprocessor command ! 249: @samp{#include}. ! 250: ! 251: @menu ! 252: * Header Uses:: What header files are used for. ! 253: * Include Syntax:: How to write @samp{#include} commands. ! 254: * Include Operation:: What @samp{#include} does. ! 255: * Once-Only:: Preventing multiple inclusion of one header file. ! 256: * Inheritance:: Including one header file in another header file. ! 257: @end menu ! 258: ! 259: @node Header Uses, Include Syntax, Header Files, Header Files ! 260: @subsection Uses of Header Files ! 261: ! 262: Header files serve two kinds of purposes. ! 263: ! 264: @itemize @bullet ! 265: @item ! 266: @findex system header files ! 267: System header files declare the interfaces to parts of the operating ! 268: system. You include them in your program to supply the definitions and ! 269: declarations you need to invoke system calls and libraries. ! 270: ! 271: @item ! 272: Your own header files contain declarations for interfaces between the ! 273: source files of your program. Each time you have a group of related ! 274: declarations and macro definitions all or most of which are needed in ! 275: several different source files, it is a good idea to create a header ! 276: file for them. ! 277: @end itemize ! 278: ! 279: Including a header file produces the same results in C compilation as ! 280: copying the header file into each source file that needs it. But such ! 281: copying would be time-consuming and error-prone. With a header file, the ! 282: related declarations appear in only one place. If they need to be changed, ! 283: they can be changed in one place, and programs that include the header file ! 284: will automatically use the new version when next recompiled. The header ! 285: file eliminates the labor of finding and changing all the copies as well as ! 286: the risk that a failure to find one copy will result in inconsistencies ! 287: within a program. ! 288: ! 289: The usual convention is to give header files names that end with @file{.h}. ! 290: ! 291: @node Include Syntax, Include Operation, Header Uses, Header Files ! 292: @subsection The @samp{#include} Command ! 293: ! 294: @findex #include ! 295: Both user and system header files are included using the preprocessor ! 296: command @samp{#include}. It has three variants: ! 297: ! 298: @table @code ! 299: @item #include <@var{file}> ! 300: This variant is used for system header files. It searches for a file ! 301: named @var{file} in a list of directories specified by you, then in a ! 302: standard list of system directories. You specify directories to ! 303: search for header files with the command option @samp{-I} ! 304: (@pxref{Invocation}). The option @samp{-nostdinc} inhibits searching ! 305: the standard system directories; in this case only the directories ! 306: you specify are searched. ! 307: ! 308: The parsing of this form of @samp{#include} is slightly special ! 309: because comments are not recognized within the @samp{<@dots{}>}. ! 310: Thus, in @samp{#include <x/*y>} the @samp{/*} does not start a comment ! 311: and the command specifies inclusion of a system header file named ! 312: @file{x/*y}. Of course, a header file with such a name is unlikely to ! 313: exist on Unix, where shell wildcard features would make it hard to ! 314: manipulate.@refill ! 315: ! 316: The argument @var{file} may not contain a @samp{>} character. It may, ! 317: however, contain a @samp{<} character. ! 318: ! 319: @item #include "@var{file}" ! 320: This variant is used for header files of your own program. It ! 321: searches for a file named @var{file} first in the current directory, ! 322: then in the same directories used for system header files. The ! 323: current directory is the directory of the current input file. It is ! 324: tried first because it is presumed to be the location of the files ! 325: that the current input file refers to. (If the @samp{-I-} option is ! 326: used, the special treatment of the current directory is inhibited.) ! 327: ! 328: The argument @var{file} may not contain @samp{"} characters. If ! 329: backslashes occur within @var{file}, they are considered ordinary text ! 330: characters, not escape characters. None of the character escape ! 331: sequences appropriate to string constants in C are processed. Thus, ! 332: @samp{#include "x\n\\y"} specifies a filename containing three ! 333: backslashes. It is not clear why this behavior is ever useful, but ! 334: the ANSI standard specifies it. ! 335: ! 336: @item #include @var{anything else} ! 337: This variant is called a @dfn{computed #include}. Any @samp{#include} ! 338: command whose argument does not fit the above two forms is a computed ! 339: include. The text @var{anything else} is checked for macro calls, ! 340: which are expanded (@pxref{Macros}). When this is done, the result ! 341: must fit one of the above two variants---in particular, the expanded ! 342: text must in the end be surrounded by either quotes or angle braces. ! 343: ! 344: This feature allows you to define a macro which controls the file name ! 345: to be used at a later point in the program. One application of this ! 346: is to allow a site-configuration file for your program to specify the ! 347: names of the system include files to be used. This can help in ! 348: porting the program to various operating systems in which the ! 349: necessary system header files are found in different places. ! 350: @end table ! 351: ! 352: @node Include Operation, Once-Only, Include Syntax, Header Files ! 353: @subsection How @samp{#include} Works ! 354: ! 355: The @samp{#include} command works by directing the C preprocessor to scan ! 356: the specified file as input before continuing with the rest of the current ! 357: file. The output from the preprocessor contains the output already ! 358: generated, followed by the output resulting from the included file, ! 359: followed by the output that comes from the text after the @samp{#include} ! 360: command. For example, given two files as follows: ! 361: ! 362: @example ! 363: /* File program.c */ ! 364: int x; ! 365: #include "header.h" ! 366: ! 367: main () ! 368: @{ ! 369: printf (test ()); ! 370: @} ! 371: ! 372: ! 373: /* File header.h */ ! 374: char *test (); ! 375: @end example ! 376: ! 377: @noindent ! 378: the output generated by the C preprocessor for @file{program.c} as input ! 379: would be ! 380: ! 381: @example ! 382: int x; ! 383: char *test (); ! 384: ! 385: main () ! 386: @{ ! 387: printf (test ()); ! 388: @} ! 389: @end example ! 390: ! 391: Included files are not limited to declarations and macro definitions; those ! 392: are merely the typical uses. Any fragment of a C program can be included ! 393: from another file. The include file could even contain the beginning of a ! 394: statement that is concluded in the containing file, or the end of a ! 395: statement that was started in the including file. However, a comment or a ! 396: string or character constant may not start in the included file and finish ! 397: in the including file. An unterminated comment, string constant or ! 398: character constant in an included file is considered to end (with an error ! 399: message) at the end of the file. ! 400: ! 401: The line following the @samp{#include} command is always treated as a ! 402: separate line by the C preprocessor even if the included file lacks a final ! 403: newline. ! 404: ! 405: @node Once-Only, Inheritance, Include Operation, Header Files ! 406: @subsection Once-Only Include Files ! 407: @cindex repeated inclusion ! 408: ! 409: Very often, one header file includes another. It can easily result that a ! 410: certain header file is included more than once. This may lead to errors, ! 411: if the header file defines structure types or typedefs, and is certainly ! 412: wasteful. Therefore, we often wish to prevent multiple inclusion of a ! 413: header file. ! 414: ! 415: The standard way to do this is to enclose the entire real contents of the ! 416: file in a conditional, like this: ! 417: ! 418: @example ! 419: #ifndef __FILE_FOO_SEEN__ ! 420: #define __FILE_FOO_SEEN__ ! 421: ! 422: @var{the entire file} ! 423: ! 424: #endif /* __FILE_FOO_SEEN__ */ ! 425: @end example ! 426: ! 427: The macro @code{__FILE_FOO_SEEN__} indicates that the file has been ! 428: included once already; its name should begin with @samp{__} to avoid ! 429: conflicts with user programs, and it should contain the name of the file ! 430: and some additional text, to avoid conflicts with other header files. ! 431: ! 432: The GNU C preprocessor is programmed to notice when a header file uses ! 433: this particular construct and handle it efficiently. If a header file ! 434: is contained entirely in a @samp{#ifndef} conditional, then it records ! 435: that fact. If a subsequent @samp{#include} specifies the same file, ! 436: and the macro in the @samp{#ifndef} is already defined, then the file ! 437: is entirely skipped, without even reading it. ! 438: ! 439: @findex #pragma once ! 440: There is also an explicit command to tell the preprocessor that it need ! 441: not include a file more than once. This is called @samp{#pragma once}, ! 442: and was used @emph{in addition to} the @samp{#ifndef} conditional around ! 443: the contents of the header file. @samp{#pragma once} is now obsolete ! 444: and should not be used at all. ! 445: ! 446: In the Objective C language, there is a variant of @samp{#include} ! 447: called @samp{#import} which includes a file, but does so at most once. ! 448: If you use @samp{#import} @emph{instead of} @samp{#include}, then you ! 449: don't need the conditionals inside the header file to prevent multiple ! 450: execution of the contents. ! 451: ! 452: @samp{#include} is obsolete because it is not a well-designed feature. ! 453: It requires the users of a header file---the applications ! 454: programmers---to know that a certain header file should only be included ! 455: once. It is much better for the header file's implementor to write the ! 456: file so that users don't need to know this. Using @samp{#ifndef} ! 457: accomplishes this goal. ! 458: ! 459: @node Inheritance,, Once-Only, Header Files ! 460: @section Inheritance and Header Files ! 461: @cindex inheritance ! 462: @cindex overriding a header file ! 463: ! 464: @dfn{Inheritance} is what happens when one object or file derives some ! 465: of its contents by virtual copying from another object or file. In ! 466: the case of C header files, inheritance means that one header file ! 467: includes another header file and then replaces or adds something. ! 468: ! 469: If the inheriting header file and the base header file have different ! 470: names, then inheritance is straightforward: simply write @samp{#include ! 471: "@var{base}"} in the inheriting file. ! 472: ! 473: Sometimes it is necessary to give the inheriting file the same name as ! 474: the base file. This is less straightforward. ! 475: ! 476: For example, suppose an application program uses the system header file ! 477: @file{sys/signal.h}, but the version of @file{/usr/include/sys/signal.h} ! 478: on a particular system doesn't do what the application program expects. ! 479: It would be convenient to define a ``local'' version ! 480: @file{/usr/local/include/sys/signal.h} to override or add to the one ! 481: supplied by the system. ! 482: ! 483: You can do this by using the option @samp{-I.} for compilation, and ! 484: writing a file @file{sys/signal.h} that does what the application ! 485: program expects. But making this file include the standard ! 486: @file{sys/signal.h} is not so easy---writing @samp{#include ! 487: <sys/signal.h>} in that file doesn't work, because it includes your own ! 488: version of the file, not the standard system version. Used in that file ! 489: itself, this leads to an infinite recursion and a fatal error in ! 490: compilation. ! 491: ! 492: @samp{#include </usr/include/sys/signal.h>} would find the proper file, ! 493: but that is not clean, since it makes an assumption about where the ! 494: system header file is found. This is bad for maintenance, since it ! 495: means that any change in where the system's header files are kept ! 496: requires a change somewhere else. ! 497: ! 498: @findex #include_next ! 499: The clean way to solve this problem is to use the directive ! 500: @samp{#include_next}, which means, ``Include the @emph{next} file with ! 501: this name.'' This command works like @samp{#include} except in ! 502: searching for the specified file: it starts searching the list of header ! 503: file directories @emph{after} the directory in which the current file ! 504: was found. ! 505: ! 506: Thus, suppose the list of directories to search contains ! 507: @file{/usr/local/include} and @file{/usr/include}, and both directories ! 508: contain a file named @file{sys/signal.h}. Ordinary @samp{#include ! 509: <sys/signal.h>} finds the file under @file{/usr/local/include}. If that ! 510: file contains @samp{#include_next <sys/signal.h>}, it starts searching ! 511: after that directory, and finds the file in @file{/usr/include}. ! 512: ! 513: @node Macros, Conditionals, Header Files, Top ! 514: @section Macros ! 515: ! 516: A macro is a sort of abbreviation which you can define once and then ! 517: use later. There are many complicated features associated with macros ! 518: in the C preprocessor. ! 519: ! 520: @menu ! 521: * Simple Macros:: Macros that always expand the same way. ! 522: * Argument Macros:: Macros that accept arguments that are substituted ! 523: into the macro expansion. ! 524: * Predefined:: Predefined macros that are always available. ! 525: * Stringification:: Macro arguments converted into string constants. ! 526: * Concatenation:: Building tokens from parts taken from macro arguments. ! 527: * Undefining:: Cancelling a macro's definition. ! 528: * Redefining:: Changing a macro's definition. ! 529: * Macro Pitfalls:: Macros can confuse the unwary. Here we explain ! 530: several common problems and strange features. ! 531: @end menu ! 532: ! 533: @node Simple Macros, Argument Macros, Macros, Macros ! 534: @subsection Simple Macros ! 535: ! 536: A @dfn{simple macro} is a kind of abbreviation. It is a name which ! 537: stands for a fragment of code. Some people refer to these as ! 538: @dfn{manifest constants}. ! 539: ! 540: Before you can use a macro, you must @dfn{define} it explicitly with the ! 541: @samp{#define} command. @samp{#define} is followed by the name of the ! 542: macro and then the code it should be an abbreviation for. For example, ! 543: ! 544: @example ! 545: #define BUFFER_SIZE 1020 ! 546: @end example ! 547: ! 548: @noindent ! 549: defines a macro named @samp{BUFFER_SIZE} as an abbreviation for the text ! 550: @samp{1020}. Therefore, if somewhere after this @samp{#define} command ! 551: there comes a C statement of the form ! 552: ! 553: @example ! 554: foo = (char *) xmalloc (BUFFER_SIZE); ! 555: @end example ! 556: ! 557: @noindent ! 558: then the C preprocessor will recognize and @dfn{expand} the macro ! 559: @samp{BUFFER_SIZE}, resulting in ! 560: ! 561: @example ! 562: foo = (char *) xmalloc (1020); ! 563: @end example ! 564: ! 565: @noindent ! 566: the definition must be a single line; however, it may not end in the ! 567: middle of a multi-line string constant or character constant. ! 568: ! 569: The use of all upper case for macro names is a standard convention. ! 570: Programs are easier to read when it is possible to tell at a glance which ! 571: names are macros. ! 572: ! 573: Normally, a macro definition must be a single line, like all C preprocessor ! 574: commands. (You can split a long macro definition cosmetically with ! 575: Backslash-Newline.) There is one exception: Newlines can be included in ! 576: the macro definition if within a string or character constant. By the same ! 577: token, it is not possible for a macro definition to contain an unbalanced ! 578: quote character; the definition automatically extends to include the ! 579: matching quote character that ends the string or character constant. ! 580: Comments within a macro definition may contain Newlines, which make no ! 581: difference since the comments are entirely replaced with Spaces regardless ! 582: of their contents. ! 583: ! 584: Aside from the above, there is no restriction on what can go in a macro ! 585: body. Parentheses need not balance. The body need not resemble valid C ! 586: code. (Of course, you might get error messages from the C compiler when ! 587: you use the macro.) ! 588: ! 589: The C preprocessor scans your program sequentially, so macro definitions ! 590: take effect at the place you write them. Therefore, the following input to ! 591: the C preprocessor ! 592: ! 593: @example ! 594: foo = X; ! 595: #define X 4 ! 596: bar = X; ! 597: @end example ! 598: ! 599: @noindent ! 600: produces as output ! 601: ! 602: @example ! 603: foo = X; ! 604: ! 605: bar = 4; ! 606: @end example ! 607: ! 608: After the preprocessor expands a macro name, the macro's definition body is ! 609: appended to the front of the remaining input, and the check for macro calls ! 610: continues. Therefore, the macro body can contain calls to other macros. ! 611: For example, after ! 612: ! 613: @example ! 614: #define BUFSIZE 1020 ! 615: #define TABLESIZE BUFSIZE ! 616: @end example ! 617: ! 618: @noindent ! 619: the name @samp{TABLESIZE} when used in the program would go through two ! 620: stages of expansion, resulting ultimately in @samp{1020}. ! 621: ! 622: This is not at all the same as defining @samp{TABLESIZE} to be @samp{1020}. ! 623: The @samp{#define} for @samp{TABLESIZE} uses exactly the body you ! 624: specify---in this case, @samp{BUFSIZE}---and does not check to see whether ! 625: it too is the name of a macro. It's only when you @emph{use} @samp{TABLESIZE} ! 626: that the result of its expansion is checked for more macro names. ! 627: @xref{Cascaded Macros}. ! 628: ! 629: @node Argument Macros, Predefined, Simple Macros, Macros ! 630: @subsection Macros with Arguments ! 631: ! 632: A simple macro always stands for exactly the same text, each time it is ! 633: used. Macros can be more flexible when they accept @dfn{arguments}. ! 634: Arguments are fragments of code that you supply each time the macro is ! 635: used. These fragments are included in the expansion of the macro according ! 636: to the directions in the macro definition. ! 637: ! 638: To define a macro that uses arguments, you write a @samp{#define} command ! 639: with a list of @dfn{argument names} in parentheses after the name of the ! 640: macro. The argument names may be any valid C identifiers, separated by ! 641: commas and optionally whitespace. The open-parenthesis must follow the ! 642: macro name immediately, with no space in between. ! 643: ! 644: For example, here is a macro that computes the minimum of two numeric ! 645: values, as it is defined in many C programs: ! 646: ! 647: @example ! 648: #define min(X, Y) ((X) < (Y) ? (X) : (Y)) ! 649: @end example ! 650: ! 651: @noindent ! 652: (This is not the best way to define a ``minimum'' macro in GNU C. ! 653: @xref{Side Effects}, for more information.) ! 654: ! 655: To use a macro that expects arguments, you write the name of the macro ! 656: followed by a list of @dfn{actual arguments} in parentheses. separated by ! 657: commas. The number of actual arguments you give must match the number of ! 658: arguments the macro expects. Examples of use of the macro @samp{min} ! 659: include @samp{min (1, 2)} and @samp{min (x + 28, *p)}. ! 660: ! 661: The expansion text of the macro depends on the arguments you use. ! 662: Each of the argument names of the macro is replaced, throughout the ! 663: macro definition, with the corresponding actual argument. Using the ! 664: same macro @samp{min} defined above, @samp{min (1, 2)} expands into ! 665: ! 666: @example ! 667: ((1) < (2) ? (1) : (2)) ! 668: @end example ! 669: ! 670: @noindent ! 671: where @samp{1} has been substituted for @samp{X} and @samp{2} for @samp{Y}. ! 672: ! 673: Likewise, @samp{min (x + 28, *p)} expands into ! 674: ! 675: @example ! 676: ((x + 28) < (*p) ? (x + 28) : (*p)) ! 677: @end example ! 678: ! 679: Parentheses in the actual arguments must balance; a comma within ! 680: parentheses does not end an argument. However, there is no requirement ! 681: for brackets or braces to balance, and they do not prevent a comma from ! 682: separating arguments. Thus, ! 683: ! 684: @example ! 685: macro (array[x = y, x + 1]) ! 686: @end example ! 687: ! 688: @noindent ! 689: passes two arguments to @code{macro}: @samp{array[x = y} and @samp{x + ! 690: 1]}. If you want to supply @samp{array[x = y, x + 1]} as an argument, ! 691: you must write it as @samp{array[(x = y, x + 1)]}, which is equivalent C ! 692: code. ! 693: ! 694: After the actual arguments are substituted into the macro body, the entire ! 695: result is appended to the front of the remaining input, and the check for ! 696: macro calls continues. Therefore, the actual arguments can contain calls ! 697: to other macros, either with or without arguments, or even to the same ! 698: macro. The macro body can also contain calls to other macros. For ! 699: example, @samp{min (min (a, b), c)} expands into this text: ! 700: ! 701: @example ! 702: ((((a) < (b) ? (a) : (b))) < (c) ! 703: ? (((a) < (b) ? (a) : (b))) ! 704: : (c)) ! 705: @end example ! 706: ! 707: @noindent ! 708: (Line breaks shown here for clarity would not actually be generated.) ! 709: ! 710: If you use the macro name followed by something other than an ! 711: open-parenthesis (after ignoring any spaces, tabs and comments that ! 712: follow), it is not a call to the macro, and the preprocessor does not ! 713: change what you have written. Therefore, it is possible for the same name ! 714: to be a variable or function in your program as well as a macro, and you ! 715: can choose in each instance whether to refer to the macro (if an actual ! 716: argument list follows) or the variable or function (if an argument list ! 717: does not follow). ! 718: ! 719: Such dual use of one name could be confusing and should be avoided ! 720: except when the two meanings are effectively synonymous: that is, when the ! 721: name is both a macro and a function and the two have similar effects. You ! 722: can think of the name simply as a function; use of the name for purposes ! 723: other than calling it (such as, to take the address) will refer to the ! 724: function, while calls will expand the macro and generate better but ! 725: equivalent code. For example, you can use a function named @samp{min} in ! 726: the same source file that defines the macro. If you write @samp{&min} with ! 727: no argument list, you refer to the function. If you write @samp{min (x, ! 728: bb)}, with an argument list, the macro is expanded. If you write ! 729: @samp{(min) (a, bb)}, where the name @samp{min} is not followed by an ! 730: open-parenthesis, the macro is not expanded, so you wind up with a call to ! 731: the function @samp{min}. ! 732: ! 733: You may not define the same name as both a simple macro and a macro with ! 734: arguments. ! 735: ! 736: In the definition of a macro with arguments, the list of argument names ! 737: must follow the macro name immediately with no space in between. If there ! 738: is a space after the macro name, the macro is defined as taking no ! 739: arguments, and all the rest of the name is taken to be the expansion. The ! 740: reason for this is that it is often useful to define a macro that takes no ! 741: arguments and whose definition begins with an identifier in parentheses. ! 742: This rule about spaces makes it possible for you to do either this: ! 743: ! 744: @example ! 745: #define FOO(x) - 1 / (x) ! 746: @end example ! 747: ! 748: @noindent ! 749: (which defines @samp{FOO} to take an argument and expand into minus the ! 750: reciprocal of that argument) or this: ! 751: ! 752: @example ! 753: #define BAR (x) - 1 / (x) ! 754: @end example ! 755: ! 756: @noindent ! 757: (which defines @samp{BAR} to take no argument and always expand into ! 758: @samp{(x) - 1 / (x)}). ! 759: ! 760: Note that the @emph{uses} of a macro with arguments can have spaces before ! 761: the left parenthesis; it's the @emph{definition} where it matters whether ! 762: there is a space. ! 763: ! 764: @node Predefined, Stringification, Argument Macros, Macros ! 765: @subsection Predefined Macros ! 766: ! 767: @cindex predefined macros ! 768: Several simple macros are predefined. You can use them without giving ! 769: definitions for them. They fall into two classes: standard macros and ! 770: system-specific macros. ! 771: ! 772: @menu ! 773: * Standard Predefined:: Standard predefined macros. ! 774: * Nonstandard Predefined:: Nonstandard predefined macros. ! 775: @end menu ! 776: ! 777: @node Standard Predefined, Nonstandard Predefined, Predefined, Predefined ! 778: @subsubsection Standard Predefined Macros ! 779: ! 780: The standard predefined macros are available with the same meanings ! 781: regardless of the machine or operating system on which you are using GNU C. ! 782: Their names all start and end with double underscores. Those preceding ! 783: @code{__GNUC__} in this table are standardized by ANSI C; the rest are ! 784: GNU C extensions. ! 785: ! 786: @table @code ! 787: @item __FILE__ ! 788: @findex __FILE__ ! 789: This macro expands to the name of the current input file, in the form of ! 790: a C string constant. The precise name returned is the one that was ! 791: specified in @samp{#include} or as the input file name argument. ! 792: ! 793: @item __BASE_FILE__ ! 794: @findex __BASE_FILE__ ! 795: This macro expands to the name of the main input file, in the form ! 796: of a C string constant. This is the source file that was specified ! 797: as an argument when the C compiler was invoked. ! 798: ! 799: @item __LINE__ ! 800: @findex __LINE__ ! 801: This macro expands to the current input line number, in the form of a ! 802: decimal integer constant. While we call it a predefined macro, it's ! 803: a pretty strange macro, since its ``definition'' changes with each ! 804: new line of source code. ! 805: ! 806: This and @samp{__FILE__} are useful in generating an error message to ! 807: report an inconsistency detected by the program; the message can state ! 808: the source line at which the inconsistency was detected. For example, ! 809: ! 810: @smallexample ! 811: fprintf (stderr, "Internal error: " ! 812: "negative string length " ! 813: "%d at %s, line %d.", ! 814: length, __FILE__, __LINE__); ! 815: @end smallexample ! 816: ! 817: A @samp{#include} command changes the expansions of @samp{__FILE__} ! 818: and @samp{__LINE__} to correspond to the included file. At the end of ! 819: that file, when processing resumes on the input file that contained ! 820: the @samp{#include} command, the expansions of @samp{__FILE__} and ! 821: @samp{__LINE__} revert to the values they had before the ! 822: @samp{#include} (but @samp{__LINE__} is then incremented by one as ! 823: processing moves to the line after the @samp{#include}). ! 824: ! 825: The expansions of both @samp{__FILE__} and @samp{__LINE__} are altered ! 826: if a @samp{#line} command is used. @xref{Combining Sources}. ! 827: ! 828: @item __DATE__ ! 829: @findex __DATE__ ! 830: This macro expands to a string constant that describes the date on ! 831: which the preprocessor is being run. The string constant contains ! 832: eleven characters and looks like @samp{"Jan 29 1987"} or @w{@samp{"Apr ! 833: 1 1905"}}. ! 834: ! 835: @item __TIME__ ! 836: @findex __TIME__ ! 837: This macro expands to a string constant that describes the time at ! 838: which the preprocessor is being run. The string constant contains ! 839: eight characters and looks like @samp{"23:59:01"}. ! 840: ! 841: @item __STDC__ ! 842: @findex __STDC__ ! 843: This macro expands to the constant 1, to signify that this is ANSI ! 844: Standard C. (Whether that is actually true depends on what C compiler ! 845: will operate on the output from the preprocessor.) ! 846: ! 847: @item __GNUC__ ! 848: This macro is defined if and only if this is GNU C. This macro is ! 849: defined only when the entire GNU C compiler is in use; if you invoke ! 850: the preprocessor directly, @samp{__GNUC__} is undefined. ! 851: ! 852: @item __STRICT_ANSI__ ! 853: This macro is defined if and only if the @samp{-ansi} switch was ! 854: specified when GNU C was invoked. Its definition is the null string. ! 855: This macro exists primarily to direct certain GNU header files not to ! 856: define certain traditional Unix constructs which are incompatible with ! 857: ANSI C. ! 858: ! 859: @item __VERSION__ ! 860: This macro expands to a string which describes the version number of ! 861: GNU C. The string is normally a sequence of decimal numbers separated ! 862: by periods, such as @samp{"1.18"}. The only reasonable use of this ! 863: macro is to incorporate it into a string constant. ! 864: ! 865: @item __OPTIMIZE__ ! 866: This macro is defined in optimizing compilations. It causes certain ! 867: GNU header files to define alternative macro definitions for some ! 868: system library functions. It is unwise to refer to or test the ! 869: definition of this macro unless you make very sure that programs will ! 870: execute with the same effect regardless. ! 871: ! 872: @item __CHAR_UNSIGNED__ ! 873: This macro is defined if and only if the data type @code{char} is ! 874: unsigned on the target machine. It exists to cause the standard ! 875: header file @file{limit.h} to work correctly. It is bad practice ! 876: to refer to this macro yourself; instead, refer to the standard ! 877: macros defined in @file{limit.h}. The preprocessor uses ! 878: this macro to determine whether or not to sign-extend large character ! 879: constants written in octal; see @ref{#if Command,,The @samp{#if} Command}. ! 880: @end table ! 881: ! 882: @node Nonstandard Predefined,, Standard Predefined, Predefined ! 883: @subsubsection Nonstandard Predefined Macros ! 884: ! 885: The C preprocessor normally has several predefined macros that vary between ! 886: machines because their purpose is to indicate what type of system and ! 887: machine is in use. This manual, being for all systems and machines, cannot ! 888: tell you exactly what their names are; instead, we offer a list of some ! 889: typical ones. You can use @samp{cpp -dM} to see the values of ! 890: predefined macros; @pxref{Invocation}. ! 891: ! 892: Some nonstandard predefined macros describe the operating system in use, ! 893: with more or less specificity. For example, ! 894: ! 895: @table @code ! 896: @item unix ! 897: @findex unix ! 898: @samp{unix} is normally predefined on all Unix systems. ! 899: ! 900: @item BSD ! 901: @findex BSD ! 902: @samp{BSD} is predefined on recent versions of Berkeley Unix ! 903: (perhaps only in version 4.3). ! 904: @end table ! 905: ! 906: Other nonstandard predefined macros describe the kind of CPU, with more or ! 907: less specificity. For example, ! 908: ! 909: @table @code ! 910: @item vax ! 911: @findex vax ! 912: @samp{vax} is predefined on Vax computers. ! 913: ! 914: @item mc68000 ! 915: @findex mc68000 ! 916: @samp{mc68000} is predefined on most computers whose CPU is a Motorola ! 917: 68000, 68010 or 68020. ! 918: ! 919: @item m68k ! 920: @findex m68k ! 921: @samp{m68k} is also predefined on most computers whose CPU is a 68000, ! 922: 68010 or 68020; however, some makers use @samp{mc68000} and some use ! 923: @samp{m68k}. Some predefine both names. What happens in GNU C ! 924: depends on the system you are using it on. ! 925: ! 926: @item M68020 ! 927: @findex M68020 ! 928: @samp{M68020} has been observed to be predefined on some systems that ! 929: use 68020 CPUs---in addition to @samp{mc68000} and @samp{m68k}, which ! 930: are less specific. ! 931: ! 932: @item _AM29K ! 933: @findex _AM29K ! 934: @itemx _AM29000 ! 935: @findex _AM29000 ! 936: Both @samp{_AM29K} and @samp{_AM29000} are predefined for the AMD 29000 ! 937: CPU family. ! 938: ! 939: @item ns32000 ! 940: @findex ns32000 ! 941: @samp{ns32000} is predefined on computers which use the National ! 942: Semiconductor 32000 series CPU. ! 943: @end table ! 944: ! 945: Yet other nonstandard predefined macros describe the manufacturer of ! 946: the system. For example, ! 947: ! 948: @table @code ! 949: @item sun ! 950: @findex sun ! 951: @samp{sun} is predefined on all models of Sun computers. ! 952: ! 953: @item pyr ! 954: @findex pyr ! 955: @samp{pyr} is predefined on all models of Pyramid computers. ! 956: ! 957: @item sequent ! 958: @findex sequent ! 959: @samp{sequent} is predefined on all models of Sequent computers. ! 960: @end table ! 961: ! 962: These predefined symbols are not only nonstandard, they are contrary to the ! 963: ANSI standard because their names do not start with underscores. ! 964: Therefore, the option @samp{-ansi} inhibits the definition of these ! 965: symbols. ! 966: ! 967: This tends to make @samp{-ansi} useless, since many programs depend on the ! 968: customary nonstandard predefined symbols. Even system header files check ! 969: them and will generate incorrect declarations if they do not find the names ! 970: that are expected. You might think that the header files supplied for the ! 971: Uglix computer would not need to test what machine they are running on, ! 972: because they can simply assume it is the Uglix; but often they do, and they ! 973: do so using the customary names. As a result, very few C programs will ! 974: compile with @samp{-ansi}. We intend to avoid such problems on the GNU ! 975: system. ! 976: ! 977: What, then, should you do in an ANSI C program to test the type of machine ! 978: it will run on? ! 979: ! 980: GNU C offers a parallel series of symbols for this purpose, whose names ! 981: are made from the customary ones by adding @samp{__} at the beginning ! 982: and end. Thus, the symbol @code{__vax__} would be available on a vax, ! 983: and so on. ! 984: ! 985: The set of nonstandard predefined names in the GNU C preprocessor is ! 986: controlled (when @code{cpp} is itself compiled) by the macro ! 987: @samp{CPP_PREDEFINES}, which should be a string containing @samp{-D} ! 988: options, separated by spaces. For example, on the Sun 3, we use the ! 989: following definition: ! 990: ! 991: @example ! 992: #define CPP_PREDEFINES "-Dmc68000 -Dsun -Dunix -Dm68k" ! 993: @end example ! 994: ! 995: @noindent ! 996: This macro is usually specified in @file{tm.h}. ! 997: ! 998: @node Stringification, Concatenation, Predefined, Macros ! 999: @subsection Stringification ! 1000: ! 1001: @cindex stringification ! 1002: @dfn{Stringification} means turning a code fragment into a string constant ! 1003: whose contents are the text for the code fragment. For example, ! 1004: stringifying @samp{foo (z)} results in @samp{"foo (z)"}. ! 1005: ! 1006: In the C preprocessor, stringification is an option available when macro ! 1007: arguments are substituted into the macro definition. In the body of the ! 1008: definition, when an argument name appears, the character @samp{#} before ! 1009: the name specifies stringification of the corresponding actual argument ! 1010: when it is substituted at that point in the definition. The same argument ! 1011: may be substituted in other places in the definition without ! 1012: stringification if the argument name appears in those places with no ! 1013: @samp{#}. ! 1014: ! 1015: Here is an example of a macro definition that uses stringification: ! 1016: ! 1017: @smallexample ! 1018: #define WARN_IF(EXP) \ ! 1019: do @{ if (EXP) \ ! 1020: fprintf (stderr, "Warning: " #EXP "\n"); @} \ ! 1021: while (0) ! 1022: @end smallexample ! 1023: ! 1024: @noindent ! 1025: Here the actual argument for @samp{EXP} is substituted once as given, ! 1026: into the @samp{if} statement, and once as stringified, into the ! 1027: argument to @samp{fprintf}. The @samp{do} and @samp{while (0)} are ! 1028: a kludge to make it possible to write @samp{WARN_IF (@var{arg});}, ! 1029: which the resemblance of @samp{WARN_IF} to a function would make ! 1030: C programmers want to do; @pxref{Swallow Semicolon}). ! 1031: ! 1032: The stringification feature is limited to transforming one macro argument ! 1033: into one string constant: there is no way to combine the argument with ! 1034: other text and then stringify it all together. But the example above shows ! 1035: how an equivalent result can be obtained in ANSI Standard C using the ! 1036: feature that adjacent string constants are concatenated as one string ! 1037: constant. The preprocessor stringifies @samp{EXP}'s actual argument ! 1038: into a separate string constant, resulting in text like ! 1039: ! 1040: @smallexample ! 1041: do @{ if (x == 0) \ ! 1042: fprintf (stderr, "Warning: " "x == 0" "\n"); @} \ ! 1043: while (0) ! 1044: @end smallexample ! 1045: ! 1046: @noindent ! 1047: but the C compiler then sees three consecutive string constants and ! 1048: concatenates them into one, producing effectively ! 1049: ! 1050: @smallexample ! 1051: do @{ if (x == 0) \ ! 1052: fprintf (stderr, "Warning: x == 0\n"); @} \ ! 1053: while (0) ! 1054: @end smallexample ! 1055: ! 1056: Stringification in C involves more than putting doublequote characters ! 1057: around the fragment; it is necessary to put backslashes in front of all ! 1058: doublequote characters, and all backslashes in string and character ! 1059: constants, in order to get a valid C string constant with the proper ! 1060: contents. Thus, stringifying @samp{p = "foo\n";} results in @samp{"p = ! 1061: \"foo\\n\";"}. However, backslashes that are not inside of string or ! 1062: character constants are not duplicated: @samp{\n} by itself stringifies to ! 1063: @samp{"\n"}. ! 1064: ! 1065: Whitespace (including comments) in the text being stringified is handled ! 1066: according to precise rules. All leading and trailing whitespace is ignored. ! 1067: Any sequence of whitespace in the middle of the text is converted to ! 1068: a single space in the stringified result. ! 1069: ! 1070: @node Concatenation, Undefining, Stringification, Macros ! 1071: @subsection Concatenation ! 1072: ! 1073: @cindex concatenation ! 1074: @dfn{Concatenation} means joining two strings into one. In the context ! 1075: of macro expansion, concatenation refers to joining two lexical units ! 1076: into one longer one. Specifically, an actual argument to the macro can be ! 1077: concatenated with another actual argument or with fixed text to produce ! 1078: a longer name. The longer name might be the name of a function, ! 1079: variable or type, or a C keyword; it might even be the name of another ! 1080: macro, in which case it will be expanded. ! 1081: ! 1082: When you define a macro, you request concatenation with the special ! 1083: operator @samp{##} in the macro body. When the macro is called, ! 1084: after actual arguments are substituted, all @samp{##} operators are ! 1085: deleted, and so is any whitespace next to them (including whitespace ! 1086: that was part of an actual argument). The result is to concatenate ! 1087: the syntactic tokens on either side of the @samp{##}. ! 1088: ! 1089: Consider a C program that interprets named commands. There probably needs ! 1090: to be a table of commands, perhaps an array of structures declared as ! 1091: follows: ! 1092: ! 1093: @example ! 1094: struct command ! 1095: @{ ! 1096: char *name; ! 1097: void (*function) (); ! 1098: @}; ! 1099: ! 1100: struct command commands[] = ! 1101: @{ ! 1102: @{ "quit", quit_command@}, ! 1103: @{ "help", help_command@}, ! 1104: @dots{} ! 1105: @}; ! 1106: @end example ! 1107: ! 1108: It would be cleaner not to have to give each command name twice, once in ! 1109: the string constant and once in the function name. A macro which takes the ! 1110: name of a command as an argument can make this unnecessary. The string ! 1111: constant can be created with stringification, and the function name by ! 1112: concatenating the argument with @samp{_command}. Here is how it is done: ! 1113: ! 1114: @example ! 1115: #define COMMAND(NAME) @{ #NAME, NAME ## _command @} ! 1116: ! 1117: struct command commands[] = ! 1118: @{ ! 1119: COMMAND (quit), ! 1120: COMMAND (help), ! 1121: @dots{} ! 1122: @}; ! 1123: @end example ! 1124: ! 1125: The usual case of concatenation is concatenating two names (or a name and a ! 1126: number) into a longer name. But this isn't the only valid case. It is ! 1127: also possible to concatenate two numbers (or a number and a name, such as ! 1128: @samp{1.5} and @samp{e3}) into a number. Also, multi-character operators ! 1129: such as @samp{+=} can be formed by concatenation. In some cases it is even ! 1130: possible to piece together a string constant. However, two pieces of text ! 1131: that don't together form a valid lexical unit cannot be concatenated. For ! 1132: example, concatenation with @samp{x} on one side and @samp{+} on the other ! 1133: is not meaningful because those two characters can't fit together in any ! 1134: lexical unit of C. The ANSI standard says that such attempts at ! 1135: concatenation are undefined, but in the GNU C preprocessor it is well ! 1136: defined: it puts the @samp{x} and @samp{+} side by side with no particular ! 1137: special results. ! 1138: ! 1139: Keep in mind that the C preprocessor converts comments to whitespace before ! 1140: macros are even considered. Therefore, you cannot create a comment by ! 1141: concatenating @samp{/} and @samp{*}: the @samp{/*} sequence that starts a ! 1142: comment is not a lexical unit, but rather the beginning of a ``long'' space ! 1143: character. Also, you can freely use comments next to a @samp{##} in a ! 1144: macro definition, or in actual arguments that will be concatenated, because ! 1145: the comments will be converted to spaces at first sight, and concatenation ! 1146: will later discard the spaces. ! 1147: ! 1148: @node Undefining, Redefining, Concatenation, Macros ! 1149: @subsection Undefining Macros ! 1150: ! 1151: @cindex undefining macros ! 1152: To @dfn{undefine} a macro means to cancel its definition. This is done ! 1153: with the @samp{#undef} command. @samp{#undef} is followed by the macro ! 1154: name to be undefined. ! 1155: ! 1156: Like definition, undefinition occurs at a specific point in the source ! 1157: file, and it applies starting from that point. The name ceases to be a ! 1158: macro name, and from that point on it is treated by the preprocessor as if ! 1159: it had never been a macro name. ! 1160: ! 1161: For example, ! 1162: ! 1163: @example ! 1164: #define FOO 4 ! 1165: x = FOO; ! 1166: #undef FOO ! 1167: x = FOO; ! 1168: @end example ! 1169: ! 1170: @noindent ! 1171: expands into ! 1172: ! 1173: @example ! 1174: x = 4; ! 1175: ! 1176: x = FOO; ! 1177: @end example ! 1178: ! 1179: @noindent ! 1180: In this example, @samp{FOO} had better be a variable or function as well ! 1181: as (temporarily) a macro, in order for the result of the expansion to be ! 1182: valid C code. ! 1183: ! 1184: The same form of @samp{#undef} command will cancel definitions with ! 1185: arguments or definitions that don't expect arguments. The @samp{#undef} ! 1186: command has no effect when used on a name not currently defined as a macro. ! 1187: ! 1188: @node Redefining, Macro Pitfalls, Undefining, Macros ! 1189: @subsection Redefining Macros ! 1190: ! 1191: @cindex redefining macros ! 1192: @dfn{Redefining} a macro means defining (with @samp{#define}) a name that ! 1193: is already defined as a macro. ! 1194: ! 1195: A redefinition is trivial if the new definition is transparently identical ! 1196: to the old one. You probably wouldn't deliberately write a trivial ! 1197: redefinition, but they can happen automatically when a header file is ! 1198: included more than once (@pxref{Header Files}), so they are accepted ! 1199: silently and without effect. ! 1200: ! 1201: Nontrivial redefinition is considered likely to be an error, so ! 1202: it provokes a warning message from the preprocessor. However, sometimes it ! 1203: is useful to change the definition of a macro in mid-compilation. You can ! 1204: inhibit the warning by undefining the macro with @samp{#undef} before the ! 1205: second definition. ! 1206: ! 1207: In order for a redefinition to be trivial, the new definition must ! 1208: exactly match the one already in effect, with two possible exceptions: ! 1209: ! 1210: @itemize @bullet ! 1211: @item ! 1212: Whitespace may be added or deleted at the beginning or the end. ! 1213: ! 1214: @item ! 1215: Whitespace may be changed in the middle (but not inside strings). ! 1216: However, it may not be eliminated entirely, and it may not be added ! 1217: where there was no whitespace at all. ! 1218: @end itemize ! 1219: ! 1220: Recall that a comment counts as whitespace. ! 1221: ! 1222: @node Macro Pitfalls,, Redefining, Macros ! 1223: @subsection Pitfalls and Subtleties of Macros ! 1224: ! 1225: In this section we describe some special rules that apply to macros and ! 1226: macro expansion, and point out certain cases in which the rules have ! 1227: counterintuitive consequences that you must watch out for. ! 1228: ! 1229: @menu ! 1230: * Misnesting:: Macros can contain unmatched parentheses. ! 1231: * Macro Parentheses:: Why apparently superfluous parentheses ! 1232: may be necessary to avoid incorrect grouping. ! 1233: * Swallow Semicolon:: Macros that look like functions ! 1234: but expand into compound statements. ! 1235: * Side Effects:: Unsafe macros that cause trouble when ! 1236: arguments contain side effects. ! 1237: * Self-Reference:: Macros whose definitions use the macros' own names. ! 1238: * Argument Prescan:: Actual arguments are checked for macro calls ! 1239: before they are substituted. ! 1240: * Cascaded Macros:: Macros whose definitions use other macros. ! 1241: @end menu ! 1242: ! 1243: @node Misnesting, Macro Parentheses, Macro Pitfalls, Macro Pitfalls ! 1244: @subsubsection Improperly Nested Constructs ! 1245: ! 1246: Recall that when a macro is called with arguments, the arguments are ! 1247: substituted into the macro body and the result is checked, together with ! 1248: the rest of the input file, for more macro calls. ! 1249: ! 1250: It is possible to piece together a macro call coming partially from the ! 1251: macro body and partially from the actual arguments. For example, ! 1252: ! 1253: @example ! 1254: #define double(x) (2*(x)) ! 1255: #define call_with_1(x) x(1) ! 1256: @end example ! 1257: ! 1258: @noindent ! 1259: would expand @samp{call_with_1 (double)} into @samp{(2*(1))}. ! 1260: ! 1261: Macro definitions do not have to have balanced parentheses. By writing an ! 1262: unbalanced open parenthesis in a macro body, it is possible to create a ! 1263: macro call that begins inside the macro body but ends outside of it. For ! 1264: example, ! 1265: ! 1266: @example ! 1267: #define strange(file) fprintf (file, "%s %d", ! 1268: @dots{} ! 1269: strange(stderr) p, 35) ! 1270: @end example ! 1271: ! 1272: @noindent ! 1273: This bizarre example expands to @samp{fprintf (stderr, "%s %d", p, 35)}! ! 1274: ! 1275: @node Macro Parentheses, Swallow Semicolon, Misnesting, Macro Pitfalls ! 1276: @subsubsection Unintended Grouping of Arithmetic ! 1277: ! 1278: You may have noticed that in most of the macro definition examples shown ! 1279: above, each occurrence of a macro argument name had parentheses around it. ! 1280: In addition, another pair of parentheses usually surround the entire macro ! 1281: definition. Here is why it is best to write macros that way. ! 1282: ! 1283: Suppose you define a macro as follows, ! 1284: ! 1285: @example ! 1286: #define ceil_div(x, y) (x + y - 1) / y ! 1287: @end example ! 1288: ! 1289: @noindent ! 1290: whose purpose is to divide, rounding up. (One use for this ! 1291: operation is to compute how many @samp{int}'s are needed to hold ! 1292: a certain number of @samp{char}'s.) Then suppose it is used as follows: ! 1293: ! 1294: @example ! 1295: a = ceil_div (b & c, sizeof (int)); ! 1296: @end example ! 1297: ! 1298: @noindent ! 1299: This expands into ! 1300: ! 1301: @example ! 1302: a = (b & c + sizeof (int) - 1) / sizeof (int); ! 1303: @end example ! 1304: ! 1305: @noindent ! 1306: which does not do what is intended. The operator-precedence rules of ! 1307: C make it equivalent to this: ! 1308: ! 1309: @example ! 1310: a = (b & (c + sizeof (int) - 1)) / sizeof (int); ! 1311: @end example ! 1312: ! 1313: @noindent ! 1314: But what we want is this: ! 1315: ! 1316: @example ! 1317: a = ((b & c) + sizeof (int) - 1)) / sizeof (int); ! 1318: @end example ! 1319: ! 1320: @noindent ! 1321: Defining the macro as ! 1322: ! 1323: @example ! 1324: #define ceil_div(x, y) ((x) + (y) - 1) / (y) ! 1325: @end example ! 1326: ! 1327: @noindent ! 1328: provides the desired result. ! 1329: ! 1330: However, unintended grouping can result in another way. Consider ! 1331: @samp{sizeof ceil_div(1, 2)}. That has the appearance of a C expression ! 1332: that would compute the size of the type of @samp{ceil_div (1, 2)}, but in ! 1333: fact it means something very different. Here is what it expands to: ! 1334: ! 1335: @example ! 1336: sizeof ((1) + (2) - 1) / (2) ! 1337: @end example ! 1338: ! 1339: @noindent ! 1340: This would take the size of an integer and divide it by two. The precedence ! 1341: rules have put the division outside the @samp{sizeof} when it was intended ! 1342: to be inside. ! 1343: ! 1344: Parentheses around the entire macro definition can prevent such problems. ! 1345: Here, then, is the recommended way to define @samp{ceil_div}: ! 1346: ! 1347: @example ! 1348: #define ceil_div(x, y) (((x) + (y) - 1) / (y)) ! 1349: @end example ! 1350: ! 1351: @node Swallow Semicolon, Side Effects, Macro Parentheses, Macro Pitfalls ! 1352: @subsubsection Swallowing the Semicolon ! 1353: ! 1354: @cindex semicolons (after macro calls) ! 1355: Often it is desirable to define a macro that expands into a compound ! 1356: statement. Consider, for example, the following macro, that advances a ! 1357: pointer (the argument @samp{p} says where to find it) across whitespace ! 1358: characters: ! 1359: ! 1360: @example ! 1361: #define SKIP_SPACES (p, limit) \ ! 1362: @{ register char *lim = (limit); \ ! 1363: while (p != lim) @{ \ ! 1364: if (*p++ != ' ') @{ \ ! 1365: p--; break; @}@}@} ! 1366: @end example ! 1367: ! 1368: @noindent ! 1369: Here Backslash-Newline is used to split the macro definition, which must ! 1370: be a single line, so that it resembles the way such C code would be ! 1371: laid out if not part of a macro definition. ! 1372: ! 1373: A call to this macro might be @samp{SKIP_SPACES (p, lim)}. Strictly ! 1374: speaking, the call expands to a compound statement, which is a complete ! 1375: statement with no need for a semicolon to end it. But it looks like a ! 1376: function call. So it minimizes confusion if you can use it like a function ! 1377: call, writing a semicolon afterward, as in @samp{SKIP_SPACES (p, lim);} ! 1378: ! 1379: But this can cause trouble before @samp{else} statements, because the ! 1380: semicolon is actually a null statement. Suppose you write ! 1381: ! 1382: @example ! 1383: if (*p != 0) ! 1384: SKIP_SPACES (p, lim); ! 1385: else @dots{} ! 1386: @end example ! 1387: ! 1388: @noindent ! 1389: The presence of two statements---the compound statement and a null ! 1390: statement---in between the @samp{if} condition and the @samp{else} ! 1391: makes invalid C code. ! 1392: ! 1393: The definition of the macro @samp{SKIP_SPACES} can be altered to solve ! 1394: this problem, using a @samp{do @dots{} while} statement. Here is how: ! 1395: ! 1396: @example ! 1397: #define SKIP_SPACES (p, limit) \ ! 1398: do @{ register char *lim = (limit); \ ! 1399: while (p != lim) @{ \ ! 1400: if (*p++ != ' ') @{ \ ! 1401: p--; break; @}@}@} \ ! 1402: while (0) ! 1403: @end example ! 1404: ! 1405: Now @samp{SKIP_SPACES (p, lim);} expands into ! 1406: ! 1407: @example ! 1408: do @{@dots{}@} while (0); ! 1409: @end example ! 1410: ! 1411: @noindent ! 1412: which is one statement. ! 1413: ! 1414: @node Side Effects, Self-Reference, Swallow Semicolon, Macro Pitfalls ! 1415: @subsubsection Duplication of Side Effects ! 1416: ! 1417: @cindex side effects (in macro arguments) ! 1418: @cindex unsafe macros ! 1419: Many C programs define a macro @samp{min}, for ``minimum'', like this: ! 1420: ! 1421: @example ! 1422: #define min(X, Y) ((X) < (Y) ? (X) : (Y)) ! 1423: @end example ! 1424: ! 1425: When you use this macro with an argument containing a side effect, ! 1426: as shown here, ! 1427: ! 1428: @example ! 1429: next = min (x + y, foo (z)); ! 1430: @end example ! 1431: ! 1432: @noindent ! 1433: it expands as follows: ! 1434: ! 1435: @example ! 1436: next = ((x + y) < (foo (z)) ? (x + y) : (foo (z))); ! 1437: @end example ! 1438: ! 1439: @noindent ! 1440: where @samp{x + y} has been substituted for @samp{X} and @samp{foo (z)} ! 1441: for @samp{Y}. ! 1442: ! 1443: The function @samp{foo} is used only once in the statement as it appears ! 1444: in the program, but the expression @samp{foo (z)} has been substituted ! 1445: twice into the macro expansion. As a result, @samp{foo} might be called ! 1446: two times when the statement is executed. If it has side effects or ! 1447: if it takes a long time to compute, the results might not be what you ! 1448: intended. We say that @samp{min} is an @dfn{unsafe} macro. ! 1449: ! 1450: The best solution to this problem is to define @samp{min} in a way that ! 1451: computes the value of @samp{foo (z)} only once. The C language offers no ! 1452: standard way to do this, but it can be done with GNU C extensions as ! 1453: follows: ! 1454: ! 1455: @example ! 1456: #define min(X, Y) \ ! 1457: (@{ typeof (X) __x = (X), __y = (Y); \ ! 1458: (__x < __y) ? __x : __y; @}) ! 1459: @end example ! 1460: ! 1461: If you do not wish to use GNU C extensions, the only solution is to be ! 1462: careful when @emph{using} the macro @samp{min}. For example, you can ! 1463: calculate the value of @samp{foo (z)}, save it in a variable, and use that ! 1464: variable in @samp{min}: ! 1465: ! 1466: @example ! 1467: #define min(X, Y) ((X) < (Y) ? (X) : (Y)) ! 1468: @dots{} ! 1469: @{ ! 1470: int tem = foo (z); ! 1471: next = min (x + y, tem); ! 1472: @} ! 1473: @end example ! 1474: ! 1475: @noindent ! 1476: (where I assume that @samp{foo} returns type @samp{int}). ! 1477: ! 1478: @node Self-Reference, Argument Prescan, Side Effects, Macro Pitfalls ! 1479: @subsubsection Self-Referential Macros ! 1480: ! 1481: @cindex self-reference ! 1482: A @dfn{self-referential} macro is one whose name appears in its definition. ! 1483: A special feature of ANSI Standard C is that the self-reference is not ! 1484: considered a macro call. It is passed into the preprocessor output ! 1485: unchanged. ! 1486: ! 1487: Let's consider an example: ! 1488: ! 1489: @example ! 1490: #define foo (4 + foo) ! 1491: @end example ! 1492: ! 1493: @noindent ! 1494: where @samp{foo} is also a variable in your program. ! 1495: ! 1496: Following the ordinary rules, each reference to @samp{foo} will expand into ! 1497: @samp{(4 + foo)}; then this will be rescanned and will expand into @samp{(4 ! 1498: + (4 + foo))}; and so on until it causes a fatal error (memory full) in the ! 1499: preprocessor. ! 1500: ! 1501: However, the special rule about self-reference cuts this process short ! 1502: after one step, at @samp{(4 + foo)}. Therefore, this macro definition ! 1503: has the possibly useful effect of causing the program to add 4 to ! 1504: the value of @samp{foo} wherever @samp{foo} is referred to. ! 1505: ! 1506: In most cases, it is a bad idea to take advantage of this feature. A ! 1507: person reading the program who sees that @samp{foo} is a variable will ! 1508: not expect that it is a macro as well. The reader will come across the ! 1509: identifier @samp{foo} in the program and think its value should be that ! 1510: of the variable @samp{foo}, whereas in fact the value is four greater. ! 1511: ! 1512: The special rule for self-reference applies also to @dfn{indirect} ! 1513: self-reference. This is the case where a macro @var{x} expands to use a ! 1514: macro @samp{y}, and @samp{y}'s expansion refers to the macro @samp{x}. The ! 1515: resulting reference to @samp{x} comes indirectly from the expansion of ! 1516: @samp{x}, so it is a self-reference and is not further expanded. Thus, ! 1517: after ! 1518: ! 1519: @example ! 1520: #define x (4 + y) ! 1521: #define y (2 * x) ! 1522: @end example ! 1523: ! 1524: @noindent ! 1525: @samp{x} would expand into @samp{(4 + (2 * x))}. Clear? ! 1526: ! 1527: But suppose @samp{y} is used elsewhere, not from the definition of @samp{x}. ! 1528: Then the use of @samp{x} in the expansion of @samp{y} is not a self-reference ! 1529: because @samp{x} is not ``in progress''. So it does expand. However, ! 1530: the expansion of @samp{x} contains a reference to @samp{y}, and that ! 1531: is an indirect self-reference now because @samp{y} is ``in progress''. ! 1532: The result is that @samp{y} expands to @samp{(2 * (4 + y))}. ! 1533: ! 1534: It is not clear that this behavior would ever be useful, but it is specified ! 1535: by the ANSI C standard, so you may need to understand it. ! 1536: ! 1537: @node Argument Prescan, Cascaded Macros, Self-Reference, Macro Pitfalls ! 1538: @subsubsection Separate Expansion of Macro Arguments ! 1539: ! 1540: We have explained that the expansion of a macro, including the substituted ! 1541: actual arguments, is scanned over again for macro calls to be expanded. ! 1542: ! 1543: What really happens is more subtle: first each actual argument text is scanned ! 1544: separately for macro calls. Then the results of this are substituted into ! 1545: the macro body to produce the macro expansion, and the macro expansion ! 1546: is scanned again for macros to expand. ! 1547: ! 1548: The result is that the actual arguments are scanned @emph{twice} to expand ! 1549: macro calls in them. ! 1550: ! 1551: Most of the time, this has no effect. If the actual argument contained ! 1552: any macro calls, they are expanded during the first scan. The result ! 1553: therefore contains no macro calls, so the second scan does not change it. ! 1554: If the actual argument were substituted as given, with no prescan, ! 1555: the single remaining scan would find the same macro calls and produce ! 1556: the same results. ! 1557: ! 1558: You might expect the double scan to change the results when a ! 1559: self-referential macro is used in an actual argument of another macro ! 1560: (@pxref{Self-Reference}): the self-referential macro would be expanded once ! 1561: in the first scan, and a second time in the second scan. But this is not ! 1562: what happens. The self-references that do not expand in the first scan are ! 1563: marked so that they will not expand in the second scan either. ! 1564: ! 1565: The prescan is not done when an argument is stringified or concatenated. ! 1566: Thus, ! 1567: ! 1568: @example ! 1569: #define str(s) #s ! 1570: #define foo 4 ! 1571: str (foo) ! 1572: @end example ! 1573: ! 1574: @noindent ! 1575: expands to @samp{"foo"}. Once more, prescan has been prevented from ! 1576: having any noticeable effect. ! 1577: ! 1578: More precisely, stringification and concatenation use the argument as ! 1579: written, in un-prescanned form. The same actual argument would be used in ! 1580: prescanned form if it is substituted elsewhere without stringification or ! 1581: concatenation. ! 1582: ! 1583: @example ! 1584: #define str(s) #s lose(s) ! 1585: #define foo 4 ! 1586: str (foo) ! 1587: @end example ! 1588: ! 1589: expands to @samp{"foo" lose(4)}. ! 1590: ! 1591: You might now ask, ``Why mention the prescan, if it makes no difference? ! 1592: And why not skip it and make the preprocessor faster?'' The answer is ! 1593: that the prescan does make a difference in three special cases: ! 1594: ! 1595: @itemize @bullet ! 1596: @item ! 1597: Nested calls to a macro. ! 1598: ! 1599: @item ! 1600: Macros that call other macros that stringify or concatenate. ! 1601: ! 1602: @item ! 1603: Macros whose expansions contain unshielded commas. ! 1604: @end itemize ! 1605: ! 1606: We say that @dfn{nested} calls to a macro occur when a macro's actual ! 1607: argument contains a call to that very macro. For example, if @samp{f} ! 1608: is a macro that expects one argument, @samp{f (f (1))} is a nested ! 1609: pair of calls to @samp{f}. The desired expansion is made by ! 1610: expanding @samp{f (1)} and substituting that into the definition of ! 1611: @samp{f}. The prescan causes the expected result to happen. ! 1612: Without the prescan, @samp{f (1)} itself would be substituted as ! 1613: an actual argument, and the inner use of @samp{f} would appear ! 1614: during the main scan as an indirect self-reference and would not ! 1615: be expanded. Here, the prescan cancels an undesirable side effect ! 1616: (in the medical, not computational, sense of the term) of the special ! 1617: rule for self-referential macros. ! 1618: ! 1619: But prescan causes trouble in certain other cases of nested macro calls. ! 1620: Here is an example: ! 1621: ! 1622: @example ! 1623: #define foo a,b ! 1624: #define bar(x) lose(x) ! 1625: #define lose(x) (1 + (x)) ! 1626: ! 1627: bar(foo) ! 1628: @end example ! 1629: ! 1630: @noindent ! 1631: We would like @samp{bar(foo)} to turn into @samp{(1 + (foo))}, which ! 1632: would then turn into @samp{(1 + (a,b))}. But instead, @samp{bar(foo)} ! 1633: expands into @samp{lose(a,b)}, and you get an error because @code{lose} ! 1634: requires a single argument. In this case, the problem is easily solved ! 1635: by the same parentheses that ought to be used to prevent misnesting of ! 1636: arithmetic operations: ! 1637: ! 1638: @example ! 1639: #define foo (a,b) ! 1640: #define bar(x) lose((x)) ! 1641: @end example ! 1642: ! 1643: The problem is more serious when the operands of the macro are not ! 1644: expressions; for example, when they are statements. Then parentheses ! 1645: are unacceptable because they would make for invalid C code: ! 1646: ! 1647: @example ! 1648: #define foo @{ int a, b; @dots{} @} ! 1649: @end example ! 1650: ! 1651: @noindent ! 1652: In GNU C you can shield the commas using the @samp{(@{@dots{}@})} ! 1653: construct which turns a compound statement into an expression: ! 1654: ! 1655: @example ! 1656: #define foo (@{ int a, b; @dots{} @}) ! 1657: @end example ! 1658: ! 1659: Or you can rewrite the macro definition to avoid such commas: ! 1660: ! 1661: @example ! 1662: #define foo @{ int a; int b; @dots{} @} ! 1663: @end example ! 1664: ! 1665: There is also one case where prescan is useful. It is possible ! 1666: to use prescan to expand an argument and then stringify it---if you use ! 1667: two levels of macros. Let's add a new macro @samp{xstr} to the ! 1668: example shown above: ! 1669: ! 1670: @example ! 1671: #define xstr(s) str(s) ! 1672: #define str(s) #s ! 1673: #define foo 4 ! 1674: xstr (foo) ! 1675: @end example ! 1676: ! 1677: This expands into @samp{"4"}, not @samp{"foo"}. The reason for the ! 1678: difference is that the argument of @samp{xstr} is expanded at prescan ! 1679: (because @samp{xstr} does not specify stringification or concatenation of ! 1680: the argument). The result of prescan then forms the actual argument for ! 1681: @samp{str}. @samp{str} uses its argument without prescan because it ! 1682: performs stringification; but it cannot prevent or undo the prescanning ! 1683: already done by @samp{xstr}. ! 1684: ! 1685: @node Cascaded Macros,, Argument Prescan, Macro Pitfalls ! 1686: @subsubsection Cascaded Use of Macros ! 1687: ! 1688: @cindex cascaded macros ! 1689: @cindex macro body uses macro ! 1690: A @dfn{cascade} of macros is when one macro's body contains a reference ! 1691: to another macro. This is very common practice. For example, ! 1692: ! 1693: @example ! 1694: #define BUFSIZE 1020 ! 1695: #define TABLESIZE BUFSIZE ! 1696: @end example ! 1697: ! 1698: This is not at all the same as defining @samp{TABLESIZE} to be @samp{1020}. ! 1699: The @samp{#define} for @samp{TABLESIZE} uses exactly the body you ! 1700: specify---in this case, @samp{BUFSIZE}---and does not check to see whether ! 1701: it too is the name of a macro. ! 1702: ! 1703: It's only when you @emph{use} @samp{TABLESIZE} that the result of its expansion ! 1704: is checked for more macro names. ! 1705: ! 1706: This makes a difference if you change the definition of @samp{BUFSIZE} ! 1707: at some point in the source file. @samp{TABLESIZE}, defined as shown, ! 1708: will always expand using the definition of @samp{BUFSIZE} that is ! 1709: currently in effect: ! 1710: ! 1711: @example ! 1712: #define BUFSIZE 1020 ! 1713: #define TABLESIZE BUFSIZE ! 1714: #undef BUFSIZE ! 1715: #define BUFSIZE 37 ! 1716: @end example ! 1717: ! 1718: @noindent ! 1719: Now @samp{TABLESIZE} expands (in two stages) to @samp{37}. ! 1720: ! 1721: @node Conditionals, Combining Sources, Macros, Top ! 1722: @section Conditionals ! 1723: ! 1724: @cindex conditionals ! 1725: In a macro processor, a @dfn{conditional} is a command that allows a part ! 1726: of the program to be ignored during compilation, on some conditions. ! 1727: In the C preprocessor, a conditional can test either an arithmetic expression ! 1728: or whether a name is defined as a macro. ! 1729: ! 1730: A conditional in the C preprocessor resembles in some ways an @samp{if} ! 1731: statement in C, but it is important to understand the difference between ! 1732: them. The condition in an @samp{if} statement is tested during the execution ! 1733: of your program. Its purpose is to allow your program to behave differently ! 1734: from run to run, depending on the data it is operating on. The condition ! 1735: in a preprocessor conditional command is tested when your program is compiled. ! 1736: Its purpose is to allow different code to be included in the program depending ! 1737: on the situation at the time of compilation. ! 1738: ! 1739: @menu ! 1740: * Uses: Conditional Uses. What conditionals are for. ! 1741: * Syntax: Conditional Syntax. How conditionals are written. ! 1742: * Deletion: Deleted Code. Making code into a comment. ! 1743: * Macros: Conditionals-Macros. Why conditionals are used with macros. ! 1744: * Errors: #error Command. Detecting inconsistent compilation parameters. ! 1745: @end menu ! 1746: ! 1747: @node Conditional Uses, Conditional Syntax, Conditionals, Conditionals ! 1748: @subsection Why Conditionals are Used ! 1749: ! 1750: Generally there are three kinds of reason to use a conditional. ! 1751: ! 1752: @itemize @bullet ! 1753: @item ! 1754: A program may need to use different code depending on the machine or ! 1755: operating system it is to run on. In some cases the code for one ! 1756: operating system may be erroneous on another operating system; for ! 1757: example, it might refer to library routines that do not exist on the ! 1758: other system. When this happens, it is not enough to avoid executing ! 1759: the invalid code: merely having it in the program makes it impossible ! 1760: to link the program and run it. With a preprocessor conditional, the ! 1761: offending code can be effectively excised from the program when it is ! 1762: not valid. ! 1763: ! 1764: @item ! 1765: You may want to be able to compile the same source file into two ! 1766: different programs. Sometimes the difference between the programs is ! 1767: that one makes frequent time-consuming consistency checks on its ! 1768: intermediate data while the other does not. ! 1769: ! 1770: @item ! 1771: A conditional whose condition is always false is a good way to exclude ! 1772: code from the program but keep it as a sort of comment for future ! 1773: reference. ! 1774: @end itemize ! 1775: ! 1776: Most simple programs that are intended to run on only one machine will ! 1777: not need to use preprocessor conditionals. ! 1778: ! 1779: @node Conditional Syntax, Deleted Code, Conditional Uses, Conditionals ! 1780: @subsection Syntax of Conditionals ! 1781: ! 1782: @findex #if ! 1783: A conditional in the C preprocessor begins with a @dfn{conditional ! 1784: command}: @samp{#if}, @samp{#ifdef} or @samp{#ifndef}. ! 1785: @xref{Conditionals-Macros}, for information on @samp{#ifdef} and ! 1786: @samp{#ifndef}; only @samp{#if} is explained here. ! 1787: ! 1788: @menu ! 1789: * If: #if Command. Basic conditionals using @samp{#if} and @samp{#endif}. ! 1790: * Else: #else Command. Including some text if the condition fails. ! 1791: * Elif: #elif Command. Testing several alternative possibilities. ! 1792: @end menu ! 1793: ! 1794: @node #if Command, #else Command, Conditional Syntax, Conditional Syntax ! 1795: @subsubsection The @samp{#if} Command ! 1796: ! 1797: The @samp{#if} command in its simplest form consists of ! 1798: ! 1799: @example ! 1800: #if @var{expression} ! 1801: @var{controlled text} ! 1802: #endif /* @var{expression} */ ! 1803: @end example ! 1804: ! 1805: The comment following the @samp{#endif} is not required, but it is a good ! 1806: practice because it helps people match the @samp{#endif} to the ! 1807: corresponding @samp{#if}. Such comments should always be used, except in ! 1808: short conditionals that are not nested. In fact, you can put anything at ! 1809: all after the @samp{#endif} and it will be ignored by the GNU C preprocessor, ! 1810: but only comments are acceptable in ANSI Standard C. ! 1811: ! 1812: @var{expression} is a C expression of integer type, subject to stringent ! 1813: restrictions. It may contain ! 1814: ! 1815: @itemize @bullet ! 1816: @item ! 1817: Integer constants, which are all regarded as @code{long} or ! 1818: @code{unsigned long}. ! 1819: ! 1820: @item ! 1821: Character constants, which are interpreted according to the character ! 1822: set and conventions of the machine and operating system on which the ! 1823: preprocessor is running. The GNU C preprocessor uses the C data type ! 1824: @samp{char} for these character constants; therefore, whether some ! 1825: character codes are negative is determined by the C compiler used to ! 1826: compile the preprocessor. If it treats @samp{char} as signed, then ! 1827: character codes large enough to set the sign bit will be considered ! 1828: negative; otherwise, no character code is considered negative. ! 1829: ! 1830: @item ! 1831: Arithmetic operators for addition, subtraction, multiplication, ! 1832: division, bitwise operations, shifts, comparisons, and @samp{&&} and ! 1833: @samp{||}. ! 1834: ! 1835: @item ! 1836: Identifiers that are not macros, which are all treated as zero(!). ! 1837: ! 1838: @item ! 1839: Macro calls. All macro calls in the expression are expanded before ! 1840: actual computation of the expression's value begins. ! 1841: @end itemize ! 1842: ! 1843: Note that @samp{sizeof} operators and @code{enum}-type values are not allowed. ! 1844: @code{enum}-type values, like all other identifiers that are not taken ! 1845: as macro calls and expanded, are treated as zero. ! 1846: ! 1847: The @var{controlled text} inside of a conditional can include ! 1848: preprocessor commands. Then the commands inside the conditional are ! 1849: obeyed only if that branch of the conditional succeeds. The text can ! 1850: also contain other conditional groups. However, the @samp{#if}'s and ! 1851: @samp{#endif}'s must balance. ! 1852: ! 1853: @node #else Command, #elif Command, #if Command, Conditional Syntax ! 1854: @subsubsection The @samp{#else} Command ! 1855: ! 1856: @findex #else ! 1857: The @samp{#else} command can be added to a conditional to provide ! 1858: alternative text to be used if the condition is false. This is what ! 1859: it looks like: ! 1860: ! 1861: @example ! 1862: #if @var{expression} ! 1863: @var{text-if-true} ! 1864: #else /* Not @var{expression} */ ! 1865: @var{text-if-false} ! 1866: #endif /* Not @var{expression} */ ! 1867: @end example ! 1868: ! 1869: If @var{expression} is nonzero, and the @var{text-if-true} is considered ! 1870: included, then @samp{#else} acts like a failing conditional and the ! 1871: @var{text-if-false} is ignored. Contrariwise, if the @samp{#if} ! 1872: conditional fails, the @var{text-if-false} is considered included. ! 1873: ! 1874: @node #elif Command,, #else Command, Conditional Syntax ! 1875: @subsubsection The @samp{#elif} Command ! 1876: ! 1877: @findex #elif ! 1878: One common case of nested conditionals is used to check for more than two ! 1879: possible alternatives. For example, you might have ! 1880: ! 1881: @example ! 1882: #if X == 1 ! 1883: @dots{} ! 1884: #else /* X != 1 */ ! 1885: #if X == 2 ! 1886: @dots{} ! 1887: #else /* X != 2 */ ! 1888: @dots{} ! 1889: #endif /* X != 2 */ ! 1890: #endif /* X != 1 */ ! 1891: @end example ! 1892: ! 1893: Another conditional command, @samp{#elif}, allows this to be abbreviated ! 1894: as follows: ! 1895: ! 1896: @example ! 1897: #if X == 1 ! 1898: @dots{} ! 1899: #elif X == 2 ! 1900: @dots{} ! 1901: #else /* X != 2 and X != 1*/ ! 1902: @dots{} ! 1903: #endif /* X != 2 and X != 1*/ ! 1904: @end example ! 1905: ! 1906: @samp{#elif} stands for ``else if''. Like @samp{#else}, it goes in the ! 1907: middle of a @samp{#if}-@samp{#endif} pair and subdivides it; it does not ! 1908: require a matching @samp{#endif} of its own. Like @samp{#if}, the ! 1909: @samp{#elif} command includes an expression to be tested. ! 1910: ! 1911: The text following the @samp{#elif} is processed only if the original ! 1912: @samp{#if}-condition failed and the @samp{#elif} condition succeeeds. More ! 1913: than one @samp{#elif} can go in the same @samp{#if}-@samp{#endif} group. ! 1914: Then the text after each @samp{#elif} is processed only if the @samp{#elif} ! 1915: condition succeeds after the original @samp{#if} and any previous ! 1916: @samp{#elif}'s within it have failed. @samp{#else} is equivalent to ! 1917: @samp{#elif 1}, and @samp{#else} is allowed after any number of ! 1918: @samp{#elif}'s, but @samp{#elif} may not follow @samp{#else}. ! 1919: ! 1920: @node Deleted Code, Conditionals-Macros, Conditional Syntax, Conditionals ! 1921: @subsection Keeping Deleted Code for Future Reference ! 1922: ! 1923: If you replace or delete a part of the program but want to keep the old ! 1924: code around as a comment for future reference, the easy way to do this is ! 1925: to put @samp{#if 0} before it and @samp{#endif} after it. ! 1926: ! 1927: This works even if the code being turned off contains conditionals, but ! 1928: they must be entire conditionals (balanced @samp{#if} and @samp{#endif}). ! 1929: ! 1930: @node Conditionals-Macros, #error Command, Deleted Code, Conditionals ! 1931: @subsection Conditionals and Macros ! 1932: ! 1933: Conditionals are rarely useful except in connection with macros. A ! 1934: @samp{#if} command whose expression uses no macros is equivalent to ! 1935: @samp{#if 1} or @samp{#if 0}; you might as well determine which one, by ! 1936: computing the value of the expression yourself, and then simplify the ! 1937: program. But when the expression uses macros, its value can vary from ! 1938: compilation to compilation. ! 1939: ! 1940: For example, here is a conditional that tests the expression ! 1941: @samp{BUFSIZE == 1020}, where @samp{BUFSIZE} must be a macro. ! 1942: ! 1943: @example ! 1944: #if BUFSIZE == 1020 ! 1945: printf ("Large buffers!\n"); ! 1946: #endif /* BUFSIZE is large */ ! 1947: @end example ! 1948: ! 1949: @findex defined ! 1950: The special operator @samp{defined} may be used in @samp{#if} expressions ! 1951: to test whether a certain name is defined as a macro. Either @samp{defined ! 1952: @var{name}} or @samp{defined (@var{name})} is an expression whose value is ! 1953: 1 if @var{name} is defined as macro at the current point in the program, ! 1954: and 0 otherwise. For the @samp{defined} operator it makes no difference ! 1955: what the definition of the macro is; all that matters is whether there is a ! 1956: definition. Thus, for example,@refill ! 1957: ! 1958: @example ! 1959: #if defined (vax) || defined (ns16000) ! 1960: @end example ! 1961: ! 1962: @noindent ! 1963: would include the following code if either of the names @samp{vax} and ! 1964: @samp{ns16000} is defined as a macro. ! 1965: ! 1966: If a macro is defined and later undefined with @samp{#undef}, ! 1967: subsequent use of the @samp{defined} operator will return 0, because ! 1968: the name is no longer defined. If the macro is defined again with ! 1969: another @samp{#define}, @samp{defined} will recommence returning 1. ! 1970: ! 1971: @findex #ifdef ! 1972: @findex #ifndef ! 1973: Conditionals that test just the definedness of one name are very common, so ! 1974: there are two special short conditional commands for this case. They are ! 1975: ! 1976: @table @code ! 1977: @item #ifdef @var{name} ! 1978: is equivalent to @samp{#if defined (@var{name})}. ! 1979: ! 1980: @item #ifndef @var{name} ! 1981: is equivalent to @samp{#if ! defined (@var{name})}. ! 1982: @end table ! 1983: ! 1984: Macro definitions can vary between compilations for several reasons. ! 1985: ! 1986: @itemize @bullet ! 1987: @item ! 1988: Some macros are predefined on each kind of machine. For example, on a ! 1989: Vax, the name @samp{vax} is a predefined macro. On other machines, it ! 1990: would not be defined. ! 1991: ! 1992: @item ! 1993: Many more macros are defined by system header files. Different ! 1994: systems and machines define different macros, or give them different ! 1995: values. It is useful to test these macros with conditionals to avoid ! 1996: using a system feature on a machine where it is not implemented. ! 1997: ! 1998: @item ! 1999: Macros are a common way of allowing users to customize a program for ! 2000: different machines or applications. For example, the macro ! 2001: @samp{BUFSIZE} might be defined in a configuration file for your ! 2002: program that is included as a header file in each source file. You ! 2003: would use @samp{BUFSIZE} in a preprocessor conditional in order to ! 2004: generate different code depending on the chosen configuration. ! 2005: ! 2006: @item ! 2007: Macros can be defined or undefined with @samp{-D} and @samp{-U} ! 2008: command options when you compile the program. You can arrange to ! 2009: compile the same source file into two different programs by choosing ! 2010: a macro name to specify which program you want, writing conditionals ! 2011: to test whether or how this macro is defined, and then controlling ! 2012: the state of the macro with compiler command options. ! 2013: @xref{Invocation}. ! 2014: @end itemize ! 2015: ! 2016: @node #error Command,, Conditionals-Macros, Conditionals ! 2017: @subsection The @samp{#error} and @samp{#warning} Commands ! 2018: ! 2019: @findex #error ! 2020: The command @samp{#error} causes the preprocessor to report a fatal ! 2021: error. The rest of the line that follows @samp{#error} is used as the ! 2022: error message. ! 2023: ! 2024: You would use @samp{#error} inside of a conditional that detects a ! 2025: combination of parameters which you know the program does not properly ! 2026: support. For example, if you know that the program will not run ! 2027: properly on a Vax, you might write ! 2028: ! 2029: @smallexample ! 2030: #ifdef vax ! 2031: #error Won't work on Vaxen. See comments at get_last_object. ! 2032: #endif ! 2033: @end smallexample ! 2034: ! 2035: @noindent ! 2036: @xref{Nonstandard Predefined}, for why this works. ! 2037: ! 2038: If you have several configuration parameters that must be set up by ! 2039: the installation in a consistent way, you can use conditionals to detect ! 2040: an inconsistency and report it with @samp{#error}. For example, ! 2041: ! 2042: @smallexample ! 2043: #if HASH_TABLE_SIZE % 2 == 0 || HASH_TABLE_SIZE % 3 == 0 \ ! 2044: || HASH_TABLE_SIZE % 5 == 0 ! 2045: #error HASH_TABLE_SIZE should not be divisible by a small prime ! 2046: #endif ! 2047: @end smallexample ! 2048: ! 2049: @findex #warning ! 2050: The command @samp{#warning} is like the command @samp{#error}, but causes ! 2051: the preprocessor to issue a warning and continue preprocessing. The rest of ! 2052: the line that follows @samp{#warning} is used as the warning message. ! 2053: ! 2054: You might use @samp{#warning} in obsolete header files, with a message ! 2055: directing the user to the header file which should be used instead. ! 2056: ! 2057: @node Combining Sources, Other Commands, Conditionals, Top ! 2058: @section Combining Source Files ! 2059: ! 2060: @cindex line control ! 2061: @findex #line ! 2062: One of the jobs of the C preprocessor is to inform the C compiler of where ! 2063: each line of C code came from: which source file and which line number. ! 2064: ! 2065: C code can come from multiple source files if you use @samp{#include}; ! 2066: both @samp{#include} and the use of conditionals and macros can cause ! 2067: the line number of a line in the preprocessor output to be different ! 2068: from the line's number in the original source file. You will appreciate ! 2069: the value of making both the C compiler (in error messages) and symbolic ! 2070: debuggers such as GDB use the line numbers in your source file. ! 2071: ! 2072: The C preprocessor builds on this feature by offering a command by which ! 2073: you can control the feature explicitly. This is useful when a file for ! 2074: input to the C preprocessor is the output from another program such as the ! 2075: @code{bison} parser generator, which operates on another file that is the ! 2076: true source file. Parts of the output from @code{bison} are generated from ! 2077: scratch, other parts come from a standard parser file. The rest are copied ! 2078: nearly verbatim from the source file, but their line numbers in the ! 2079: @code{bison} output are not the same as their original line numbers. ! 2080: Naturally you would like compiler error messages and symbolic debuggers to ! 2081: know the original source file and line number of each line in the ! 2082: @code{bison} output. ! 2083: ! 2084: @code{bison} arranges this by writing @samp{#line} commands into the output ! 2085: file. @samp{#line} is a command that specifies the original line number ! 2086: and source file name for subsequent input in the current preprocessor input ! 2087: file. @samp{#line} has three variants: ! 2088: ! 2089: @table @code ! 2090: @item #line @var{linenum} ! 2091: Here @var{linenum} is a decimal integer constant. This specifies that ! 2092: the line number of the following line of input, in its original source file, ! 2093: was @var{linenum}. ! 2094: ! 2095: @item #line @var{linenum} @var{filename} ! 2096: Here @var{linenum} is a decimal integer constant and @var{filename} ! 2097: is a string constant. This specifies that the following line of input ! 2098: came originally from source file @var{filename} and its line number there ! 2099: was @var{linenum}. Keep in mind that @var{filename} is not just a ! 2100: file name; it is surrounded by doublequote characters so that it looks ! 2101: like a string constant. ! 2102: ! 2103: @item #line @var{anything else} ! 2104: @var{anything else} is checked for macro calls, which are expanded. ! 2105: The result should be a decimal integer constant followed optionally ! 2106: by a string constant, as described above. ! 2107: @end table ! 2108: ! 2109: @samp{#line} commands alter the results of the @samp{__FILE__} and ! 2110: @samp{__LINE__} predefined macros from that point on. @xref{Standard ! 2111: Predefined}. ! 2112: ! 2113: @node Other Commands, Output, Combining Sources, Top ! 2114: @section Miscellaneous Preprocessor Commands ! 2115: ! 2116: @findex #pragma ! 2117: @findex #ident ! 2118: @cindex null command ! 2119: This section describes three additional preprocessor commands. They are ! 2120: not very useful, but are mentioned for completeness. ! 2121: ! 2122: The @dfn{null command} consists of a @samp{#} followed by a Newline, with ! 2123: only whitespace (including comments) in between. A null command is ! 2124: understood as a preprocessor command but has no effect on the preprocessor ! 2125: output. The primary significance of the existence of the null command is ! 2126: that an input line consisting of just a @samp{#} will produce no output, ! 2127: rather than a line of output containing just a @samp{#}. Supposedly ! 2128: some old C programs contain such lines. ! 2129: ! 2130: The ANSI standard specifies that the @samp{#pragma} command has an ! 2131: arbitrary, implementation-defined effect. In the GNU C preprocessor, ! 2132: @samp{#pragma} commands are ignored, except for @samp{#pragma once} ! 2133: (@pxref{Once-Only}). ! 2134: ! 2135: The @samp{#ident} command is supported for compatibility with certain ! 2136: other systems. It is followed by a line of text. On some systems, the ! 2137: text is copied into a special place in the object file; on most systems, ! 2138: the text is ignored and this directive has no effect. Typically ! 2139: @samp{#ident} is only used in header files supplied with those systems ! 2140: where it is meaningful. ! 2141: ! 2142: @node Output, Invocation, Other Commands, Top ! 2143: @section C Preprocessor Output ! 2144: ! 2145: @cindex output format ! 2146: The output from the C preprocessor looks much like the input, except ! 2147: that all preprocessor command lines have been replaced with blank lines ! 2148: and all comments with spaces. Whitespace within a line is not altered; ! 2149: however, a space is inserted after the expansions of most macro calls. ! 2150: ! 2151: Source file name and line number information is conveyed by lines of ! 2152: the form ! 2153: ! 2154: @example ! 2155: # @var{linenum} @var{filename} @var{flag} ! 2156: @end example ! 2157: ! 2158: @noindent ! 2159: which are inserted as needed into the middle of the input (but never ! 2160: within a string or character constant). Such a line means that the ! 2161: following line originated in file @var{filename} at line @var{linenum}. ! 2162: ! 2163: The third field, @var{flag}, may be a number, or may be absent. It is ! 2164: @samp{1} for the beginning of a new source file, and @samp{2} for return ! 2165: to an old source file at the end of an included file. It is absent ! 2166: otherwise. ! 2167: ! 2168: @node Invocation, Concept Index, Output, Top ! 2169: @section Invoking the C Preprocessor ! 2170: ! 2171: Most often when you use the C preprocessor you will not have to invoke it ! 2172: explicitly: the C compiler will do so automatically. However, the ! 2173: preprocessor is sometimes useful individually. ! 2174: ! 2175: The C preprocessor expects two file names as arguments, @var{infile} and ! 2176: @var{outfile}. The preprocessor reads @var{infile} together with any other ! 2177: files it specifies with @samp{#include}. All the output generated by the ! 2178: combined input files is written in @var{outfile}. ! 2179: ! 2180: Either @var{infile} or @var{outfile} may be @samp{-}, which as @var{infile} ! 2181: means to read from standard input and as @var{outfile} means to write to ! 2182: standard output. Also, if @var{outfile} or both file names are omitted, ! 2183: the standard output and standard input are used for the omitted file names. ! 2184: ! 2185: @cindex options ! 2186: Here is a table of command options accepted by the C preprocessor. ! 2187: These options can also be given when compiling a C program; they are ! 2188: passed along automatically to the preprocessor when it is invoked by the ! 2189: compiler. ! 2190: ! 2191: @table @samp ! 2192: @item -P ! 2193: @findex -P ! 2194: Inhibit generation of @samp{#}-lines with line-number information in ! 2195: the output from the preprocessor (@pxref{Output}). This might be ! 2196: useful when running the preprocessor on something that is not C code ! 2197: and will be sent to a program which might be confused by the ! 2198: @samp{#}-lines. ! 2199: ! 2200: @item -C ! 2201: @findex -C ! 2202: Do not discard comments: pass them through to the output file. ! 2203: Comments appearing in arguments of a macro call will be copied to the ! 2204: output before the expansion of the macro call. ! 2205: ! 2206: @item -trigraphs ! 2207: @findex -trigraphs ! 2208: Process ANSI standard trigraph sequences. These are three-character ! 2209: sequences, all starting with @samp{??}, that are defined by ANSI C to ! 2210: stand for single characters. For example, @samp{??/} stands for ! 2211: @samp{\}, so @samp{'??/n'} is a character constant for a newline. ! 2212: Strictly speaking, the GNU C preprocessor does not support all ! 2213: programs in ANSI Standard C unless @samp{-trigraphs} is used, but if ! 2214: you ever notice the difference it will be with relief. ! 2215: ! 2216: You don't want to know any more about trigraphs. ! 2217: ! 2218: @item -pedantic ! 2219: @findex -pedantic ! 2220: Issue warnings required by the ANSI C standard in certain cases such ! 2221: as when text other than a comment follows @samp{#else} or @samp{#endif}. ! 2222: ! 2223: @item -pedantic-errors ! 2224: @findex -pedantic-errors ! 2225: Like @samp{-pedantic}, except that errors are produced rather than ! 2226: warnings. ! 2227: ! 2228: @item -Wtrigraphs ! 2229: Warn if any trigraphs are encountered (assuming they are enabled). ! 2230: ! 2231: @item -Wcomment ! 2232: @findex -Wcomment ! 2233: @ignore ! 2234: @c "Not worth documenting" both singular and plural forms of this ! 2235: @c option, per RMS. But also unclear which is better; hence may need to ! 2236: @c switch this at some future date. [email protected], 2jan92. ! 2237: @itemx -Wcomments ! 2238: (Both forms have the same effect). ! 2239: @end ignore ! 2240: Warn whenever a comment-start sequence @samp{/*} appears in a comment. ! 2241: ! 2242: @item -Wall ! 2243: @findex -Wall ! 2244: Requests both @samp{-Wtrigraphs} and @samp{-Wcomment} (but not ! 2245: @samp{-Wtraditional}). ! 2246: ! 2247: @item -Wtraditional ! 2248: @findex -Wtraditional ! 2249: Warn about certain constructs that behave differently in traditional and ! 2250: ANSI C. ! 2251: ! 2252: @item -I @var{directory} ! 2253: @findex -I ! 2254: Add the directory @var{directory} to the end of the list of ! 2255: directories to be searched for header files (@pxref{Include Syntax}). ! 2256: This can be used to override a system header file, substituting your ! 2257: own version, since these directories are searched before the system ! 2258: header file directories. If you use more than one @samp{-I} option, ! 2259: the directories are scanned in left-to-right order; the standard ! 2260: system directories come after. ! 2261: ! 2262: @item -I- ! 2263: Any directories specified with @samp{-I} options before the @samp{-I-} ! 2264: option are searched only for the case of @samp{#include "@var{file}"}; ! 2265: they are not searched for @samp{#include <@var{file}>}. ! 2266: ! 2267: If additional directories are specified with @samp{-I} options after ! 2268: the @samp{-I-}, these directories are searched for all @samp{#include} ! 2269: directives. ! 2270: ! 2271: In addition, the @samp{-I-} option inhibits the use of the current ! 2272: directory as the first search directory for @samp{#include "@var{file}"}. ! 2273: Therefore, the current directory is searched only if it is requested ! 2274: explicitly with @samp{-I.}. Specifying both @samp{-I-} and @samp{-I.} ! 2275: allows you to control precisely which directories are searched before ! 2276: the current one and which are searched after. ! 2277: ! 2278: @item -nostdinc ! 2279: Do not search the standard system directories for header files. ! 2280: Only the directories you have specified with @samp{-I} options ! 2281: (and the current directory, if appropriate) are searched. ! 2282: ! 2283: @item -D @var{name} ! 2284: @findex -D ! 2285: Predefine @var{name} as a macro, with definition @samp{1}. ! 2286: ! 2287: @item -D @var{name}=@var{definition} ! 2288: Predefine @var{name} as a macro, with definition @var{definition}. ! 2289: There are no restrictions on the contents of @var{definition}, but if ! 2290: you are invoking the preprocessor from a shell or shell-like program you ! 2291: may need to use the shell's quoting syntax to protect characters such as ! 2292: spaces that have a meaning in the shell syntax. If you use more than ! 2293: one @samp{-D} for the same @var{name}, the rightmost definition takes ! 2294: effect. ! 2295: ! 2296: @item -U @var{name} ! 2297: @findex -U ! 2298: Do not predefine @var{name}. If both @samp{-U} and @samp{-D} are ! 2299: specified for one name, the @samp{-U} beats the @samp{-D} and the name ! 2300: is not predefined. ! 2301: ! 2302: @item -undef ! 2303: @findex -undef ! 2304: Do not predefine any nonstandard macros. ! 2305: ! 2306: @item -dM ! 2307: @findex -dM ! 2308: Instead of outputting the result of preprocessing, output a list of ! 2309: @samp{#define} commands for all the macros defined during the ! 2310: execution of the preprocessor, including predefined macros. This gives ! 2311: you a way of finding out what is predefined in your version of the ! 2312: preprocessor; assuming you have no file @samp{foo.h}, the command ! 2313: ! 2314: @example ! 2315: touch foo.h; cpp -dM foo.h ! 2316: @end example ! 2317: ! 2318: @noindent ! 2319: will show the values of any predefined macros. ! 2320: ! 2321: @item -dD ! 2322: @findex -dD ! 2323: Like @samp{-dM} except in two respects: it does @emph{not} include the ! 2324: predefined macros, and it outputs @emph{both} the @samp{#define} ! 2325: commands and the result of preprocessing. Both kinds of output go to ! 2326: the standard output file. ! 2327: ! 2328: @item -M ! 2329: @findex -M ! 2330: Instead of outputting the result of preprocessing, output a rule ! 2331: suitable for @code{make} describing the dependencies of the main ! 2332: source file. The preprocessor outputs one @code{make} rule containing ! 2333: the object file name for that source file, a colon, and the names of ! 2334: all the included files. If there are many included files then the ! 2335: rule is split into several lines using @samp{\}-newline. ! 2336: ! 2337: This feature is used in automatic updating of makefiles. ! 2338: ! 2339: @item -MM ! 2340: @findex -MM ! 2341: Like @samp{-M} but mention only the files included with @samp{#include ! 2342: "@var{file}"}. System header files included with @samp{#include ! 2343: <@var{file}>} are omitted. ! 2344: ! 2345: @item -MD ! 2346: @findex -MD ! 2347: Like @samp{-M} but the dependency information is written to files with ! 2348: names made by replacing @samp{.c} with @samp{.d} at the end of the ! 2349: input file names. This is in addition to compiling the file as ! 2350: specified---@samp{-MD} does not inhibit ordinary compilation the way ! 2351: @samp{-M} does. ! 2352: ! 2353: In Mach, you can use the utility @code{md} to merge the @samp{.d} files ! 2354: into a single dependency file suitable for using with the @samp{make} ! 2355: command. ! 2356: ! 2357: @item -MMD ! 2358: @findex -MMD ! 2359: Like @samp{-MD} except mention only user header files, not system ! 2360: header files. ! 2361: ! 2362: @item -H ! 2363: @findex -H ! 2364: Print the name of each header file used, in addition to other normal ! 2365: activities. ! 2366: ! 2367: @item -imacros @var{file} ! 2368: @findex -imacros ! 2369: Process @var{file} as input, discarding the resulting output, before ! 2370: processing the regular input file. Because the output generated from ! 2371: @var{file} is discarded, the only effect of @samp{-imacros @var{file}} ! 2372: is to make the macros defined in @var{file} available for use in the ! 2373: main input. ! 2374: ! 2375: @item -include @var{file} ! 2376: @findex -include ! 2377: Process @var{file} as input, and include all the resulting output, ! 2378: before processing the regular input file. ! 2379: ! 2380: @item -lang-c ! 2381: @itemx -lang-c++ ! 2382: @itemx -lang-objc ! 2383: @itemx -lang-objc++ ! 2384: Specify the source language. @samp{-lang-c++} makes the preprocessor ! 2385: handle C++ comment syntax, and includes extra default include ! 2386: directories for C++, and @samp{-lang-objc} enables the Objective C ! 2387: @samp{#import} directive. @samp{-lang-c} explicitly turns off both of ! 2388: these extensions, and @samp{-lang-objc++} enables both. ! 2389: ! 2390: These options are generated by the compiler driver @code{gcc}, but not ! 2391: passed from the @samp{gcc} command line. ! 2392: ! 2393: @item -lint ! 2394: Look for commands to the program checker @code{lint} embedded in ! 2395: comments, and emit them preceded by @samp{#pragma lint}. For example, ! 2396: the comment @samp{/* NOTREACHED */} becomes @samp{#pragma lint ! 2397: NOTREACHED}. ! 2398: ! 2399: This option is available only when you call @code{cpp} directly; ! 2400: @code{gcc} will not pass it from its command line. ! 2401: ! 2402: @item -$ ! 2403: Forbid the use of @samp{$} in identifiers. This is required for ANSI ! 2404: conformance. @code{gcc} automatically supplies this option to the ! 2405: preprocessor if you specify @samp{-ansi}, but @code{gcc} doesn't ! 2406: recognize the @samp{-$} option itself---to use it without the other ! 2407: effects of @samp{-ansi}, you must call the preprocessor directly. ! 2408: ! 2409: @end table ! 2410: ! 2411: @node Concept Index, Index, Invocation, Top ! 2412: @unnumbered Concept Index ! 2413: @printindex cp ! 2414: ! 2415: @node Index,, Concept Index, Top ! 2416: @unnumbered Index of Commands, Macros and Options ! 2417: @printindex fn ! 2418: ! 2419: @contents ! 2420: @bye
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.