• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /asuswrt-rt-n18u-9.0.0.4.380.2695/release/src-rt-6.x.4708/toolchains/hndtools-armeabi-2011.09/share/doc/arm-arm-none-eabi/html/cpp/
1<html lang="en">
2<head>
3<title>Ifdef - The C Preprocessor</title>
4<meta http-equiv="Content-Type" content="text/html">
5<meta name="description" content="The C Preprocessor">
6<meta name="generator" content="makeinfo 4.13">
7<link title="Top" rel="start" href="index.html#Top">
8<link rel="up" href="Conditional-Syntax.html#Conditional-Syntax" title="Conditional Syntax">
9<link rel="next" href="If.html#If" title="If">
10<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
11<!--
12Copyright (C) 1987, 1989, 1991, 1992, 1993, 1994, 1995, 1996,
131997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
142008, 2009, 2010, 2011
15Free Software Foundation, Inc.
16
17Permission is granted to copy, distribute and/or modify this document
18under the terms of the GNU Free Documentation License, Version 1.3 or
19any later version published by the Free Software Foundation.  A copy of
20the license is included in the
21section entitled ``GNU Free Documentation License''.
22
23This manual contains no Invariant Sections.  The Front-Cover Texts are
24(a) (see below), and the Back-Cover Texts are (b) (see below).
25
26(a) The FSF's Front-Cover Text is:
27
28     A GNU Manual
29
30(b) The FSF's Back-Cover Text is:
31
32     You have freedom to copy and modify this GNU Manual, like GNU
33     software.  Copies published by the Free Software Foundation raise
34     funds for GNU development.
35-->
36<meta http-equiv="Content-Style-Type" content="text/css">
37<style type="text/css"><!--
38  pre.display { font-family:inherit }
39  pre.format  { font-family:inherit }
40  pre.smalldisplay { font-family:inherit; font-size:smaller }
41  pre.smallformat  { font-family:inherit; font-size:smaller }
42  pre.smallexample { font-size:smaller }
43  pre.smalllisp    { font-size:smaller }
44  span.sc    { font-variant:small-caps }
45  span.roman { font-family:serif; font-weight:normal; } 
46  span.sansserif { font-family:sans-serif; font-weight:normal; } 
47--></style>
48<link rel="stylesheet" type="text/css" href="../cs.css">
49</head>
50<body>
51<div class="node">
52<a name="Ifdef"></a>
53<p>
54Next:&nbsp;<a rel="next" accesskey="n" href="If.html#If">If</a>,
55Up:&nbsp;<a rel="up" accesskey="u" href="Conditional-Syntax.html#Conditional-Syntax">Conditional Syntax</a>
56<hr>
57</div>
58
59<h4 class="subsection">4.2.1 Ifdef</h4>
60
61<p><a name="index-g_t_0023ifdef-85"></a><a name="index-g_t_0023endif-86"></a>
62The simplest sort of conditional is
63
64<pre class="smallexample">     #ifdef <var>MACRO</var>
65     
66     <var>controlled text</var>
67     
68     #endif /* <var>MACRO</var> */
69</pre>
70   <p><a name="index-conditional-group-87"></a>This block is called a <dfn>conditional group</dfn>.  <var>controlled text</var>
71will be included in the output of the preprocessor if and only if
72<var>MACRO</var> is defined.  We say that the conditional <dfn>succeeds</dfn> if
73<var>MACRO</var> is defined, <dfn>fails</dfn> if it is not.
74
75   <p>The <var>controlled text</var> inside of a conditional can include
76preprocessing directives.  They are executed only if the conditional
77succeeds.  You can nest conditional groups inside other conditional
78groups, but they must be completely nested.  In other words,
79&lsquo;<samp><span class="samp">#endif</span></samp>&rsquo; always matches the nearest &lsquo;<samp><span class="samp">#ifdef</span></samp>&rsquo; (or
80&lsquo;<samp><span class="samp">#ifndef</span></samp>&rsquo;, or &lsquo;<samp><span class="samp">#if</span></samp>&rsquo;).  Also, you cannot start a conditional
81group in one file and end it in another.
82
83   <p>Even if a conditional fails, the <var>controlled text</var> inside it is
84still run through initial transformations and tokenization.  Therefore,
85it must all be lexically valid C.  Normally the only way this matters is
86that all comments and string literals inside a failing conditional group
87must still be properly ended.
88
89   <p>The comment following the &lsquo;<samp><span class="samp">#endif</span></samp>&rsquo; is not required, but it is a
90good practice if there is a lot of <var>controlled text</var>, because it
91helps people match the &lsquo;<samp><span class="samp">#endif</span></samp>&rsquo; to the corresponding &lsquo;<samp><span class="samp">#ifdef</span></samp>&rsquo;. 
92Older programs sometimes put <var>MACRO</var> directly after the
93&lsquo;<samp><span class="samp">#endif</span></samp>&rsquo; without enclosing it in a comment.  This is invalid code
94according to the C standard.  CPP accepts it with a warning.  It
95never affects which &lsquo;<samp><span class="samp">#ifndef</span></samp>&rsquo; the &lsquo;<samp><span class="samp">#endif</span></samp>&rsquo; matches.
96
97   <p><a name="index-g_t_0023ifndef-88"></a>Sometimes you wish to use some code if a macro is <em>not</em> defined. 
98You can do this by writing &lsquo;<samp><span class="samp">#ifndef</span></samp>&rsquo; instead of &lsquo;<samp><span class="samp">#ifdef</span></samp>&rsquo;. 
99One common use of &lsquo;<samp><span class="samp">#ifndef</span></samp>&rsquo; is to include code only the first
100time a header file is included.  See <a href="Once_002dOnly-Headers.html#Once_002dOnly-Headers">Once-Only Headers</a>.
101
102   <p>Macro definitions can vary between compilations for several reasons. 
103Here are some samples.
104
105     <ul>
106<li>Some macros are predefined on each kind of machine
107(see <a href="System_002dspecific-Predefined-Macros.html#System_002dspecific-Predefined-Macros">System-specific Predefined Macros</a>).  This allows you to provide
108code specially tuned for a particular machine.
109
110     <li>System header files define more macros, associated with the features
111they implement.  You can test these macros with conditionals to avoid
112using a system feature on a machine where it is not implemented.
113
114     <li>Macros can be defined or undefined with the <samp><span class="option">-D</span></samp> and <samp><span class="option">-U</span></samp>
115command line options when you compile the program.  You can arrange to
116compile the same source file into two different programs by choosing a
117macro name to specify which program you want, writing conditionals to
118test whether or how this macro is defined, and then controlling the
119state of the macro with command line options, perhaps set in the
120Makefile.  See <a href="Invocation.html#Invocation">Invocation</a>.
121
122     <li>Your program might have a special header file (often called
123<samp><span class="file">config.h</span></samp>) that is adjusted when the program is compiled.  It can
124define or not define macros depending on the features of the system and
125the desired capabilities of the program.  The adjustment can be
126automated by a tool such as <samp><span class="command">autoconf</span></samp>, or done by hand. 
127</ul>
128
129   </body></html>
130
131