Function-Attributes.html 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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): Function Attributes</title>
  20. <meta name="description" content="Using the GNU Compiler Collection (GCC): Function Attributes">
  21. <meta name="keywords" content="Using the GNU Compiler Collection (GCC): Function 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="C-Extensions.html#C-Extensions" rel="up" title="C Extensions">
  30. <link href="Common-Function-Attributes.html#Common-Function-Attributes" rel="next" title="Common Function Attributes">
  31. <link href="Mixed-Declarations.html#Mixed-Declarations" rel="prev" title="Mixed Declarations">
  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="Function-Attributes"></a>
  63. <div class="header">
  64. <p>
  65. Next: <a href="Variable-Attributes.html#Variable-Attributes" accesskey="n" rel="next">Variable Attributes</a>, Previous: <a href="Mixed-Declarations.html#Mixed-Declarations" accesskey="p" rel="prev">Mixed Declarations</a>, Up: <a href="C-Extensions.html#C-Extensions" accesskey="u" rel="up">C Extensions</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="Declaring-Attributes-of-Functions"></a>
  69. <h3 class="section">6.31 Declaring Attributes of Functions</h3>
  70. <a name="index-function-attributes"></a>
  71. <a name="index-declaring-attributes-of-functions"></a>
  72. <a name="index-volatile-applied-to-function"></a>
  73. <a name="index-const-applied-to-function"></a>
  74. <p>In GNU C, you can use function attributes to declare certain things
  75. about functions called in your program which help the compiler
  76. optimize calls and check your code more carefully. For example, you
  77. can use attributes to declare that a function never returns
  78. (<code>noreturn</code>), returns a value depending only on its arguments
  79. (<code>pure</code>), or has <code>printf</code>-style arguments (<code>format</code>).
  80. </p>
  81. <p>You can also use attributes to control memory placement, code
  82. generation options or call/return conventions within the function
  83. being annotated. Many of these attributes are target-specific. For
  84. example, many targets support attributes for defining interrupt
  85. handler functions, which typically must follow special register usage
  86. and return conventions.
  87. </p>
  88. <p>Function attributes are introduced by the <code>__attribute__</code> keyword
  89. on a declaration, followed by an attribute specification inside double
  90. parentheses. You can specify multiple attributes in a declaration by
  91. separating them by commas within the double parentheses or by
  92. immediately following an attribute declaration with another attribute
  93. declaration. See <a href="Attribute-Syntax.html#Attribute-Syntax">Attribute Syntax</a>, for the exact rules on
  94. attribute syntax and placement.
  95. </p>
  96. <p>GCC also supports attributes on
  97. variable declarations (see <a href="Variable-Attributes.html#Variable-Attributes">Variable Attributes</a>),
  98. labels (see <a href="Label-Attributes.html#Label-Attributes">Label Attributes</a>),
  99. enumerators (see <a href="Enumerator-Attributes.html#Enumerator-Attributes">Enumerator Attributes</a>),
  100. statements (see <a href="Statement-Attributes.html#Statement-Attributes">Statement Attributes</a>),
  101. and types (see <a href="Type-Attributes.html#Type-Attributes">Type Attributes</a>).
  102. </p>
  103. <p>There is some overlap between the purposes of attributes and pragmas
  104. (see <a href="Pragmas.html#Pragmas">Pragmas Accepted by GCC</a>). It has been
  105. found convenient to use <code>__attribute__</code> to achieve a natural
  106. attachment of attributes to their corresponding declarations, whereas
  107. <code>#pragma</code> is of use for compatibility with other compilers
  108. or constructs that do not naturally form part of the grammar.
  109. </p>
  110. <p>In addition to the attributes documented here,
  111. GCC plugins may provide their own attributes.
  112. </p>
  113. <table class="menu" border="0" cellspacing="0">
  114. <tr><td align="left" valign="top">&bull; <a href="Common-Function-Attributes.html#Common-Function-Attributes" accesskey="1">Common Function Attributes</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
  115. </td></tr>
  116. <tr><td align="left" valign="top">&bull; <a href="AArch64-Function-Attributes.html#AArch64-Function-Attributes" accesskey="2">AArch64 Function Attributes</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
  117. </td></tr>
  118. <tr><td align="left" valign="top">&bull; <a href="ARC-Function-Attributes.html#ARC-Function-Attributes" accesskey="3">ARC Function Attributes</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
  119. </td></tr>
  120. <tr><td align="left" valign="top">&bull; <a href="ARM-Function-Attributes.html#ARM-Function-Attributes" accesskey="4">ARM Function Attributes</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
  121. </td></tr>
  122. <tr><td align="left" valign="top">&bull; <a href="AVR-Function-Attributes.html#AVR-Function-Attributes" accesskey="5">AVR Function Attributes</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
  123. </td></tr>
  124. <tr><td align="left" valign="top">&bull; <a href="Blackfin-Function-Attributes.html#Blackfin-Function-Attributes" accesskey="6">Blackfin Function Attributes</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
  125. </td></tr>
  126. <tr><td align="left" valign="top">&bull; <a href="CR16-Function-Attributes.html#CR16-Function-Attributes" accesskey="7">CR16 Function Attributes</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
  127. </td></tr>
  128. <tr><td align="left" valign="top">&bull; <a href="Epiphany-Function-Attributes.html#Epiphany-Function-Attributes" accesskey="8">Epiphany Function Attributes</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
  129. </td></tr>
  130. <tr><td align="left" valign="top">&bull; <a href="H8_002f300-Function-Attributes.html#H8_002f300-Function-Attributes" accesskey="9">H8/300 Function Attributes</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
  131. </td></tr>
  132. <tr><td align="left" valign="top">&bull; <a href="IA_002d64-Function-Attributes.html#IA_002d64-Function-Attributes">IA-64 Function Attributes</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
  133. </td></tr>
  134. <tr><td align="left" valign="top">&bull; <a href="M32C-Function-Attributes.html#M32C-Function-Attributes">M32C Function Attributes</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
  135. </td></tr>
  136. <tr><td align="left" valign="top">&bull; <a href="M32R_002fD-Function-Attributes.html#M32R_002fD-Function-Attributes">M32R/D Function Attributes</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
  137. </td></tr>
  138. <tr><td align="left" valign="top">&bull; <a href="m68k-Function-Attributes.html#m68k-Function-Attributes">m68k Function Attributes</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
  139. </td></tr>
  140. <tr><td align="left" valign="top">&bull; <a href="MCORE-Function-Attributes.html#MCORE-Function-Attributes">MCORE Function Attributes</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
  141. </td></tr>
  142. <tr><td align="left" valign="top">&bull; <a href="MeP-Function-Attributes.html#MeP-Function-Attributes">MeP Function Attributes</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
  143. </td></tr>
  144. <tr><td align="left" valign="top">&bull; <a href="MicroBlaze-Function-Attributes.html#MicroBlaze-Function-Attributes">MicroBlaze Function Attributes</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
  145. </td></tr>
  146. <tr><td align="left" valign="top">&bull; <a href="Microsoft-Windows-Function-Attributes.html#Microsoft-Windows-Function-Attributes">Microsoft Windows Function Attributes</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
  147. </td></tr>
  148. <tr><td align="left" valign="top">&bull; <a href="MIPS-Function-Attributes.html#MIPS-Function-Attributes">MIPS Function Attributes</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
  149. </td></tr>
  150. <tr><td align="left" valign="top">&bull; <a href="MSP430-Function-Attributes.html#MSP430-Function-Attributes">MSP430 Function Attributes</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
  151. </td></tr>
  152. <tr><td align="left" valign="top">&bull; <a href="NDS32-Function-Attributes.html#NDS32-Function-Attributes">NDS32 Function Attributes</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
  153. </td></tr>
  154. <tr><td align="left" valign="top">&bull; <a href="Nios-II-Function-Attributes.html#Nios-II-Function-Attributes">Nios II Function Attributes</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
  155. </td></tr>
  156. <tr><td align="left" valign="top">&bull; <a href="Nvidia-PTX-Function-Attributes.html#Nvidia-PTX-Function-Attributes">Nvidia PTX Function Attributes</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
  157. </td></tr>
  158. <tr><td align="left" valign="top">&bull; <a href="PowerPC-Function-Attributes.html#PowerPC-Function-Attributes">PowerPC Function Attributes</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
  159. </td></tr>
  160. <tr><td align="left" valign="top">&bull; <a href="RL78-Function-Attributes.html#RL78-Function-Attributes">RL78 Function Attributes</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
  161. </td></tr>
  162. <tr><td align="left" valign="top">&bull; <a href="RX-Function-Attributes.html#RX-Function-Attributes">RX Function Attributes</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
  163. </td></tr>
  164. <tr><td align="left" valign="top">&bull; <a href="S_002f390-Function-Attributes.html#S_002f390-Function-Attributes">S/390 Function Attributes</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
  165. </td></tr>
  166. <tr><td align="left" valign="top">&bull; <a href="SH-Function-Attributes.html#SH-Function-Attributes">SH Function Attributes</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
  167. </td></tr>
  168. <tr><td align="left" valign="top">&bull; <a href="SPU-Function-Attributes.html#SPU-Function-Attributes">SPU Function Attributes</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
  169. </td></tr>
  170. <tr><td align="left" valign="top">&bull; <a href="Symbian-OS-Function-Attributes.html#Symbian-OS-Function-Attributes">Symbian OS Function Attributes</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
  171. </td></tr>
  172. <tr><td align="left" valign="top">&bull; <a href="V850-Function-Attributes.html#V850-Function-Attributes">V850 Function Attributes</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
  173. </td></tr>
  174. <tr><td align="left" valign="top">&bull; <a href="Visium-Function-Attributes.html#Visium-Function-Attributes">Visium Function Attributes</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
  175. </td></tr>
  176. <tr><td align="left" valign="top">&bull; <a href="x86-Function-Attributes.html#x86-Function-Attributes">x86 Function Attributes</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
  177. </td></tr>
  178. <tr><td align="left" valign="top">&bull; <a href="Xstormy16-Function-Attributes.html#Xstormy16-Function-Attributes">Xstormy16 Function Attributes</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
  179. </td></tr>
  180. </table>
  181. <hr>
  182. <div class="header">
  183. <p>
  184. Next: <a href="Variable-Attributes.html#Variable-Attributes" accesskey="n" rel="next">Variable Attributes</a>, Previous: <a href="Mixed-Declarations.html#Mixed-Declarations" accesskey="p" rel="prev">Mixed Declarations</a>, Up: <a href="C-Extensions.html#C-Extensions" accesskey="u" rel="up">C Extensions</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>
  185. </div>
  186. </body>
  187. </html>