Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

222 lines
9.5KB

  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>Pragmas (The C Preprocessor)</title>
  21. <meta name="description" content="Pragmas (The C Preprocessor)">
  22. <meta name="keywords" content="Pragmas (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="index.html#Top" rel="up" title="Top">
  30. <link href="Other-Directives.html#Other-Directives" rel="next" title="Other Directives">
  31. <link href="Line-Control.html#Line-Control" rel="prev" title="Line Control">
  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="Pragmas"></a>
  62. <div class="header">
  63. <p>
  64. Next: <a href="Other-Directives.html#Other-Directives" accesskey="n" rel="next">Other Directives</a>, Previous: <a href="Line-Control.html#Line-Control" accesskey="p" rel="prev">Line Control</a>, Up: <a href="index.html#Top" accesskey="u" rel="up">Top</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="Pragmas-1"></a>
  68. <h2 class="chapter">7 Pragmas</h2>
  69. <a name="index-pragma-directive"></a>
  70. <p>The &lsquo;<samp>#pragma</samp>&rsquo; directive is the method specified by the C standard
  71. for providing additional information to the compiler, beyond what is
  72. conveyed in the language itself. The forms of this directive
  73. (commonly known as <em>pragmas</em>) specified by C standard are prefixed with
  74. <code>STDC</code>. A C compiler is free to attach any meaning it likes to other
  75. pragmas. Most GNU-defined, supported pragmas have been given a
  76. <code>GCC</code> prefix.
  77. </p>
  78. <a name="index-_005fPragma"></a>
  79. <p>C99 introduced the <code><span class="nolinebreak">_Pragma</span><!-- /@w --></code> operator. This feature addresses a
  80. major problem with &lsquo;<samp>#pragma</samp>&rsquo;: being a directive, it cannot be
  81. produced as the result of macro expansion. <code><span class="nolinebreak">_Pragma</span><!-- /@w --></code> is an
  82. operator, much like <code>sizeof</code> or <code>defined</code>, and can be embedded
  83. in a macro.
  84. </p>
  85. <p>Its syntax is <code><span class="nolinebreak">_Pragma</span>&nbsp;(<var><span class="nolinebreak">string-literal</span></var>)<!-- /@w --></code>, where
  86. <var>string-literal</var> can be either a normal or wide-character string
  87. literal. It is destringized, by replacing all &lsquo;<samp>\\</samp>&rsquo; with a single
  88. &lsquo;<samp>\</samp>&rsquo; and all &lsquo;<samp>\&quot;</samp>&rsquo; with a &lsquo;<samp>&quot;</samp>&rsquo;. The result is then
  89. processed as if it had appeared as the right hand side of a
  90. &lsquo;<samp>#pragma</samp>&rsquo; directive. For example,
  91. </p>
  92. <div class="smallexample">
  93. <pre class="smallexample">_Pragma (&quot;GCC dependency \&quot;parse.y\&quot;&quot;)
  94. </pre></div>
  95. <p>has the same effect as <code>#pragma GCC dependency &quot;parse.y&quot;</code>. The
  96. same effect could be achieved using macros, for example
  97. </p>
  98. <div class="smallexample">
  99. <pre class="smallexample">#define DO_PRAGMA(x) _Pragma (#x)
  100. DO_PRAGMA (GCC dependency &quot;parse.y&quot;)
  101. </pre></div>
  102. <p>The standard is unclear on where a <code>_Pragma</code> operator can appear.
  103. The preprocessor does not accept it within a preprocessing conditional
  104. directive like &lsquo;<samp>#if</samp>&rsquo;. To be safe, you are probably best keeping it
  105. out of directives other than &lsquo;<samp>#define</samp>&rsquo;, and putting it on a line of
  106. its own.
  107. </p>
  108. <p>This manual documents the pragmas which are meaningful to the
  109. preprocessor itself. Other pragmas are meaningful to the C or C++
  110. compilers. They are documented in the GCC manual.
  111. </p>
  112. <p>GCC plugins may provide their own pragmas.
  113. </p>
  114. <dl compact="compact">
  115. <dt><code>#pragma GCC dependency</code>
  116. <a name="index-_0023pragma-GCC-dependency"></a>
  117. </dt>
  118. <dd><p><code>#pragma GCC dependency</code> allows you to check the relative dates of
  119. the current file and another file. If the other file is more recent than
  120. the current file, a warning is issued. This is useful if the current
  121. file is derived from the other file, and should be regenerated. The
  122. other file is searched for using the normal include search path.
  123. Optional trailing text can be used to give more information in the
  124. warning message.
  125. </p>
  126. <div class="smallexample">
  127. <pre class="smallexample">#pragma GCC dependency &quot;parse.y&quot;
  128. #pragma GCC dependency &quot;/usr/include/time.h&quot; rerun fixincludes
  129. </pre></div>
  130. </dd>
  131. <dt><code>#pragma GCC poison</code>
  132. <a name="index-_0023pragma-GCC-poison"></a>
  133. </dt>
  134. <dd><p>Sometimes, there is an identifier that you want to remove completely
  135. from your program, and make sure that it never creeps back in. To
  136. enforce this, you can <em>poison</em> the identifier with this pragma.
  137. <code>#pragma GCC poison</code> is followed by a list of identifiers to
  138. poison. If any of those identifiers appears anywhere in the source
  139. after the directive, it is a hard error. For example,
  140. </p>
  141. <div class="smallexample">
  142. <pre class="smallexample">#pragma GCC poison printf sprintf fprintf
  143. sprintf(some_string, &quot;hello&quot;);
  144. </pre></div>
  145. <p>will produce an error.
  146. </p>
  147. <p>If a poisoned identifier appears as part of the expansion of a macro
  148. which was defined before the identifier was poisoned, it will <em>not</em>
  149. cause an error. This lets you poison an identifier without worrying
  150. about system headers defining macros that use it.
  151. </p>
  152. <p>For example,
  153. </p>
  154. <div class="smallexample">
  155. <pre class="smallexample">#define strrchr rindex
  156. #pragma GCC poison rindex
  157. strrchr(some_string, 'h');
  158. </pre></div>
  159. <p>will not produce an error.
  160. </p>
  161. </dd>
  162. <dt><code>#pragma GCC system_header</code>
  163. <a name="index-_0023pragma-GCC-system_005fheader-1"></a>
  164. </dt>
  165. <dd><p>This pragma takes no arguments. It causes the rest of the code in the
  166. current file to be treated as if it came from a system header.
  167. See <a href="System-Headers.html#System-Headers">System Headers</a>.
  168. </p>
  169. </dd>
  170. <dt><code>#pragma GCC warning</code>
  171. <a name="index-_0023pragma-GCC-warning"></a>
  172. </dt>
  173. <dt><code>#pragma GCC error</code>
  174. <a name="index-_0023pragma-GCC-error"></a>
  175. </dt>
  176. <dd><p><code>#pragma GCC warning &quot;message&quot;</code> causes the preprocessor to issue
  177. a warning diagnostic with the text &lsquo;<samp>message</samp>&rsquo;. The message
  178. contained in the pragma must be a single string literal. Similarly,
  179. <code>#pragma GCC error &quot;message&quot;</code> issues an error message. Unlike
  180. the &lsquo;<samp>#warning</samp>&rsquo; and &lsquo;<samp>#error</samp>&rsquo; directives, these pragmas can be
  181. embedded in preprocessor macros using &lsquo;<samp>_Pragma</samp>&rsquo;.
  182. </p>
  183. </dd>
  184. <dt><code>#pragma once</code>
  185. <a name="index-_0023pragma-once"></a>
  186. </dt>
  187. <dd><p>If <code>#pragma once</code> is seen when scanning a header file, that
  188. file will never be read again, no matter what. It is a less-portable
  189. alternative to using &lsquo;<samp>#ifndef</samp>&rsquo; to guard the contents of header files
  190. against multiple inclusions.
  191. </p>
  192. </dd>
  193. </dl>
  194. <hr>
  195. <div class="header">
  196. <p>
  197. Next: <a href="Other-Directives.html#Other-Directives" accesskey="n" rel="next">Other Directives</a>, Previous: <a href="Line-Control.html#Line-Control" accesskey="p" rel="prev">Line Control</a>, Up: <a href="index.html#Top" accesskey="u" rel="up">Top</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>
  198. </div>
  199. </body>
  200. </html>