⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cpp.texi

📁 一个能在WIN32环境下使用GCC的功能进行程序设计的SHE
💻 TEXI
📖 第 1 页 / 共 5 页
字号:
where the current file was found.

The use of @samp{#include_next} can lead to great confusion.  We
recommend it be used only when there is no other alternative.  In
particular, it should not be used in the headers belonging to a
specific program, only to make global corrections along the lines
of @command{fixincludes}.

@node System Headers
@section System Headers
@cindex system header files

The header files declaring interfaces to the operating system and
runtime libraries often cannot be written in strictly conforming C@.
Therefore, GCC gives code found in @dfn{system headers} special
treatment.  All warnings, other than those generated by @samp{#warning}
(@pxref{Diagnostics}), are suppressed while GCC is processing a system
header.  Macros defined in a system header are immune to a few warnings
wherever they are expanded.  This immunity is granted on an ad-hoc
basis, when we find that a warning generates lots of false positives
because of code in macros defined in system headers.

Normally, only the headers found in specific directories are considered
system headers.  These directories are determined when GCC is compiled.
There are, however, two ways to make normal headers into system headers.

The @option{-isystem} command line option adds its argument to the list of
directories to search for headers, just like @option{-I}.  Any headers
found in that directory will be considered system headers.  Note that
unlike @option{-I}, you must put a space between @option{-isystem} and its
argument.

All directories named by @option{-isystem} are searched @emph{after} all
directories named by @option{-I}, no matter what their order was on the
command line.  If the same directory is named by both @option{-I} and
@option{-isystem}, @option{-I} wins; it is as if the @option{-isystem} option
had never been specified at all.

@findex #pragma GCC system_header
There is also a directive, @code{@w{#pragma GCC system_header}}, which
tells GCC to consider the rest of the current include file a system
header, no matter where it was found.  Code that comes before the
@samp{#pragma} in the file will not be affected.  @code{@w{#pragma GCC
system_header}} has no effect in the primary source file.

On very old systems, some of the pre-defined system header directories
get even more special treatment.  GNU C++ considers code in headers
found in those directories to be surrounded by an @code{@w{extern "C"}}
block.  There is no way to request this behavior with a @samp{#pragma},
or from the command line.

@node Macros
@chapter Macros

A @dfn{macro} is a fragment of code which has been given a name.
Whenever the name is used, it is replaced by the contents of the macro.
There are two kinds of macros.  They differ mostly in what they look
like when they are used.  @dfn{Object-like} macros resemble data objects
when used, @dfn{function-like} macros resemble function calls.

You may define any valid identifier as a macro, even if it is a C
keyword.  The preprocessor does not know anything about keywords.  This
can be useful if you wish to hide a keyword such as @code{const} from an
older compiler that does not understand it.  However, the preprocessor
operator @code{defined} (@pxref{Defined}) can never be defined as a
macro, and C++'s named operators (@pxref{C++ Named Operators}) cannot be
macros when you are compiling C++.

@menu
* Object-like Macros::
* Function-like Macros::
* Macro Arguments::
* Stringification::
* Concatenation::
* Variadic Macros::
* Predefined Macros::
* Undefining and Redefining Macros::
* Macro Pitfalls::
@end menu

@node Object-like Macros
@section Object-like Macros
@cindex object-like macro
@cindex symbolic constants
@cindex manifest constants

An @dfn{object-like macro} is a simple identifier which will be replaced
by a code fragment.  It is called object-like because it looks like a
data object in code that uses it.  They are most commonly used to give
symbolic names to numeric constants.

@findex #define
You create macros with the @samp{#define} directive.  @samp{#define} is
followed by the name of the macro and then the token sequence it should
be an abbreviation for, which is variously referred to as the macro's
@dfn{body}, @dfn{expansion} or @dfn{replacement list}.  For example,

@example
#define BUFFER_SIZE 1024
@end example

@noindent
defines a macro named @code{BUFFER_SIZE} as an abbreviation for the
token @code{1024}.  If somewhere after this @samp{#define} directive
there comes a C statement of the form

@example
foo = (char *) malloc (BUFFER_SIZE);
@end example

@noindent
then the C preprocessor will recognize and @dfn{expand} the macro
@code{BUFFER_SIZE}.  The C compiler will see the same tokens as it would
if you had written

@example
foo = (char *) malloc (1024);
@end example

By convention, macro names are written in upper case.  Programs are
easier to read when it is possible to tell at a glance which names are
macros.

The macro's body ends at the end of the @samp{#define} line.  You may
continue the definition onto multiple lines, if necessary, using
backslash-newline.  When the macro is expanded, however, it will all
come out on one line.  For example,

@example
#define NUMBERS 1, \
                2, \
                3
int x[] = @{ NUMBERS @};
     @expansion{} int x[] = @{ 1, 2, 3 @};
@end example

@noindent
The most common visible consequence of this is surprising line numbers
in error messages.

There is no restriction on what can go in a macro body provided it
decomposes into valid preprocessing tokens.  Parentheses need not
balance, and the body need not resemble valid C code.  (If it does not,
you may get error messages from the C compiler when you use the macro.)

The C preprocessor scans your program sequentially.  Macro definitions
take effect at the place you write them.  Therefore, the following input
to the C preprocessor

@example
foo = X;
#define X 4
bar = X;
@end example

@noindent
produces

@example
foo = X;
bar = 4;
@end example

When the preprocessor expands a macro name, the macro's expansion
replaces the macro invocation, then the expansion is examined for more
macros to expand.  For example,

@example
@group
#define TABLESIZE BUFSIZE
#define BUFSIZE 1024
TABLESIZE
     @expansion{} BUFSIZE
     @expansion{} 1024
@end group
@end example

@noindent
@code{TABLESIZE} is expanded first to produce @code{BUFSIZE}, then that
macro is expanded to produce the final result, @code{1024}.

Notice that @code{BUFSIZE} was not defined when @code{TABLESIZE} was
defined.  The @samp{#define} for @code{TABLESIZE} uses exactly the
expansion you specify---in this case, @code{BUFSIZE}---and does not
check to see whether it too contains macro names.  Only when you
@emph{use} @code{TABLESIZE} is the result of its expansion scanned for
more macro names.

This makes a difference if you change the definition of @code{BUFSIZE}
at some point in the source file.  @code{TABLESIZE}, defined as shown,
will always expand using the definition of @code{BUFSIZE} that is
currently in effect:

@example
#define BUFSIZE 1020
#define TABLESIZE BUFSIZE
#undef BUFSIZE
#define BUFSIZE 37
@end example

@noindent
Now @code{TABLESIZE} expands (in two stages) to @code{37}.

If the expansion of a macro contains its own name, either directly or
via intermediate macros, it is not expanded again when the expansion is
examined for more macros.  This prevents infinite recursion.
@xref{Self-Referential Macros}, for the precise details.

@node Function-like Macros
@section Function-like Macros
@cindex function-like macros

You can also define macros whose use looks like a function call.  These
are called @dfn{function-like macros}.  To define a function-like macro,
you use the same @samp{#define} directive, but you put a pair of
parentheses immediately after the macro name.  For example,

@example
#define lang_init()  c_init()
lang_init()
     @expansion{} c_init()
@end example

A function-like macro is only expanded if its name appears with a pair
of parentheses after it.  If you write just the name, it is left alone.
This can be useful when you have a function and a macro of the same
name, and you wish to use the function sometimes.

@example
extern void foo(void);
#define foo() /* optimized inline version */
@dots{}
  foo();
  funcptr = foo;
@end example

Here the call to @code{foo()} will use the macro, but the function
pointer will get the address of the real function.  If the macro were to
be expanded, it would cause a syntax error.

If you put spaces between the macro name and the parentheses in the
macro definition, that does not define a function-like macro, it defines
an object-like macro whose expansion happens to begin with a pair of
parentheses.

@example
#define lang_init ()    c_init()
lang_init()
     @expansion{} () c_init()()
@end example

The first two pairs of parentheses in this expansion come from the
macro.  The third is the pair that was originally after the macro
invocation.  Since @code{lang_init} is an object-like macro, it does not
consume those parentheses.

@node Macro Arguments
@section Macro Arguments
@cindex arguments
@cindex macros with arguments
@cindex arguments in macro definitions

Function-like macros can take @dfn{arguments}, just like true functions.
To define a macro that uses arguments, you insert @dfn{parameters}
between the pair of parentheses in the macro definition that make the
macro function-like.  The parameters must be valid C identifiers,
separated by commas and optionally whitespace.

To invoke a macro that takes arguments, you write the name of the macro
followed by a list of @dfn{actual arguments} in parentheses, separated
by commas.  The invocation of the macro need not be restricted to a
single logical line---it can cross as many lines in the source file as
you wish.  The number of arguments you give must match the number of
parameters in the macro definition.  When the macro is expanded, each
use of a parameter in its body is replaced by the tokens of the
corresponding argument.  (You need not use all of the parameters in the
macro body.)

As an example, here is a macro that computes the minimum of two numeric
values, as it is defined in many C programs, and some uses.

@example
#define min(X, Y)  ((X) < (Y) ? (X) : (Y))
  x = min(a, b);          @expansion{}  x = ((a) < (b) ? (a) : (b));
  y = min(1, 2);          @expansion{}  y = ((1) < (2) ? (1) : (2));
  z = min(a + 28, *p);    @expansion{}  z = ((a + 28) < (*p) ? (a + 28) : (*p));
@end example

@noindent
(In this small example you can already see several of the dangers of
macro arguments.  @xref{Macro Pitfalls}, for detailed explanations.)

Leading and trailing whitespace in each argument is dropped, and all
whitespace between the tokens of an argument is reduced to a single
space.  Parentheses within each argument must balance; a comma within
such parentheses does not end the argument.  However, there is no
requirement for square brackets or braces to balance, and they do not
prevent a comma from separating arguments.  Thus,

@example
macro (array[x = y, x + 1])
@end example

@noindent
passes two arguments to @code{macro}: @code{array[x = y} and @code{x +
1]}.  If you want to supply @code{array[x = y, x + 1]} as an argument,
you can write it as @code{array[(x = y, x + 1)]}, which is equivalent C
code.

All arguments to a macro are completely macro-expanded before they are
substituted into the macro body.  After substitution, the complete text
is scanned again for macros to expand, including the arguments.  This rule
may seem strange, but it is carefully designed so you need not worry
about whether any function call is actually a macro invocation.  You can
run into trouble if you try to be too clever, though.  @xref{Argument
Prescan}, for detailed discussion.

For example, @code{min (min (a, b), c)} is first expanded to

@example
  min (((a) < (b) ? (a) : (b)), (c))
@end example

@noindent
and then to

@example
@group
((((a) < (b) ? (a) : (b))) < (c)
 ? (((a) < (b) ? (a) : (b)))
 : (c))
@end group
@end example

@noindent
(Line breaks shown here for clarity would not actually be generated.)

@cindex empty macro arguments
You can leave macro arguments empty; this is not an error for the
preprocessor (but many macros will then expand to invalid code).
You cannot leave out arguments entirely; if a macro takes two arguments,
there must be exactly one comma at the top level of its argument list.
Here are some silly examples using @code{min}:

@example

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -