Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

167 linhas
7.7KB

  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>Computed Includes (The C Preprocessor)</title>
  21. <meta name="description" content="Computed Includes (The C Preprocessor)">
  22. <meta name="keywords" content="Computed Includes (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="Header-Files.html#Header-Files" rel="up" title="Header Files">
  30. <link href="Wrapper-Headers.html#Wrapper-Headers" rel="next" title="Wrapper Headers">
  31. <link href="Alternatives-to-Wrapper-_0023ifndef.html#Alternatives-to-Wrapper-_0023ifndef" rel="prev" title="Alternatives to Wrapper #ifndef">
  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="Computed-Includes"></a>
  62. <div class="header">
  63. <p>
  64. Next: <a href="Wrapper-Headers.html#Wrapper-Headers" accesskey="n" rel="next">Wrapper Headers</a>, Previous: <a href="Alternatives-to-Wrapper-_0023ifndef.html#Alternatives-to-Wrapper-_0023ifndef" accesskey="p" rel="prev">Alternatives to Wrapper #ifndef</a>, Up: <a href="Header-Files.html#Header-Files" accesskey="u" rel="up">Header Files</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="Computed-Includes-1"></a>
  68. <h3 class="section">2.6 Computed Includes</h3>
  69. <a name="index-computed-includes"></a>
  70. <a name="index-macros-in-include"></a>
  71. <p>Sometimes it is necessary to select one of several different header
  72. files to be included into your program. They might specify
  73. configuration parameters to be used on different sorts of operating
  74. systems, for instance. You could do this with a series of conditionals,
  75. </p>
  76. <div class="smallexample">
  77. <pre class="smallexample">#if SYSTEM_1
  78. # include &quot;system_1.h&quot;
  79. #elif SYSTEM_2
  80. # include &quot;system_2.h&quot;
  81. #elif SYSTEM_3
  82. &hellip;
  83. #endif
  84. </pre></div>
  85. <p>That rapidly becomes tedious. Instead, the preprocessor offers the
  86. ability to use a macro for the header name. This is called a
  87. <em>computed include</em>. Instead of writing a header name as the direct
  88. argument of &lsquo;<samp>#include</samp>&rsquo;, you simply put a macro name there instead:
  89. </p>
  90. <div class="smallexample">
  91. <pre class="smallexample">#define SYSTEM_H &quot;system_1.h&quot;
  92. &hellip;
  93. #include SYSTEM_H
  94. </pre></div>
  95. <p><code>SYSTEM_H</code> will be expanded, and the preprocessor will look for
  96. <samp>system_1.h</samp> as if the &lsquo;<samp>#include</samp>&rsquo; had been written that way
  97. originally. <code>SYSTEM_H</code> could be defined by your Makefile with a
  98. <samp>-D</samp> option.
  99. </p>
  100. <p>You must be careful when you define the macro. &lsquo;<samp>#define</samp>&rsquo; saves
  101. tokens, not text. The preprocessor has no way of knowing that the macro
  102. will be used as the argument of &lsquo;<samp>#include</samp>&rsquo;, so it generates
  103. ordinary tokens, not a header name. This is unlikely to cause problems
  104. if you use double-quote includes, which are close enough to string
  105. constants. If you use angle brackets, however, you may have trouble.
  106. </p>
  107. <p>The syntax of a computed include is actually a bit more general than the
  108. above. If the first non-whitespace character after &lsquo;<samp>#include</samp>&rsquo; is
  109. not &lsquo;<samp>&quot;</samp>&rsquo; or &lsquo;<samp>&lt;</samp>&rsquo;, then the entire line is macro-expanded
  110. like running text would be.
  111. </p>
  112. <p>If the line expands to a single string constant, the contents of that
  113. string constant are the file to be included. CPP does not re-examine the
  114. string for embedded quotes, but neither does it process backslash
  115. escapes in the string. Therefore
  116. </p>
  117. <div class="smallexample">
  118. <pre class="smallexample">#define HEADER &quot;a\&quot;b&quot;
  119. #include HEADER
  120. </pre></div>
  121. <p>looks for a file named <samp>a\&quot;b</samp>. CPP searches for the file according
  122. to the rules for double-quoted includes.
  123. </p>
  124. <p>If the line expands to a token stream beginning with a &lsquo;<samp>&lt;</samp>&rsquo; token
  125. and including a &lsquo;<samp>&gt;</samp>&rsquo; token, then the tokens between the &lsquo;<samp>&lt;</samp>&rsquo; and
  126. the first &lsquo;<samp>&gt;</samp>&rsquo; are combined to form the filename to be included.
  127. Any whitespace between tokens is reduced to a single space; then any
  128. space after the initial &lsquo;<samp>&lt;</samp>&rsquo; is retained, but a trailing space
  129. before the closing &lsquo;<samp>&gt;</samp>&rsquo; is ignored. CPP searches for the file
  130. according to the rules for angle-bracket includes.
  131. </p>
  132. <p>In either case, if there are any tokens on the line after the file name,
  133. an error occurs and the directive is not processed. It is also an error
  134. if the result of expansion does not match either of the two expected
  135. forms.
  136. </p>
  137. <p>These rules are implementation-defined behavior according to the C
  138. standard. To minimize the risk of different compilers interpreting your
  139. computed includes differently, we recommend you use only a single
  140. object-like macro which expands to a string constant. This will also
  141. minimize confusion for people reading your program.
  142. </p>
  143. <hr>
  144. <div class="header">
  145. <p>
  146. Next: <a href="Wrapper-Headers.html#Wrapper-Headers" accesskey="n" rel="next">Wrapper Headers</a>, Previous: <a href="Alternatives-to-Wrapper-_0023ifndef.html#Alternatives-to-Wrapper-_0023ifndef" accesskey="p" rel="prev">Alternatives to Wrapper #ifndef</a>, Up: <a href="Header-Files.html#Header-Files" accesskey="u" rel="up">Header Files</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>
  147. </div>
  148. </body>
  149. </html>