Pragmas.html 9.2 KB

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