1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
---|
2 | <html>
|
---|
3 | <!-- Copyright (C) 2022 Richard Stallman and Free Software Foundation, Inc.
|
---|
4 |
|
---|
5 | (The work of Trevis Rothwell and Nelson Beebe has been assigned or
|
---|
6 | licensed to the FSF.)
|
---|
7 |
|
---|
8 | Permission is granted to copy, distribute and/or modify this document
|
---|
9 | under the terms of the GNU Free Documentation License, Version 1.3 or
|
---|
10 | any later version published by the Free Software Foundation; with the
|
---|
11 | Invariant Sections being "GNU General Public License," with the
|
---|
12 | Front-Cover Texts being "A GNU Manual," and with the Back-Cover
|
---|
13 | Texts as in (a) below. A copy of the license is included in the
|
---|
14 | section entitled "GNU Free Documentation License."
|
---|
15 |
|
---|
16 | (a) The FSF's Back-Cover Text is: "You have the freedom to copy and
|
---|
17 | modify this GNU manual. Buying copies from the FSF supports it in
|
---|
18 | developing GNU and promoting software freedom." -->
|
---|
19 | <!-- Created by GNU Texinfo 6.7, http://www.gnu.org/software/texinfo/ -->
|
---|
20 | <head>
|
---|
21 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
|
---|
22 | <title>ifdef (GNU C Language Manual)</title>
|
---|
23 |
|
---|
24 | <meta name="description" content="ifdef (GNU C Language Manual)">
|
---|
25 | <meta name="keywords" content="ifdef (GNU C Language Manual)">
|
---|
26 | <meta name="resource-type" content="document">
|
---|
27 | <meta name="distribution" content="global">
|
---|
28 | <meta name="Generator" content="makeinfo">
|
---|
29 | <link href="index.html" rel="start" title="Top">
|
---|
30 | <link href="Symbol-Index.html" rel="index" title="Symbol Index">
|
---|
31 | <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
|
---|
32 | <link href="Conditional-Syntax.html" rel="up" title="Conditional Syntax">
|
---|
33 | <link href="if.html" rel="next" title="if">
|
---|
34 | <link href="Conditional-Syntax.html" rel="prev" title="Conditional Syntax">
|
---|
35 | <style type="text/css">
|
---|
36 | <!--
|
---|
37 | a.summary-letter {text-decoration: none}
|
---|
38 | blockquote.indentedblock {margin-right: 0em}
|
---|
39 | div.display {margin-left: 3.2em}
|
---|
40 | div.example {margin-left: 3.2em}
|
---|
41 | div.lisp {margin-left: 3.2em}
|
---|
42 | kbd {font-style: oblique}
|
---|
43 | pre.display {font-family: inherit}
|
---|
44 | pre.format {font-family: inherit}
|
---|
45 | pre.menu-comment {font-family: serif}
|
---|
46 | pre.menu-preformatted {font-family: serif}
|
---|
47 | span.nolinebreak {white-space: nowrap}
|
---|
48 | span.roman {font-family: initial; font-weight: normal}
|
---|
49 | span.sansserif {font-family: sans-serif; font-weight: normal}
|
---|
50 | ul.no-bullet {list-style: none}
|
---|
51 | -->
|
---|
52 | </style>
|
---|
53 |
|
---|
54 |
|
---|
55 | </head>
|
---|
56 |
|
---|
57 | <body lang="en">
|
---|
58 | <span id="ifdef"></span><div class="header">
|
---|
59 | <p>
|
---|
60 | Next: <a href="if.html" accesskey="n" rel="next">if</a>, Up: <a href="Conditional-Syntax.html" accesskey="u" rel="up">Conditional Syntax</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Symbol-Index.html" title="Index" rel="index">Index</a>]</p>
|
---|
61 | </div>
|
---|
62 | <hr>
|
---|
63 | <span id="The-_0023ifdef-directive"></span><h4 class="subsubsection">26.6.2.1 The <code>#ifdef</code> directive</h4>
|
---|
64 | <span id="index-_0023ifdef"></span>
|
---|
65 | <span id="index-_0023endif"></span>
|
---|
66 |
|
---|
67 | <p>The simplest sort of conditional is
|
---|
68 | </p>
|
---|
69 | <div class="example">
|
---|
70 | <pre class="example">#ifdef <var>MACRO</var>
|
---|
71 |
|
---|
72 | <var>controlled text</var>
|
---|
73 |
|
---|
74 | #endif /* <var>MACRO</var> */
|
---|
75 | </pre></div>
|
---|
76 |
|
---|
77 | <span id="index-conditional-group"></span>
|
---|
78 | <p>This block is called a <em>conditional group</em>. The body,
|
---|
79 | <var>controlled text</var>, will be included in compilation if
|
---|
80 | and only if <var>MACRO</var> is defined. We say that the conditional
|
---|
81 | <em>succeeds</em> if <var>MACRO</var> is defined, <em>fails</em> if it is not.
|
---|
82 | </p>
|
---|
83 | <p>The <var>controlled text</var> inside a conditional can include
|
---|
84 | preprocessing directives. They are executed only if the conditional
|
---|
85 | succeeds. You can nest conditional groups inside other conditional
|
---|
86 | groups, but they must be completely nested. In other words,
|
---|
87 | <code>#endif</code> always matches the nearest <code>#ifdef</code> (or
|
---|
88 | <code>#ifndef</code>, or <code>#if</code>). Also, you cannot start a conditional
|
---|
89 | group in one file and end it in another.
|
---|
90 | </p>
|
---|
91 | <p>Even if a conditional fails, the <var>controlled text</var> inside it is
|
---|
92 | still run through initial transformations and tokenization. Therefore,
|
---|
93 | it must all be lexically valid C. Normally the only way this matters is
|
---|
94 | that all comments and string literals inside a failing conditional group
|
---|
95 | must still be properly ended.
|
---|
96 | </p>
|
---|
97 | <p>The comment following the <code>#endif</code> is not required, but it is a
|
---|
98 | good practice if there is a lot of <var>controlled text</var>, because it
|
---|
99 | helps people match the <code>#endif</code> to the corresponding <code>#ifdef</code>.
|
---|
100 | </p>
|
---|
101 | <p>Older programs sometimes put <var>macro</var> directly after the
|
---|
102 | <code>#endif</code> without enclosing it in a comment. This is invalid code
|
---|
103 | according to the C standard, but it only causes a warning in GNU C.
|
---|
104 | It never affects which <code>#ifndef</code> the <code>#endif</code> matches.
|
---|
105 | </p>
|
---|
106 | <span id="index-_0023ifndef"></span>
|
---|
107 | <p>Sometimes you wish to use some code if a macro is <em>not</em> defined.
|
---|
108 | You can do this by writing <code>#ifndef</code> instead of <code>#ifdef</code>.
|
---|
109 | One common use of <code>#ifndef</code> is to include code only the first
|
---|
110 | time a header file is included. See <a href="Once_002dOnly-Headers.html">Once-Only Headers</a>.
|
---|
111 | </p>
|
---|
112 | <p>Macro definitions can vary between compilations for several reasons.
|
---|
113 | Here are some samples.
|
---|
114 | </p>
|
---|
115 | <ul>
|
---|
116 | <li> Some macros are predefined on each kind of machine
|
---|
117 | (see <a href="https://gcc.gnu.org/onlinedocs/gcc/System_002dspecific-Predefined-Macros.html#System_002dspecific-Predefined-Macros">System-specific Predefined Macros</a> in <cite>Using the GNU Compiler
|
---|
118 | Collection</cite>). This allows you to provide code specially tuned for a
|
---|
119 | particular machine.
|
---|
120 |
|
---|
121 | </li><li> System header files define more macros, associated with the features
|
---|
122 | they implement. You can test these macros with conditionals to avoid
|
---|
123 | using a system feature on a machine where it is not implemented.
|
---|
124 |
|
---|
125 | </li><li> Macros can be defined or undefined with the <samp>-D</samp> and <samp>-U</samp>
|
---|
126 | command-line options when you compile the program. You can arrange to
|
---|
127 | compile the same source file into two different programs by choosing a
|
---|
128 | macro name to specify which program you want, writing conditionals to
|
---|
129 | test whether or how this macro is defined, and then controlling the
|
---|
130 | state of the macro with command-line options, perhaps set in the
|
---|
131 | file <samp>Makefile</samp>. See <a href="https://gcc.gnu.org/onlinedocs/gcc/Invocation.html#Invocation">Invoking GCC</a> in <cite>Using the GNU Compiler Collection</cite>.
|
---|
132 |
|
---|
133 | </li><li> Your program might have a special header file (often called
|
---|
134 | <samp>config.h</samp>) that is adjusted when the program is compiled. It can
|
---|
135 | define or not define macros depending on the features of the system and
|
---|
136 | the desired capabilities of the program. The adjustment can be
|
---|
137 | automated by a tool such as <code>autoconf</code>, or done by hand.
|
---|
138 | </li></ul>
|
---|
139 |
|
---|
140 | <hr>
|
---|
141 | <div class="header">
|
---|
142 | <p>
|
---|
143 | Next: <a href="if.html" accesskey="n" rel="next">if</a>, Up: <a href="Conditional-Syntax.html" accesskey="u" rel="up">Conditional Syntax</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Symbol-Index.html" title="Index" rel="index">Index</a>]</p>
|
---|
144 | </div>
|
---|
145 |
|
---|
146 |
|
---|
147 |
|
---|
148 | </body>
|
---|
149 | </html>
|
---|