1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
---|
2 | <html>
|
---|
3 | <!-- Copyright (C) 2022 Richard Stallman and Free Software Foundation, Inc.
|
---|
4 |
|
---|
5 | (The work of Trevis Rothwell and Nelson Beebe has been assigned or
|
---|
6 | licensed to the FSF.)
|
---|
7 |
|
---|
8 | Permission is granted to copy, distribute and/or modify this document
|
---|
9 | under the terms of the GNU Free Documentation License, Version 1.3 or
|
---|
10 | any later version published by the Free Software Foundation; with the
|
---|
11 | Invariant Sections being "GNU General Public License," with the
|
---|
12 | Front-Cover Texts being "A GNU Manual," and with the Back-Cover
|
---|
13 | Texts as in (a) below. A copy of the license is included in the
|
---|
14 | section entitled "GNU Free Documentation License."
|
---|
15 |
|
---|
16 | (a) The FSF's Back-Cover Text is: "You have the freedom to copy and
|
---|
17 | modify this GNU manual. Buying copies from the FSF supports it in
|
---|
18 | developing GNU and promoting software freedom." -->
|
---|
19 | <!-- Created by GNU Texinfo 6.7, http://www.gnu.org/software/texinfo/ -->
|
---|
20 | <head>
|
---|
21 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
|
---|
22 | <title>Pragma Basics (GNU C Language Manual)</title>
|
---|
23 |
|
---|
24 | <meta name="description" content="Pragma Basics (GNU C Language Manual)">
|
---|
25 | <meta name="keywords" content="Pragma Basics (GNU C Language Manual)">
|
---|
26 | <meta name="resource-type" content="document">
|
---|
27 | <meta name="distribution" content="global">
|
---|
28 | <meta name="Generator" content="makeinfo">
|
---|
29 | <link href="index.html" rel="start" title="Top">
|
---|
30 | <link href="Symbol-Index.html" rel="index" title="Symbol Index">
|
---|
31 | <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
|
---|
32 | <link href="Pragmas.html" rel="up" title="Pragmas">
|
---|
33 | <link href="Severity-Pragmas.html" rel="next" title="Severity Pragmas">
|
---|
34 | <link href="Pragmas.html" rel="prev" title="Pragmas">
|
---|
35 | <style type="text/css">
|
---|
36 | <!--
|
---|
37 | a.summary-letter {text-decoration: none}
|
---|
38 | blockquote.indentedblock {margin-right: 0em}
|
---|
39 | div.display {margin-left: 3.2em}
|
---|
40 | div.example {margin-left: 3.2em}
|
---|
41 | div.lisp {margin-left: 3.2em}
|
---|
42 | kbd {font-style: oblique}
|
---|
43 | pre.display {font-family: inherit}
|
---|
44 | pre.format {font-family: inherit}
|
---|
45 | pre.menu-comment {font-family: serif}
|
---|
46 | pre.menu-preformatted {font-family: serif}
|
---|
47 | span.nolinebreak {white-space: nowrap}
|
---|
48 | span.roman {font-family: initial; font-weight: normal}
|
---|
49 | span.sansserif {font-family: sans-serif; font-weight: normal}
|
---|
50 | ul.no-bullet {list-style: none}
|
---|
51 | -->
|
---|
52 | </style>
|
---|
53 |
|
---|
54 |
|
---|
55 | </head>
|
---|
56 |
|
---|
57 | <body lang="en">
|
---|
58 | <span id="Pragma-Basics"></span><div class="header">
|
---|
59 | <p>
|
---|
60 | Next: <a href="Severity-Pragmas.html" accesskey="n" rel="next">Severity Pragmas</a>, Up: <a href="Pragmas.html" accesskey="u" rel="up">Pragmas</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Symbol-Index.html" title="Index" rel="index">Index</a>]</p>
|
---|
61 | </div>
|
---|
62 | <hr>
|
---|
63 | <span id="Pragma-Basics-1"></span><h4 class="subsection">30.1.1 Pragma Basics</h4>
|
---|
64 |
|
---|
65 | <p>C defines two syntactical forms for pragmas, the line form and the
|
---|
66 | token form. You can write any pragma in either form, with the same
|
---|
67 | meaning.
|
---|
68 | </p>
|
---|
69 | <p>The line form is a line in the source code, like this:
|
---|
70 | </p>
|
---|
71 | <div class="example">
|
---|
72 | <pre class="example">#pragma <var>line</var>
|
---|
73 | </pre></div>
|
---|
74 |
|
---|
75 | <p>The line pragma has no effect on the parsing of the lines around it.
|
---|
76 | This form has the drawback that it can’t be generated by a macro expansion.
|
---|
77 | </p>
|
---|
78 | <p>The token form is a series of tokens; it can appear anywhere in the
|
---|
79 | program between the other tokens.
|
---|
80 | </p>
|
---|
81 | <div class="example">
|
---|
82 | <pre class="example">_Pragma (<var>stringconstant</var>)
|
---|
83 | </pre></div>
|
---|
84 |
|
---|
85 | <p>The pragma has no effect on the syntax of the tokens that surround it;
|
---|
86 | thus, here’s a pragma in the middle of an <code>if</code> statement:
|
---|
87 | </p>
|
---|
88 | <div class="example">
|
---|
89 | <pre class="example">if _Pragma ("hello") (x > 1)
|
---|
90 | </pre></div>
|
---|
91 |
|
---|
92 | <p>However, that’s an unclear thing to do; for the sake of
|
---|
93 | understandability, it is better to put a pragma on a line by itself
|
---|
94 | and not embedded in the middle of another construct.
|
---|
95 | </p>
|
---|
96 | <p>Both forms of pragma have a textual argument. In a line pragma, the
|
---|
97 | text is the rest of the line. The textual argument to <code>_Pragma</code>
|
---|
98 | uses the same syntax as a C string constant: surround the text with
|
---|
99 | two ‘<samp>"</samp>’ characters, and add a backslash before each ‘<samp>"</samp>’ or
|
---|
100 | ‘<samp>\</samp>’ character in it.
|
---|
101 | </p>
|
---|
102 | <p>With either syntax, the textual argument specifies what to do.
|
---|
103 | It begins with one or several words that specify the operation.
|
---|
104 | If the compiler does not recognize them, it ignores the pragma.
|
---|
105 | </p>
|
---|
106 | <p>Here are the pragma operations supported in GNU C.
|
---|
107 | </p>
|
---|
108 | <dl compact="compact">
|
---|
109 | <dt><code>#pragma GCC dependency "<var>file</var>" [<var>message</var>]</code></dt>
|
---|
110 | <dt><code>_Pragma ("GCC dependency \"<var>file</var>\" [<var>message</var>]")</code></dt>
|
---|
111 | <dd><p>Declares that the current source file depends on <var>file</var>, so GNU C
|
---|
112 | compares the file times and gives a warning if <var>file</var> is newer
|
---|
113 | than the current source file.
|
---|
114 | </p>
|
---|
115 | <p>This directive searches for <var>file</var> the way <code>#include</code>
|
---|
116 | searches for a non-system header file.
|
---|
117 | </p>
|
---|
118 | <p>If <var>message</var> is given, the warning message includes that text.
|
---|
119 | </p>
|
---|
120 | <p>Examples:
|
---|
121 | </p>
|
---|
122 | <div class="example">
|
---|
123 | <pre class="example">#pragma GCC dependency "parse.y"
|
---|
124 | _pragma ("GCC dependency \"/usr/include/time.h\" \
|
---|
125 | rerun fixincludes")
|
---|
126 | </pre></div>
|
---|
127 |
|
---|
128 | </dd>
|
---|
129 | <dt><code>#pragma GCC poison <var>identifiers</var></code></dt>
|
---|
130 | <dt><code>_Pragma ("GCC poison <var>identifiers</var>")</code></dt>
|
---|
131 | <dd><p>Poisons the identifiers listed in <var>identifiers</var>.
|
---|
132 | </p>
|
---|
133 | <p>This is useful to make sure all mention of <var>identifiers</var> has been
|
---|
134 | deleted from the program and that no reference to them creeps back in.
|
---|
135 | If any of those identifiers appears anywhere in the source after the
|
---|
136 | directive, it causes a compilation error. For example,
|
---|
137 | </p>
|
---|
138 | <div class="example">
|
---|
139 | <pre class="example">#pragma GCC poison printf sprintf fprintf
|
---|
140 | sprintf(some_string, "hello");
|
---|
141 | </pre></div>
|
---|
142 |
|
---|
143 | <p>generates an error.
|
---|
144 | </p>
|
---|
145 | <p>If a poisoned identifier appears as part of the expansion of a macro
|
---|
146 | that was defined before the identifier was poisoned, it will <em>not</em>
|
---|
147 | cause an error. Thus, system headers that define macros that use
|
---|
148 | the identifier will not cause errors.
|
---|
149 | </p>
|
---|
150 | <p>For example,
|
---|
151 | </p>
|
---|
152 | <div class="example">
|
---|
153 | <pre class="example">#define strrchr rindex
|
---|
154 | _Pragma ("GCC poison rindex")
|
---|
155 | strrchr(some_string, 'h');
|
---|
156 | </pre></div>
|
---|
157 |
|
---|
158 | <p>does not cause a compilation error.
|
---|
159 | </p>
|
---|
160 | </dd>
|
---|
161 | <dt><code>#pragma GCC system_header</code></dt>
|
---|
162 | <dt><code>_Pragma ("GCC system_header")</code></dt>
|
---|
163 | <dd><p>Specify treating the rest of the current source file as if it came
|
---|
164 | from a system header file. See <a href="https://gcc.gnu.org/onlinedocs/gcc/System-Headers.html#System-Headers">System Headers</a> in <cite>Using the GNU Compiler Collection</cite>.
|
---|
165 | </p>
|
---|
166 | </dd>
|
---|
167 | <dt><code>#pragma GCC warning <var>message</var></code></dt>
|
---|
168 | <dt><code>_Pragma ("GCC warning <var>message</var>")</code></dt>
|
---|
169 | <dd><p>Equivalent to <code>#warning</code>. Its advantage is that the
|
---|
170 | <code>_Pragma</code> form can be included in a macro definition.
|
---|
171 | </p>
|
---|
172 | </dd>
|
---|
173 | <dt><code>#pragma GCC error <var>message</var></code></dt>
|
---|
174 | <dt><code>_Pragma ("GCC error <var>message</var>")</code></dt>
|
---|
175 | <dd><p>Equivalent to <code>#error</code>. Its advantage is that the
|
---|
176 | <code>_Pragma</code> form can be included in a macro definition.
|
---|
177 | </p>
|
---|
178 | </dd>
|
---|
179 | <dt><code>#pragma GCC message <var>message</var></code></dt>
|
---|
180 | <dt><code>_Pragma ("GCC message <var>message</var>")</code></dt>
|
---|
181 | <dd><p>Similar to ‘<samp>GCC warning</samp>’ and ‘<samp>GCC error</samp>’, this simply prints an
|
---|
182 | informational message, and could be used to include additional warning
|
---|
183 | or error text without triggering more warnings or errors. (Note that
|
---|
184 | unlike ‘<samp>warning</samp>’ and ‘<samp>error</samp>’, ‘<samp>message</samp>’ does not include
|
---|
185 | ‘<samp>GCC</samp>’ as part of the pragma.)
|
---|
186 | </p></dd>
|
---|
187 | </dl>
|
---|
188 |
|
---|
189 | <hr>
|
---|
190 | <div class="header">
|
---|
191 | <p>
|
---|
192 | Next: <a href="Severity-Pragmas.html" accesskey="n" rel="next">Severity Pragmas</a>, Up: <a href="Pragmas.html" accesskey="u" rel="up">Pragmas</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Symbol-Index.html" title="Index" rel="index">Index</a>]</p>
|
---|
193 | </div>
|
---|
194 |
|
---|
195 |
|
---|
196 |
|
---|
197 | </body>
|
---|
198 | </html>
|
---|