📄 cpp.texi
字号:
\input texinfo@setfilename cpp.info@settitle The C Preprocessor@ignore@ifinfo@formatSTART-INFO-DIR-ENTRY* Cpp: (cpp). The C preprocessor.END-INFO-DIR-ENTRY@end format@end ifinfo@end ignore@c @smallbook@c @cropmarks@c @finalout@setchapternewpage odd@ifinfoThis file documents the GNU C Preprocessor.Copyright 1987, 1989, 1991, 1992, 1993, 1994, 1995 Free SoftwareFoundation, Inc.Permission is granted to make and distribute verbatim copies ofthis manual provided the copyright notice and this permission noticeare preserved on all copies.@ignorePermission is granted to process this file through Tex and print theresults, provided the printed document carries copying permissionnotice identical to this one except for the removal of this paragraph(this paragraph not being relevant to the printed manual).@end ignorePermission is granted to copy and distribute modified versions of thismanual under the conditions for verbatim copying, provided also thatthe entire resulting derived work is distributed under the terms of apermission notice identical to this one.Permission is granted to copy and distribute translations of this manualinto another language, under the above conditions for modified versions.@end ifinfo@titlepage@c @finalout@title The C Preprocessor@subtitle Last revised July 1992@subtitle for GCC version 2@author Richard M. Stallman@page@vskip 2pcThis booklet is eventually intended to form the first chapter of a GNU C Language manual.@vskip 0pt plus 1filllCopyright @copyright{} 1987, 1989, 1991, 1992, 1993, 1994, 1995 FreeSoftware Foundation, Inc.Permission is granted to make and distribute verbatim copies ofthis manual provided the copyright notice and this permission noticeare preserved on all copies.Permission is granted to copy and distribute modified versions of thismanual under the conditions for verbatim copying, provided also thatthe entire resulting derived work is distributed under the terms of apermission notice identical to this one.Permission is granted to copy and distribute translations of this manualinto another language, under the above conditions for modified versions.@end titlepage@page@node Top, Global Actions,, (DIR)@chapter The C PreprocessorThe C preprocessor is a @dfn{macro processor} that is used automatically bythe C compiler to transform your program before actual compilation. It iscalled a macro processor because it allows you to define @dfn{macros},which are brief abbreviations for longer constructs.The C preprocessor provides four separate facilities that you can use asyou see fit:@itemize @bullet@itemInclusion of header files. These are files of declarations that can besubstituted into your program.@itemMacro expansion. You can define @dfn{macros}, which are abbreviationsfor arbitrary fragments of C code, and then the C preprocessor willreplace the macros with their definitions throughout the program.@itemConditional compilation. Using special preprocessing directives, youcan include or exclude parts of the program according to variousconditions.@itemLine control. If you use a program to combine or rearrange source files intoan intermediate file which is then compiled, you can use line controlto inform the compiler of where each source line originally came from.@end itemizeC preprocessors vary in some details. This manual discusses the GNU Cpreprocessor, the C Compatible Compiler Preprocessor. The GNU Cpreprocessor provides a superset of the features of ANSI Standard C.ANSI Standard C requires the rejection of many harmless constructs commonlyused by today's C programs. Such incompatibility would be inconvenient forusers, so the GNU C preprocessor is configured to accept these constructsby default. Strictly speaking, to get ANSI Standard C, you must use theoptions @samp{-trigraphs}, @samp{-undef} and @samp{-pedantic}, but inpractice the consequences of having strict ANSI Standard C make itundesirable to do this. @xref{Invocation}.@menu* Global Actions:: Actions made uniformly on all input files.* Directives:: General syntax of preprocessing directives.* Header Files:: How and why to use header files.* Macros:: How and why to use macros.* Conditionals:: How and why to use conditionals.* Combining Sources:: Use of line control when you combine source files.* Other Directives:: Miscellaneous preprocessing directives.* Output:: Format of output from the C preprocessor.* Invocation:: How to invoke the preprocessor; command options.* Concept Index:: Index of concepts and terms.* Index:: Index of directives, predefined macros and options.@end menu@node Global Actions, Directives, Top, Top@section Transformations Made GloballyMost C preprocessor features are inactive unless you give specific directivesto request their use. (Preprocessing directives are lines starting with@samp{#}; @pxref{Directives}). But there are three transformations that thepreprocessor always makes on all the input it receives, even in the absenceof directives.@itemize @bullet@itemAll C comments are replaced with single spaces.@itemBackslash-Newline sequences are deleted, no matter where. Thisfeature allows you to break long lines for cosmetic purposes withoutchanging their meaning.@itemPredefined macro names are replaced with their expansions(@pxref{Predefined}).@end itemizeThe first two transformations are done @emph{before} nearly all other parsingand before preprocessing directives are recognized. Thus, for example, youcan split a line cosmetically with Backslash-Newline anywhere (exceptwhen trigraphs are in use; see below).@example/**/ # /**/ defi\ne FO\O 10\20@end example@noindentis equivalent into @samp{#define FOO 1020}. You can split even an escapesequence with Backslash-Newline. For example, you can split @code{"foo\bar"}between the @samp{\} and the @samp{b} to get@example"foo\\bar"@end example@noindentThis behavior is unclean: in all other contexts, a Backslash can beinserted in a string constant as an ordinary character by writing a doubleBackslash, and this creates an exception. But the ANSI C standard requiresit. (Strict ANSI C does not allow Newlines in string constants, so theydo not consider this a problem.)But there are a few exceptions to all three transformations.@itemize @bullet@itemC comments and predefined macro names are not recognized inside a@samp{#include} directive in which the file name is delimited with@samp{<} and @samp{>}.@itemC comments and predefined macro names are never recognized within acharacter or string constant. (Strictly speaking, this is the rule,not an exception, but it is worth noting here anyway.)@itemBackslash-Newline may not safely be used within an ANSI ``trigraph''.Trigraphs are converted before Backslash-Newline is deleted. If youwrite what looks like a trigraph with a Backslash-Newline inside, theBackslash-Newline is deleted as usual, but it is then too late torecognize the trigraph.This exception is relevant only if you use the @samp{-trigraphs}option to enable trigraph processing. @xref{Invocation}.@end itemize@node Directives, Header Files, Global Actions, Top@section Preprocessing Directives@cindex preprocessing directives@cindex directivesMost preprocessor features are active only if you use preprocessing directivesto request their use.Preprocessing directives are lines in your program that start with @samp{#}.The @samp{#} is followed by an identifier that is the @dfn{directive name}.For example, @samp{#define} is the directive that defines a macro.Whitespace is also allowed before and after the @samp{#}.The set of valid directive names is fixed. Programs cannot define newpreprocessing directives.Some directive names require arguments; these make up the rest of the directiveline and must be separated from the directive name by whitespace. For example,@samp{#define} must be followed by a macro name and the intended expansionof the macro. @xref{Simple Macros}.A preprocessing directive cannot be more than one line in normal circumstances.It may be split cosmetically with Backslash-Newline, but that has no effecton its meaning. Comments containing Newlines can also divide thedirective into multiple lines, but the comments are changed to Spacesbefore the directive is interpreted. The only way a significant Newlinecan occur in a preprocessing directive is within a string constant orcharacter constant. Note thatmost C compilers that might be applied to the output from the preprocessordo not accept string or character constants containing Newlines.The @samp{#} and the directive name cannot come from a macro expansion. Forexample, if @samp{foo} is defined as a macro expanding to @samp{define},that does not make @samp{#foo} a valid preprocessing directive.@node Header Files, Macros, Directives, Top@section Header Files@cindex header fileA header file is a file containing C declarations and macro definitions(@pxref{Macros}) to be shared between several source files. You requestthe use of a header file in your program with the C preprocessing directive@samp{#include}.@menu* Header Uses:: What header files are used for.* Include Syntax:: How to write @samp{#include} directives.* Include Operation:: What @samp{#include} does.* Once-Only:: Preventing multiple inclusion of one header file.* Inheritance:: Including one header file in another header file.@end menu@node Header Uses, Include Syntax, Header Files, Header Files@subsection Uses of Header FilesHeader files serve two kinds of purposes.@itemize @bullet@item@findex system header filesSystem header files declare the interfaces to parts of the operatingsystem. You include them in your program to supply the definitions anddeclarations you need to invoke system calls and libraries.@itemYour own header files contain declarations for interfaces between thesource files of your program. Each time you have a group of relateddeclarations and macro definitions all or most of which are needed inseveral different source files, it is a good idea to create a headerfile for them.@end itemizeIncluding a header file produces the same results in C compilation ascopying the header file into each source file that needs it. But suchcopying would be time-consuming and error-prone. With a header file, therelated declarations appear in only one place. If they need to be changed,they can be changed in one place, and programs that include the header filewill automatically use the new version when next recompiled. The headerfile eliminates the labor of finding and changing all the copies as well asthe risk that a failure to find one copy will result in inconsistencieswithin a program.The usual convention is to give header files names that end with@file{.h}. Avoid unusual characters in header file names, as theyreduce portability.@node Include Syntax, Include Operation, Header Uses, Header Files@subsection The @samp{#include} Directive@findex #includeBoth user and system header files are included using the preprocessingdirective @samp{#include}. It has three variants:@table @code@item #include <@var{file}>This variant is used for system header files. It searches for a filenamed @var{file} in a list of directories specified by you, then in astandard list of system directories. You specify directories tosearch for header files with the command option @samp{-I}(@pxref{Invocation}). The option @samp{-nostdinc} inhibits searchingthe standard system directories; in this case only the directoriesyou specify are searched.The parsing of this form of @samp{#include} is slightly specialbecause comments are not recognized within the @samp{<@dots{}>}.Thus, in @samp{#include <x/*y>} the @samp{/*} does not start a commentand the directive specifies inclusion of a system header file named@file{x/*y}. Of course, a header file with such a name is unlikely toexist on Unix, where shell wildcard features would make it hard tomanipulate.@refillThe argument @var{file} may not contain a @samp{>} character. It may,however, contain a @samp{<} character.@item #include "@var{file}"This variant is used for header files of your own program. Itsearches for a file named @var{file} first in the current directory,then in the same directories used for system header files. Thecurrent directory is the directory of the current input file. It istried first because it is presumed to be the location of the filesthat the current input file refers to. (If the @samp{-I-} option isused, the special treatment of the current directory is inhibited.)The argument @var{file} may not contain @samp{"} characters. Ifbackslashes occur within @var{file}, they are considered ordinary textcharacters, not escape characters. None of the character escapesequences appropriate to string constants in C are processed. Thus,@samp{#include "x\n\\y"} specifies a filename containing threebackslashes. It is not clear why this behavior is ever useful, butthe ANSI standard specifies it.@item #include @var{anything else}@cindex computed @samp{#include}This variant is called a @dfn{computed #include}. Any @samp{#include}directive whose argument does not fit the above two forms is a computedinclude. The text @var{anything else} is checked for macro calls,which are expanded (@pxref{Macros}). When this is done, the result
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -