The C Preprocessor.pdf

(470 KB) Pobierz
The C Preprocessor
For
gcc
version 4.9.0
(GCC)
Richard M. Stallman, Zachary Weinberg
Copyright c 1987-2014 Free Software Foundation, Inc.
Permission is granted to copy, distribute and/or modify this document under the terms of
the GNU Free Documentation License, Version 1.3 or any later version published by the
Free Software Foundation. A copy of the license is included in the section entitled “GNU
Free Documentation License”.
This manual contains no Invariant Sections. The Front-Cover Texts are (a) (see below),
and the Back-Cover Texts are (b) (see below).
(a) The FSF’s Front-Cover Text is:
A GNU Manual
(b) The FSF’s Back-Cover Text is:
You have freedom to copy and modify this GNU Manual, like GNU software. Copies
published by the Free Software Foundation raise funds for GNU development.
i
Table of Contents
1
Overview
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1
1.1
1.2
1.3
1.4
Character sets.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Initial processing
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Tokenization
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
The preprocessing language
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1
2
4
6
2
Header Files
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
7
2.1
2.2
2.3
2.4
2.5
2.6
2.7
2.8
Include Syntax
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
8
Include Operation
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
8
Search Path
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
9
Once-Only Headers
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
10
Alternatives to Wrapper #ifndef
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
10
Computed Includes
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
11
Wrapper Headers
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
12
System Headers
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
13
3
Macros
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
13
Object-like Macros
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Function-like Macros
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Macro Arguments
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Stringification
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Concatenation
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Variadic Macros
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Predefined Macros
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3.7.1 Standard Predefined Macros
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3.7.2 Common Predefined Macros
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3.7.3 System-specific Predefined Macros
. . . . . . . . . . . . . . . . . . . . . . . .
3.7.4 C++ Named Operators
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3.8 Undefining and Redefining Macros
. . . . . . . . . . . . . . . . . . . . . . . . . . . .
3.9 Directives Within Macro Arguments
. . . . . . . . . . . . . . . . . . . . . . . . . . .
3.10 Macro Pitfalls
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3.10.1 Misnesting
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3.10.2 Operator Precedence Problems
. . . . . . . . . . . . . . . . . . . . . . . . . .
3.10.3 Swallowing the Semicolon
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3.10.4 Duplication of Side Effects
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3.10.5 Self-Referential Macros
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3.10.6 Argument Prescan
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3.10.7 Newlines in Arguments
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3.1
3.2
3.3
3.4
3.5
3.6
3.7
14
15
16
17
18
19
21
21
23
32
33
33
34
34
35
35
36
36
37
38
39
ii
4
Conditionals
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
40
4.1 Conditional Uses
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4.2 Conditional Syntax
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4.2.1 Ifdef
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4.2.2 If
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4.2.3 Defined
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4.2.4 Else
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4.2.5 Elif
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4.3 Deleted Code
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
40
40
41
42
42
43
43
44
5
6
7
8
9
10
Diagnostics
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
44
Line Control
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
45
Pragmas
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
46
Other Directives
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
47
Preprocessor Output
. . . . . . . . . . . . . . . . . . . . . . . . . .
48
Traditional Mode.
. . . . . . . . . . . . . . . . . . . . . . . . . . . .
49
Traditional
Traditional
Traditional
Traditional
lexical analysis
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
macros
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
miscellany
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
warnings
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
49
50
51
52
10.1
10.2
10.3
10.4
11
Implementation Details
. . . . . . . . . . . . . . . . . . . . . .
52
52
54
55
55
56
11.1 Implementation-defined behavior
. . . . . . . . . . . . . . . . . . . . . . . . . . . . .
11.2 Implementation limits
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
11.3 Obsolete Features
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
11.3.1 Assertions
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
11.4 Differences from previous versions
. . . . . . . . . . . . . . . . . . . . . . . . . . . .
12
13
Invocation
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
57
Environment Variables
. . . . . . . . . . . . . . . . . . . . . .
67
GNU Free Documentation License.
. . . . . . . . . . . . . .
69
ADDENDUM: How to use this License for your documents
. . . . . . . . .
76
Index of Directives
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
77
Option Index
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
77
Concept Index
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
79
Chapter 1: Overview
1
1 Overview
The C preprocessor, often known as
cpp,
is a
macro processor
that is used automatically by
the C compiler to transform your program before compilation. It is called a macro processor
because it allows you to define
macros,
which are brief abbreviations for longer constructs.
The C preprocessor is intended to be used only with C, C++, and Objective-C source
code. In the past, it has been abused as a general text processor. It will choke on input
which does not obey C’s lexical rules. For example, apostrophes will be interpreted as the
beginning of character constants, and cause errors. Also, you cannot rely on it preserving
characteristics of the input which are not significant to C-family languages. If a Makefile is
preprocessed, all the hard tabs will be removed, and the Makefile will not work.
Having said that, you can often get away with using cpp on things which are not C. Other
Algol-ish programming languages are often safe (Pascal, Ada, etc.) So is assembly, with
caution. ‘-traditional-cpp’ mode preserves more white space, and is otherwise more
permissive. Many of the problems can be avoided by writing C or C++ style comments
instead of native language comments, and keeping macros simple.
Wherever possible, you should use a preprocessor geared to the language you are writing
in. Modern versions of the GNU assembler have macro facilities. Most high level program-
ming languages have their own conditional compilation and inclusion mechanism. If all else
fails, try a true general text processor, such as GNU M4.
C preprocessors vary in some details. This manual discusses the GNU C preprocessor,
which provides a small superset of the features of ISO Standard C. In its default mode,
the GNU C preprocessor does not do a few things required by the standard. These are
features which are rarely, if ever, used, and may cause surprising changes to the meaning
of a program which does not expect them. To get strict ISO Standard C, you should
use the ‘-std=c90’, ‘-std=c99’ or ‘-std=c11’ options, depending on which version of the
standard you want. To get all the mandatory diagnostics, you must also use ‘-pedantic’.
See
Chapter 12 [Invocation], page 57.
This manual describes the behavior of the ISO preprocessor. To minimize gratuitous
differences, where the ISO preprocessor’s behavior does not conflict with traditional seman-
tics, the traditional preprocessor should behave the same way. The various differences that
do exist are detailed in the section
Chapter 10 [Traditional Mode], page 49.
For clarity, unless noted otherwise, references to ‘CPP’ in this manual refer to GNU CPP.
1.1 Character sets
Source code character set processing in C and related languages is rather complicated. The
C standard discusses two character sets, but there are really at least four.
The files input to CPP might be in any character set at all. CPP’s very first action,
before it even looks for line boundaries, is to convert the file into the character set it uses
for internal processing. That set is what the C standard calls the
source
character set. It
must be isomorphic with ISO 10646, also known as Unicode. CPP uses the UTF-8 encoding
of Unicode.
The character sets of the input files are specified using the ‘-finput-charset=’ option.
Zgłoś jeśli naruszono regulamin