📄 cpp.html
字号:
<H3><A NAME="SEC12" HREF="cpp_toc.html#TOC12">Predefined Macros</A></H3><P><A NAME="IDX22"></A>Several simple macros are predefined. You can use them without givingdefinitions for them. They fall into two classes: standard macros andsystem-specific macros.</P><H4><A NAME="SEC13" HREF="cpp_toc.html#TOC13">Standard Predefined Macros</A></H4><P><A NAME="IDX23"></A></P><P>The standard predefined macros are available with the same meaningsregardless of the machine or operating system on which you are using GNU C.Their names all start and end with double underscores. Those preceding<CODE>__GNUC__</CODE> in this table are standardized by ANSI C; the rest areGNU C extensions.</P><DL COMPACT><DT><CODE>__FILE__</CODE><DD><A NAME="IDX24"></A>This macro expands to the name of the current input file, in the form ofa C string constant. The precise name returned is the one that wasspecified in <SAMP>`#include'</SAMP> or as the input file name argument.<DT><CODE>__LINE__</CODE><DD><A NAME="IDX25"></A>This macro expands to the current input line number, in the form of adecimal integer constant. While we call it a predefined macro, it'sa pretty strange macro, since its "definition" changes with eachnew line of source code.This and <SAMP>`__FILE__'</SAMP> are useful in generating an error message toreport an inconsistency detected by the program; the message can statethe source line at which the inconsistency was detected. For example,<PRE>fprintf (stderr, "Internal error: " "negative string length " "%d at %s, line %d.", length, __FILE__, __LINE__);</PRE>A <SAMP>`#include'</SAMP> directive changes the expansions of <SAMP>`__FILE__'</SAMP>and <SAMP>`__LINE__'</SAMP> to correspond to the included file. At the end ofthat file, when processing resumes on the input file that containedthe <SAMP>`#include'</SAMP> directive, the expansions of <SAMP>`__FILE__'</SAMP> and<SAMP>`__LINE__'</SAMP> revert to the values they had before the<SAMP>`#include'</SAMP> (but <SAMP>`__LINE__'</SAMP> is then incremented by one asprocessing moves to the line after the <SAMP>`#include'</SAMP>).The expansions of both <SAMP>`__FILE__'</SAMP> and <SAMP>`__LINE__'</SAMP> are alteredif a <SAMP>`#line'</SAMP> directive is used. See section <A HREF="cpp.html#SEC38">Combining Source Files</A>.<DT><CODE>__DATE__</CODE><DD><A NAME="IDX26"></A>This macro expands to a string constant that describes the date onwhich the preprocessor is being run. The string constant containseleven characters and looks like <SAMP>`"Feb 1 1996"'</SAMP>.<DT><CODE>__TIME__</CODE><DD><A NAME="IDX27"></A>This macro expands to a string constant that describes the time atwhich the preprocessor is being run. The string constant containseight characters and looks like <SAMP>`"23:59:01"'</SAMP>.<DT><CODE>__STDC__</CODE><DD><A NAME="IDX28"></A>This macro expands to the constant 1, to signify that this is ANSIStandard C. (Whether that is actually true depends on what C compilerwill operate on the output from the preprocessor.)<DT><CODE>__STDC_VERSION__</CODE><DD><A NAME="IDX29"></A>This macro expands to the C Standard's version number,a long integer constant of the form <SAMP>`<VAR>yyyy</VAR><VAR>mm</VAR>L'</SAMP>where <VAR>yyyy</VAR> and <VAR>mm</VAR> are the year and month of the Standard version.This signifies which version of the C Standard the preprocessor conforms to.Like <SAMP>`__STDC__'</SAMP>, whether this version number is accuratefor the entire implementation depends on what C compilerwill operate on the output from the preprocessor.<DT><CODE>__GNUC__</CODE><DD><A NAME="IDX30"></A>This macro is defined if and only if this is GNU C. This macro isdefined only when the entire GNU C compiler is in use; if you invoke thepreprocessor directly, <SAMP>`__GNUC__'</SAMP> is undefined. The valueidentifies the major version number of GNU CC (<SAMP>`1'</SAMP> for GNU CCversion 1, which is now obsolete, and <SAMP>`2'</SAMP> for version 2).<DT><CODE>__GNUC_MINOR__</CODE><DD><A NAME="IDX31"></A>The macro contains the minor version number of the compiler. This canbe used to work around differences between different releases of thecompiler (for example, if gcc 2.6.3 is known to support a feature, youcan test for <CODE>__GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 6)</CODE>).The last number, <SAMP>`3'</SAMP> in theexample above, denotes the bugfix level of the compiler; no macrocontains this value.<DT><CODE>__GNUG__</CODE><DD><A NAME="IDX32"></A>The GNU C compiler defines this when the compilation language isC++; use <SAMP>`__GNUG__'</SAMP> to distinguish between GNU C and GNUC++.<DT><CODE>__cplusplus</CODE><DD><A NAME="IDX33"></A>The draft ANSI standard for C++ used to require predefining thisvariable. Though it is no longer required, GNU C++ continues to defineit, as do other popular C++ compilers. You can use <SAMP>`__cplusplus'</SAMP>to test whether a header is compiled by a C compiler or a C++ compiler.<DT><CODE>__STRICT_ANSI__</CODE><DD><A NAME="IDX34"></A>This macro is defined if and only if the <SAMP>`-ansi'</SAMP> switch wasspecified when GNU C was invoked. Its definition is the null string.This macro exists primarily to direct certain GNU header files not todefine certain traditional Unix constructs which are incompatible withANSI C.<DT><CODE>__BASE_FILE__</CODE><DD><A NAME="IDX35"></A>This macro expands to the name of the main input file, in the formof a C string constant. This is the source file that was specifiedas an argument when the C compiler was invoked.<DT><CODE>__INCLUDE_LEVEL__</CODE><DD><A NAME="IDX36"></A>This macro expands to a decimal integer constant that represents thedepth of nesting in include files. The value of this macro isincremented on every <SAMP>`#include'</SAMP> directive and decremented at everyend of file. For input files specified by command line arguments,the nesting level is zero.<DT><CODE>__VERSION__</CODE><DD><A NAME="IDX37"></A>This macro expands to a string which describes the version number ofGNU C. The string is normally a sequence of decimal numbers separatedby periods, such as <SAMP>`"2.6.0"'</SAMP>. The only reasonable use of thismacro is to incorporate it into a string constant.<DT><CODE>__OPTIMIZE__</CODE><DD><A NAME="IDX38"></A>This macro is defined in optimizing compilations. It causes certainGNU header files to define alternative macro definitions for somesystem library functions. It is unwise to refer to or test thedefinition of this macro unless you make very sure that programs willexecute with the same effect regardless.<DT><CODE>__CHAR_UNSIGNED__</CODE><DD><A NAME="IDX39"></A>This macro is defined if and only if the data type <CODE>char</CODE> isunsigned on the target machine. It exists to cause the standardheader file <TT>`limit.h'</TT> to work correctly. It is bad practiceto refer to this macro yourself; instead, refer to the standardmacros defined in <TT>`limit.h'</TT>. The preprocessor usesthis macro to determine whether or not to sign-extend large characterconstants written in octal; see section <A HREF="cpp.html#SEC31">The <SAMP>`#if'</SAMP> Directive</A>.<DT><CODE>__REGISTER_PREFIX__</CODE><DD><A NAME="IDX40"></A>This macro expands to a string describing the prefix applied to cpuregisters in assembler code. It can be used to write assembler codethat is usable in multiple environments. For example, in the<SAMP>`m68k-aout'</SAMP> environment it expands to the string <SAMP>`""'</SAMP>,but in the <SAMP>`m68k-coff'</SAMP> environment it expands to the string<SAMP>`"%"'</SAMP>.<DT><CODE>__USER_LABEL_PREFIX__</CODE><DD><A NAME="IDX41"></A>This macro expands to a string describing the prefix applied touser generated labels in assembler code. It can be used to writeassembler code that is usable in multiple environments.For example, in the <SAMP>`m68k-aout'</SAMP> environment it expands to thestring <SAMP>`"_"'</SAMP>, but in the <SAMP>`m68k-coff'</SAMP> environment it expandsto the string <SAMP>`""'</SAMP>.</DL><H4><A NAME="SEC14" HREF="cpp_toc.html#TOC14">Nonstandard Predefined Macros</A></H4><P>The C preprocessor normally has several predefined macros that vary betweenmachines because their purpose is to indicate what type of system andmachine is in use. This manual, being for all systems and machines, cannottell you exactly what their names are; instead, we offer a list of sometypical ones. You can use <SAMP>`cpp -dM'</SAMP> to see the values ofpredefined macros; see section <A HREF="cpp.html#SEC41">Invoking the C Preprocessor</A>.</P><P>Some nonstandard predefined macros describe the operating system in use,with more or less specificity. For example,</P><DL COMPACT><DT><CODE>unix</CODE><DD><A NAME="IDX42"></A><SAMP>`unix'</SAMP> is normally predefined on all Unix systems.<DT><CODE>BSD</CODE><DD><A NAME="IDX43"></A><SAMP>`BSD'</SAMP> is predefined on recent versions of Berkeley Unix(perhaps only in version 4.3).</DL><P>Other nonstandard predefined macros describe the kind of CPU, with more orless specificity. For example,</P><DL COMPACT><DT><CODE>vax</CODE><DD><A NAME="IDX44"></A><SAMP>`vax'</SAMP> is predefined on Vax computers.<DT><CODE>mc68000</CODE><DD><A NAME="IDX45"></A><SAMP>`mc68000'</SAMP> is predefined on most computers whose CPU is a Motorola68000, 68010 or 68020.<DT><CODE>m68k</CODE><DD><A NAME="IDX46"></A><SAMP>`m68k'</SAMP> is also predefined on most computers whose CPU is a 68000,68010 or 68020; however, some makers use <SAMP>`mc68000'</SAMP> and some use<SAMP>`m68k'</SAMP>. Some predefine both names. What happens in GNU Cdepends on the system you are using it on.<DT><CODE>M68020</CODE><DD><A NAME="IDX47"></A><SAMP>`M68020'</SAMP> has been observed to be predefined on some systems thatuse 68020 CPUs--in addition to <SAMP>`mc68000'</SAMP> and <SAMP>`m68k'</SAMP>, whichare less specific.<DT><CODE>_AM29K</CODE><DD><A NAME="IDX48"></A><DT><CODE>_AM29000</CODE><DD><A NAME="IDX49"></A>Both <SAMP>`_AM29K'</SAMP> and <SAMP>`_AM29000'</SAMP> are predefined for the AMD 29000CPU family.<DT><CODE>ns32000</CODE><DD><A NAME="IDX50"></A><SAMP>`ns32000'</SAMP> is predefined on computers which use the NationalSemiconductor 32000 series CPU.</DL><P>Yet other nonstandard predefined macros describe the manufacturer ofthe system. For example,</P><DL COMPACT><DT><CODE>sun</CODE><DD><A NAME="IDX51"></A><SAMP>`sun'</SAMP> is predefined on all models of Sun computers.<DT><CODE>pyr</CODE><DD><A NAME="IDX52"></A><SAMP>`pyr'</SAMP> is predefined on all models of Pyramid computers.<DT><CODE>sequent</CODE><DD><A NAME="IDX53"></A><SAMP>`sequent'</SAMP> is predefined on all models of Sequent computers.</DL><P>These predefined symbols are not only nonstandard, they are contrary to theANSI standard because their names do not start with underscores.Therefore, the option <SAMP>`-ansi'</SAMP> inhibits the definition of thesesymbols.</P><P>This tends to make <SAMP>`-ansi'</SAMP> useless, since many programs depend on thecustomary nonstandard predefined symbols. Even system header files checkthem and will generate incorrect declarations if they do not find the namesthat are expected. You might think that the header files supplied for theUglix computer would not need to test what machine they are running on,because they can simply assume it is the Uglix; but often they do, and theydo so using the customary names. As a result, very few C programs willcompile with <SAMP>`-ansi'</SAMP>. We intend to avoid such problems on the GNUsystem.</P><P>What, then, should you do in an ANSI C program to test the type of machineit will run on?</P><P>GNU C offers a parallel series of symbols for this purpose, whose namesare made from the customary ones by adding <SAMP>`__'</SAMP> at the beginningand end. Thus, the symbol <CODE>__vax__</CODE> would be available on a Vax,and so on.</P><P>The set of nonstandard predefined names in the GNU C preprocessor iscontrolled (when <CODE>cpp</CODE> is itself compiled) by the macro<SAMP>`CPP_PREDEFINES'</SAMP>, which should be a string containing <SAMP>`-D'</SAMP>options, separated by spaces. For example, on the Sun 3, we use thefollowing definition:</P><PRE>#define CPP_PREDEFINES "-Dmc68000 -Dsun -Dunix -Dm68k"</PRE><P>This macro is usually specified in <TT>`tm.h'</TT>.</P><H3><A NAME="SEC15" HREF="cpp_toc.html#TOC15">Stringification</A></H3><P><A NAME="IDX54"></A><EM>Stringification</EM> means turning a code fragment into a string constantwhose contents are the text for the code fragment. For example,stringifying <SAMP>`foo (z)'</SAMP> results in <SAMP>`"foo (z)"'</SAMP>.</P><P>In the C preprocessor, stringification is an option available when macroarguments are substituted into the macro definition. In the body of thedefinition, when an argument name appears, the character <SAMP>`#'</SAMP> beforethe name specifies stringification of the corresponding actual argumentwhen it is substituted at that point in the definition. The same argumentmay be substituted in other places in the definition withoutstringification if the argument name appears in those places with no<SAMP>`#'</SAMP>.</P><P>Here is an example of a macro definition that uses stringification:</P><PRE>#define WARN_IF(EXP) \do { if (EXP) \ fprintf (stderr, "Warning: " #EXP "\n"); } \while (0)</PRE><P>Here the actual argument for <SAMP>`EXP'</SAMP> is substituted once as given,into the <SAMP>`if'</SAMP> statement, and once as stringified, into theargument to <SAMP>`fprintf'</SAMP>. The <SAMP>`do'</SAMP> and <SAMP>`while (0)'</SAMP> area kludge to make it possible to write <SAMP>`WARN_IF (<VAR>arg</VAR>);'</SAMP>,which the resemblance of <SAMP>`WARN_IF'</SAMP> to a function would makeC programmers want to do; see section <A HREF="cpp.html#SEC22">Swallowing the Semicolon</A>.</P><P>The stringification feature is limited to transforming one macro argumentinto one string constant: there is no way to combine the argument withother text and then stringify it all together. But the example above showshow an equivalent result can be obtained in ANSI Standard C using thefeature that adjacent string constants are concatenated as one stringconstant. The preprocessor stringifies the actual value of <SAMP>`EXP'</SAMP> into a separate string constant, resulting in text like</P><PRE>do { if (x == 0) \ fprintf (stderr, "Warning: " "x == 0" "\n"); } \while (0)</PRE><P>but the C compiler then sees three consecutive string constants andconcatenates them into one, producing effectively</P>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -