You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Ifdef.html 7.4KB

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