Ifdef.html 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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-2023 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.7, 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" rel="start" title="Top">
  27. <link href="Index-of-Directives.html" 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" rel="up" title="Conditional Syntax">
  30. <link href="If.html" rel="next" title="If">
  31. <link href="Conditional-Syntax.html" 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. div.display {margin-left: 3.2em}
  37. div.example {margin-left: 3.2em}
  38. div.lisp {margin-left: 3.2em}
  39. kbd {font-style: oblique}
  40. pre.display {font-family: inherit}
  41. pre.format {font-family: inherit}
  42. pre.menu-comment {font-family: serif}
  43. pre.menu-preformatted {font-family: serif}
  44. span.nolinebreak {white-space: nowrap}
  45. span.roman {font-family: initial; font-weight: normal}
  46. span.sansserif {font-family: sans-serif; font-weight: normal}
  47. ul.no-bullet {list-style: none}
  48. -->
  49. </style>
  50. </head>
  51. <body lang="en">
  52. <span id="Ifdef"></span><div class="header">
  53. <p>
  54. Next: <a href="If.html" accesskey="n" rel="next">If</a>, Up: <a href="Conditional-Syntax.html" 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" title="Index" rel="index">Index</a>]</p>
  55. </div>
  56. <hr>
  57. <span id="Ifdef-1"></span><h4 class="subsection">4.2.1 Ifdef</h4>
  58. <span id="index-_0023ifdef"></span>
  59. <span id="index-_0023endif"></span>
  60. <p>The simplest sort of conditional is
  61. </p>
  62. <div class="example">
  63. <pre class="example">#ifdef <var>MACRO</var>
  64. <var>controlled text</var>
  65. #endif /* <var>MACRO</var> */
  66. </pre></div>
  67. <span id="index-conditional-group"></span>
  68. <p>This block is called a <em>conditional group</em>. <var>controlled text</var>
  69. will be included in the output of the preprocessor if and only if
  70. <var>MACRO</var> is defined. We say that the conditional <em>succeeds</em> if
  71. <var>MACRO</var> is defined, <em>fails</em> if it is not.
  72. </p>
  73. <p>The <var>controlled text</var> inside of a conditional can include
  74. preprocessing directives. They are executed only if the conditional
  75. succeeds. You can nest conditional groups inside other conditional
  76. groups, but they must be completely nested. In other words,
  77. &lsquo;<samp>#endif</samp>&rsquo; always matches the nearest &lsquo;<samp>#ifdef</samp>&rsquo; (or
  78. &lsquo;<samp>#ifndef</samp>&rsquo;, or &lsquo;<samp>#if</samp>&rsquo;). Also, you cannot start a conditional
  79. group in one file and end it in another.
  80. </p>
  81. <p>Even if a conditional fails, the <var>controlled text</var> inside it is
  82. still run through initial transformations and tokenization. Therefore,
  83. it must all be lexically valid C. Normally the only way this matters is
  84. that all comments and string literals inside a failing conditional group
  85. must still be properly ended.
  86. </p>
  87. <p>The comment following the &lsquo;<samp>#endif</samp>&rsquo; is not required, but it is a
  88. good practice if there is a lot of <var>controlled text</var>, because it
  89. helps people match the &lsquo;<samp>#endif</samp>&rsquo; to the corresponding &lsquo;<samp>#ifdef</samp>&rsquo;.
  90. Older programs sometimes put <var>MACRO</var> directly after the
  91. &lsquo;<samp>#endif</samp>&rsquo; without enclosing it in a comment. This is invalid code
  92. according to the C standard. CPP accepts it with a warning. It
  93. never affects which &lsquo;<samp>#ifndef</samp>&rsquo; the &lsquo;<samp>#endif</samp>&rsquo; matches.
  94. </p>
  95. <span id="index-_0023ifndef"></span>
  96. <p>Sometimes you wish to use some code if a macro is <em>not</em> defined.
  97. You can do this by writing &lsquo;<samp>#ifndef</samp>&rsquo; instead of &lsquo;<samp>#ifdef</samp>&rsquo;.
  98. One common use of &lsquo;<samp>#ifndef</samp>&rsquo; is to include code only the first
  99. time a header file is included. See <a href="Once_002dOnly-Headers.html">Once-Only Headers</a>.
  100. </p>
  101. <p>Macro definitions can vary between compilations for several reasons.
  102. Here are some samples.
  103. </p>
  104. <ul>
  105. <li> Some macros are predefined on each kind of machine
  106. (see <a href="System_002dspecific-Predefined-Macros.html">System-specific Predefined Macros</a>). This allows you to provide
  107. code specially tuned for a particular machine.
  108. </li><li> System header files define more macros, associated with the features
  109. they implement. You can test these macros with conditionals to avoid
  110. using a system feature on a machine where it is not implemented.
  111. </li><li> Macros can be defined or undefined with the <samp>-D</samp> and <samp>-U</samp>
  112. command-line options when you compile the program. You can arrange to
  113. compile the same source file into two different programs by choosing a
  114. macro name to specify which program you want, writing conditionals to
  115. test whether or how this macro is defined, and then controlling the
  116. state of the macro with command-line options, perhaps set in the
  117. Makefile. See <a href="Invocation.html">Invocation</a>.
  118. </li><li> Your program might have a special header file (often called
  119. <samp>config.h</samp>) that is adjusted when the program is compiled. It can
  120. define or not define macros depending on the features of the system and
  121. the desired capabilities of the program. The adjustment can be
  122. automated by a tool such as <code>autoconf</code>, or done by hand.
  123. </li></ul>
  124. <hr>
  125. <div class="header">
  126. <p>
  127. Next: <a href="If.html" accesskey="n" rel="next">If</a>, Up: <a href="Conditional-Syntax.html" 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" title="Index" rel="index">Index</a>]</p>
  128. </div>
  129. </body>
  130. </html>