Common-Variable-Attributes.html 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <!-- Copyright (C) 1988-2017 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; with the
  7. Invariant Sections being "Funding Free Software", the Front-Cover
  8. Texts being (a) (see below), and with the Back-Cover Texts being (b)
  9. (see below). A copy of the license is included in the section entitled
  10. "GNU Free Documentation License".
  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 5.2, http://www.gnu.org/software/texinfo/ -->
  18. <head>
  19. <title>Using the GNU Compiler Collection (GCC): Common Variable Attributes</title>
  20. <meta name="description" content="Using the GNU Compiler Collection (GCC): Common Variable Attributes">
  21. <meta name="keywords" content="Using the GNU Compiler Collection (GCC): Common Variable Attributes">
  22. <meta name="resource-type" content="document">
  23. <meta name="distribution" content="global">
  24. <meta name="Generator" content="makeinfo">
  25. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  26. <link href="index.html#Top" rel="start" title="Top">
  27. <link href="Option-Index.html#Option-Index" rel="index" title="Option Index">
  28. <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
  29. <link href="Variable-Attributes.html#Variable-Attributes" rel="up" title="Variable Attributes">
  30. <link href="AVR-Variable-Attributes.html#AVR-Variable-Attributes" rel="next" title="AVR Variable Attributes">
  31. <link href="Variable-Attributes.html#Variable-Attributes" rel="prev" title="Variable Attributes">
  32. <style type="text/css">
  33. <!--
  34. a.summary-letter {text-decoration: none}
  35. blockquote.smallquotation {font-size: smaller}
  36. div.display {margin-left: 3.2em}
  37. div.example {margin-left: 3.2em}
  38. div.indentedblock {margin-left: 3.2em}
  39. div.lisp {margin-left: 3.2em}
  40. div.smalldisplay {margin-left: 3.2em}
  41. div.smallexample {margin-left: 3.2em}
  42. div.smallindentedblock {margin-left: 3.2em; font-size: smaller}
  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.nocodebreak {white-space:nowrap}
  54. span.nolinebreak {white-space:nowrap}
  55. span.roman {font-family:serif; font-weight:normal}
  56. span.sansserif {font-family:sans-serif; font-weight:normal}
  57. ul.no-bullet {list-style: none}
  58. -->
  59. </style>
  60. </head>
  61. <body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
  62. <a name="Common-Variable-Attributes"></a>
  63. <div class="header">
  64. <p>
  65. Next: <a href="AVR-Variable-Attributes.html#AVR-Variable-Attributes" accesskey="n" rel="next">AVR Variable Attributes</a>, Up: <a href="Variable-Attributes.html#Variable-Attributes" accesskey="u" rel="up">Variable Attributes</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Option-Index.html#Option-Index" title="Index" rel="index">Index</a>]</p>
  66. </div>
  67. <hr>
  68. <a name="Common-Variable-Attributes-1"></a>
  69. <h4 class="subsection">6.32.1 Common Variable Attributes</h4>
  70. <p>The following attributes are supported on most targets.
  71. </p>
  72. <dl compact="compact">
  73. <dd><a name="index-aligned-variable-attribute"></a>
  74. </dd>
  75. <dt><code>aligned (<var>alignment</var>)</code></dt>
  76. <dd><p>This attribute specifies a minimum alignment for the variable or
  77. structure field, measured in bytes. For example, the declaration:
  78. </p>
  79. <div class="smallexample">
  80. <pre class="smallexample">int x __attribute__ ((aligned (16))) = 0;
  81. </pre></div>
  82. <p>causes the compiler to allocate the global variable <code>x</code> on a
  83. 16-byte boundary. On a 68040, this could be used in conjunction with
  84. an <code>asm</code> expression to access the <code>move16</code> instruction which
  85. requires 16-byte aligned operands.
  86. </p>
  87. <p>You can also specify the alignment of structure fields. For example, to
  88. create a double-word aligned <code>int</code> pair, you could write:
  89. </p>
  90. <div class="smallexample">
  91. <pre class="smallexample">struct foo { int x[2] __attribute__ ((aligned (8))); };
  92. </pre></div>
  93. <p>This is an alternative to creating a union with a <code>double</code> member,
  94. which forces the union to be double-word aligned.
  95. </p>
  96. <p>As in the preceding examples, you can explicitly specify the alignment
  97. (in bytes) that you wish the compiler to use for a given variable or
  98. structure field. Alternatively, you can leave out the alignment factor
  99. and just ask the compiler to align a variable or field to the
  100. default alignment for the target architecture you are compiling for.
  101. The default alignment is sufficient for all scalar types, but may not be
  102. enough for all vector types on a target that supports vector operations.
  103. The default alignment is fixed for a particular target ABI.
  104. </p>
  105. <p>GCC also provides a target specific macro <code>__BIGGEST_ALIGNMENT__</code>,
  106. which is the largest alignment ever used for any data type on the
  107. target machine you are compiling for. For example, you could write:
  108. </p>
  109. <div class="smallexample">
  110. <pre class="smallexample">short array[3] __attribute__ ((aligned (__BIGGEST_ALIGNMENT__)));
  111. </pre></div>
  112. <p>The compiler automatically sets the alignment for the declared
  113. variable or field to <code>__BIGGEST_ALIGNMENT__</code>. Doing this can
  114. often make copy operations more efficient, because the compiler can
  115. use whatever instructions copy the biggest chunks of memory when
  116. performing copies to or from the variables or fields that you have
  117. aligned this way. Note that the value of <code>__BIGGEST_ALIGNMENT__</code>
  118. may change depending on command-line options.
  119. </p>
  120. <p>When used on a struct, or struct member, the <code>aligned</code> attribute can
  121. only increase the alignment; in order to decrease it, the <code>packed</code>
  122. attribute must be specified as well. When used as part of a typedef, the
  123. <code>aligned</code> attribute can both increase and decrease alignment, and
  124. specifying the <code>packed</code> attribute generates a warning.
  125. </p>
  126. <p>Note that the effectiveness of <code>aligned</code> attributes may be limited
  127. by inherent limitations in your linker. On many systems, the linker is
  128. only able to arrange for variables to be aligned up to a certain maximum
  129. alignment. (For some linkers, the maximum supported alignment may
  130. be very very small.) If your linker is only able to align variables
  131. up to a maximum of 8-byte alignment, then specifying <code>aligned(16)</code>
  132. in an <code>__attribute__</code> still only provides you with 8-byte
  133. alignment. See your linker documentation for further information.
  134. </p>
  135. <p>The <code>aligned</code> attribute can also be used for functions
  136. (see <a href="Common-Function-Attributes.html#Common-Function-Attributes">Common Function Attributes</a>.)
  137. </p>
  138. </dd>
  139. <dt><code>cleanup (<var>cleanup_function</var>)</code></dt>
  140. <dd><a name="index-cleanup-variable-attribute"></a>
  141. <p>The <code>cleanup</code> attribute runs a function when the variable goes
  142. out of scope. This attribute can only be applied to auto function
  143. scope variables; it may not be applied to parameters or variables
  144. with static storage duration. The function must take one parameter,
  145. a pointer to a type compatible with the variable. The return value
  146. of the function (if any) is ignored.
  147. </p>
  148. <p>If <samp>-fexceptions</samp> is enabled, then <var>cleanup_function</var>
  149. is run during the stack unwinding that happens during the
  150. processing of the exception. Note that the <code>cleanup</code> attribute
  151. does not allow the exception to be caught, only to perform an action.
  152. It is undefined what happens if <var>cleanup_function</var> does not
  153. return normally.
  154. </p>
  155. </dd>
  156. <dt><code>common</code></dt>
  157. <dt><code>nocommon</code></dt>
  158. <dd><a name="index-common-variable-attribute"></a>
  159. <a name="index-nocommon-variable-attribute"></a>
  160. <a name="index-fcommon"></a>
  161. <a name="index-fno_002dcommon-1"></a>
  162. <p>The <code>common</code> attribute requests GCC to place a variable in
  163. &ldquo;common&rdquo; storage. The <code>nocommon</code> attribute requests the
  164. opposite&mdash;to allocate space for it directly.
  165. </p>
  166. <p>These attributes override the default chosen by the
  167. <samp>-fno-common</samp> and <samp>-fcommon</samp> flags respectively.
  168. </p>
  169. </dd>
  170. <dt><code>deprecated</code></dt>
  171. <dt><code>deprecated (<var>msg</var>)</code></dt>
  172. <dd><a name="index-deprecated-variable-attribute"></a>
  173. <p>The <code>deprecated</code> attribute results in a warning if the variable
  174. is used anywhere in the source file. This is useful when identifying
  175. variables that are expected to be removed in a future version of a
  176. program. The warning also includes the location of the declaration
  177. of the deprecated variable, to enable users to easily find further
  178. information about why the variable is deprecated, or what they should
  179. do instead. Note that the warning only occurs for uses:
  180. </p>
  181. <div class="smallexample">
  182. <pre class="smallexample">extern int old_var __attribute__ ((deprecated));
  183. extern int old_var;
  184. int new_fn () { return old_var; }
  185. </pre></div>
  186. <p>results in a warning on line 3 but not line 2. The optional <var>msg</var>
  187. argument, which must be a string, is printed in the warning if
  188. present.
  189. </p>
  190. <p>The <code>deprecated</code> attribute can also be used for functions and
  191. types (see <a href="Common-Function-Attributes.html#Common-Function-Attributes">Common Function Attributes</a>,
  192. see <a href="Common-Type-Attributes.html#Common-Type-Attributes">Common Type Attributes</a>).
  193. </p>
  194. </dd>
  195. <dt><code>mode (<var>mode</var>)</code></dt>
  196. <dd><a name="index-mode-variable-attribute"></a>
  197. <p>This attribute specifies the data type for the declaration&mdash;whichever
  198. type corresponds to the mode <var>mode</var>. This in effect lets you
  199. request an integer or floating-point type according to its width.
  200. </p>
  201. <p>See <a href="http://gcc.gnu.org/onlinedocs/gccint/Machine-Modes.html#Machine-Modes">Machine Modes</a> in <cite>GNU Compiler Collection (GCC) Internals</cite>,
  202. for a list of the possible keywords for <var>mode</var>.
  203. You may also specify a mode of <code>byte</code> or <code>__byte__</code> to
  204. indicate the mode corresponding to a one-byte integer, <code>word</code> or
  205. <code>__word__</code> for the mode of a one-word integer, and <code>pointer</code>
  206. or <code>__pointer__</code> for the mode used to represent pointers.
  207. </p>
  208. </dd>
  209. <dt><code>packed</code></dt>
  210. <dd><a name="index-packed-variable-attribute"></a>
  211. <p>The <code>packed</code> attribute specifies that a variable or structure field
  212. should have the smallest possible alignment&mdash;one byte for a variable,
  213. and one bit for a field, unless you specify a larger value with the
  214. <code>aligned</code> attribute.
  215. </p>
  216. <p>Here is a structure in which the field <code>x</code> is packed, so that it
  217. immediately follows <code>a</code>:
  218. </p>
  219. <div class="smallexample">
  220. <pre class="smallexample">struct foo
  221. {
  222. char a;
  223. int x[2] __attribute__ ((packed));
  224. };
  225. </pre></div>
  226. <p><em>Note:</em> The 4.1, 4.2 and 4.3 series of GCC ignore the
  227. <code>packed</code> attribute on bit-fields of type <code>char</code>. This has
  228. been fixed in GCC 4.4 but the change can lead to differences in the
  229. structure layout. See the documentation of
  230. <samp>-Wpacked-bitfield-compat</samp> for more information.
  231. </p>
  232. </dd>
  233. <dt><code>section (&quot;<var>section-name</var>&quot;)</code></dt>
  234. <dd><a name="index-section-variable-attribute"></a>
  235. <p>Normally, the compiler places the objects it generates in sections like
  236. <code>data</code> and <code>bss</code>. Sometimes, however, you need additional sections,
  237. or you need certain particular variables to appear in special sections,
  238. for example to map to special hardware. The <code>section</code>
  239. attribute specifies that a variable (or function) lives in a particular
  240. section. For example, this small program uses several specific section names:
  241. </p>
  242. <div class="smallexample">
  243. <pre class="smallexample">struct duart a __attribute__ ((section (&quot;DUART_A&quot;))) = { 0 };
  244. struct duart b __attribute__ ((section (&quot;DUART_B&quot;))) = { 0 };
  245. char stack[10000] __attribute__ ((section (&quot;STACK&quot;))) = { 0 };
  246. int init_data __attribute__ ((section (&quot;INITDATA&quot;)));
  247. main()
  248. {
  249. /* <span class="roman">Initialize stack pointer</span> */
  250. init_sp (stack + sizeof (stack));
  251. /* <span class="roman">Initialize initialized data</span> */
  252. memcpy (&amp;init_data, &amp;data, &amp;edata - &amp;data);
  253. /* <span class="roman">Turn on the serial ports</span> */
  254. init_duart (&amp;a);
  255. init_duart (&amp;b);
  256. }
  257. </pre></div>
  258. <p>Use the <code>section</code> attribute with
  259. <em>global</em> variables and not <em>local</em> variables,
  260. as shown in the example.
  261. </p>
  262. <p>You may use the <code>section</code> attribute with initialized or
  263. uninitialized global variables but the linker requires
  264. each object be defined once, with the exception that uninitialized
  265. variables tentatively go in the <code>common</code> (or <code>bss</code>) section
  266. and can be multiply &ldquo;defined&rdquo;. Using the <code>section</code> attribute
  267. changes what section the variable goes into and may cause the
  268. linker to issue an error if an uninitialized variable has multiple
  269. definitions. You can force a variable to be initialized with the
  270. <samp>-fno-common</samp> flag or the <code>nocommon</code> attribute.
  271. </p>
  272. <p>Some file formats do not support arbitrary sections so the <code>section</code>
  273. attribute is not available on all platforms.
  274. If you need to map the entire contents of a module to a particular
  275. section, consider using the facilities of the linker instead.
  276. </p>
  277. </dd>
  278. <dt><code>tls_model (&quot;<var>tls_model</var>&quot;)</code></dt>
  279. <dd><a name="index-tls_005fmodel-variable-attribute"></a>
  280. <p>The <code>tls_model</code> attribute sets thread-local storage model
  281. (see <a href="Thread_002dLocal.html#Thread_002dLocal">Thread-Local</a>) of a particular <code>__thread</code> variable,
  282. overriding <samp>-ftls-model=</samp> command-line switch on a per-variable
  283. basis.
  284. The <var>tls_model</var> argument should be one of <code>global-dynamic</code>,
  285. <code>local-dynamic</code>, <code>initial-exec</code> or <code>local-exec</code>.
  286. </p>
  287. <p>Not all targets support this attribute.
  288. </p>
  289. </dd>
  290. <dt><code>unused</code></dt>
  291. <dd><a name="index-unused-variable-attribute"></a>
  292. <p>This attribute, attached to a variable, means that the variable is meant
  293. to be possibly unused. GCC does not produce a warning for this
  294. variable.
  295. </p>
  296. </dd>
  297. <dt><code>used</code></dt>
  298. <dd><a name="index-used-variable-attribute"></a>
  299. <p>This attribute, attached to a variable with static storage, means that
  300. the variable must be emitted even if it appears that the variable is not
  301. referenced.
  302. </p>
  303. <p>When applied to a static data member of a C++ class template, the
  304. attribute also means that the member is instantiated if the
  305. class itself is instantiated.
  306. </p>
  307. </dd>
  308. <dt><code>vector_size (<var>bytes</var>)</code></dt>
  309. <dd><a name="index-vector_005fsize-variable-attribute"></a>
  310. <p>This attribute specifies the vector size for the variable, measured in
  311. bytes. For example, the declaration:
  312. </p>
  313. <div class="smallexample">
  314. <pre class="smallexample">int foo __attribute__ ((vector_size (16)));
  315. </pre></div>
  316. <p>causes the compiler to set the mode for <code>foo</code>, to be 16 bytes,
  317. divided into <code>int</code> sized units. Assuming a 32-bit int (a vector of
  318. 4 units of 4 bytes), the corresponding mode of <code>foo</code> is V4SI.
  319. </p>
  320. <p>This attribute is only applicable to integral and float scalars,
  321. although arrays, pointers, and function return values are allowed in
  322. conjunction with this construct.
  323. </p>
  324. <p>Aggregates with this attribute are invalid, even if they are of the same
  325. size as a corresponding scalar. For example, the declaration:
  326. </p>
  327. <div class="smallexample">
  328. <pre class="smallexample">struct S { int a; };
  329. struct S __attribute__ ((vector_size (16))) foo;
  330. </pre></div>
  331. <p>is invalid even if the size of the structure is the same as the size of
  332. the <code>int</code>.
  333. </p>
  334. </dd>
  335. <dt><code>visibility (&quot;<var>visibility_type</var>&quot;)</code></dt>
  336. <dd><a name="index-visibility-variable-attribute"></a>
  337. <p>This attribute affects the linkage of the declaration to which it is attached.
  338. The <code>visibility</code> attribute is described in
  339. <a href="Common-Function-Attributes.html#Common-Function-Attributes">Common Function Attributes</a>.
  340. </p>
  341. </dd>
  342. <dt><code>weak</code></dt>
  343. <dd><a name="index-weak-variable-attribute"></a>
  344. <p>The <code>weak</code> attribute is described in
  345. <a href="Common-Function-Attributes.html#Common-Function-Attributes">Common Function Attributes</a>.
  346. </p>
  347. </dd>
  348. </dl>
  349. <hr>
  350. <div class="header">
  351. <p>
  352. Next: <a href="AVR-Variable-Attributes.html#AVR-Variable-Attributes" accesskey="n" rel="next">AVR Variable Attributes</a>, Up: <a href="Variable-Attributes.html#Variable-Attributes" accesskey="u" rel="up">Variable Attributes</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Option-Index.html#Option-Index" title="Index" rel="index">Index</a>]</p>
  353. </div>
  354. </body>
  355. </html>