Inline.html 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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): Inline</title>
  20. <meta name="description" content="Using the GNU Compiler Collection (GCC): Inline">
  21. <meta name="keywords" content="Using the GNU Compiler Collection (GCC): Inline">
  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="Volatiles.html#Volatiles" rel="next" title="Volatiles">
  31. <link href="Alignment.html#Alignment" rel="prev" title="Alignment">
  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="Inline"></a>
  63. <div class="header">
  64. <p>
  65. Next: <a href="Volatiles.html#Volatiles" accesskey="n" rel="next">Volatiles</a>, Previous: <a href="Alignment.html#Alignment" accesskey="p" rel="prev">Alignment</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="An-Inline-Function-is-As-Fast-As-a-Macro"></a>
  69. <h3 class="section">6.43 An Inline Function is As Fast As a Macro</h3>
  70. <a name="index-inline-functions"></a>
  71. <a name="index-integrating-function-code"></a>
  72. <a name="index-open-coding"></a>
  73. <a name="index-macros_002c-inline-alternative"></a>
  74. <p>By declaring a function inline, you can direct GCC to make
  75. calls to that function faster. One way GCC can achieve this is to
  76. integrate that function&rsquo;s code into the code for its callers. This
  77. makes execution faster by eliminating the function-call overhead; in
  78. addition, if any of the actual argument values are constant, their
  79. known values may permit simplifications at compile time so that not
  80. all of the inline function&rsquo;s code needs to be included. The effect on
  81. code size is less predictable; object code may be larger or smaller
  82. with function inlining, depending on the particular case. You can
  83. also direct GCC to try to integrate all &ldquo;simple enough&rdquo; functions
  84. into their callers with the option <samp>-finline-functions</samp>.
  85. </p>
  86. <p>GCC implements three different semantics of declaring a function
  87. inline. One is available with <samp>-std=gnu89</samp> or
  88. <samp>-fgnu89-inline</samp> or when <code>gnu_inline</code> attribute is present
  89. on all inline declarations, another when
  90. <samp>-std=c99</samp>, <samp>-std=c11</samp>,
  91. <samp>-std=gnu99</samp> or <samp>-std=gnu11</samp>
  92. (without <samp>-fgnu89-inline</samp>), and the third
  93. is used when compiling C++.
  94. </p>
  95. <p>To declare a function inline, use the <code>inline</code> keyword in its
  96. declaration, like this:
  97. </p>
  98. <div class="smallexample">
  99. <pre class="smallexample">static inline int
  100. inc (int *a)
  101. {
  102. return (*a)++;
  103. }
  104. </pre></div>
  105. <p>If you are writing a header file to be included in ISO C90 programs, write
  106. <code>__inline__</code> instead of <code>inline</code>. See <a href="Alternate-Keywords.html#Alternate-Keywords">Alternate Keywords</a>.
  107. </p>
  108. <p>The three types of inlining behave similarly in two important cases:
  109. when the <code>inline</code> keyword is used on a <code>static</code> function,
  110. like the example above, and when a function is first declared without
  111. using the <code>inline</code> keyword and then is defined with
  112. <code>inline</code>, like this:
  113. </p>
  114. <div class="smallexample">
  115. <pre class="smallexample">extern int inc (int *a);
  116. inline int
  117. inc (int *a)
  118. {
  119. return (*a)++;
  120. }
  121. </pre></div>
  122. <p>In both of these common cases, the program behaves the same as if you
  123. had not used the <code>inline</code> keyword, except for its speed.
  124. </p>
  125. <a name="index-inline-functions_002c-omission-of"></a>
  126. <a name="index-fkeep_002dinline_002dfunctions-1"></a>
  127. <p>When a function is both inline and <code>static</code>, if all calls to the
  128. function are integrated into the caller, and the function&rsquo;s address is
  129. never used, then the function&rsquo;s own assembler code is never referenced.
  130. In this case, GCC does not actually output assembler code for the
  131. function, unless you specify the option <samp>-fkeep-inline-functions</samp>.
  132. If there is a nonintegrated call, then the function is compiled to
  133. assembler code as usual. The function must also be compiled as usual if
  134. the program refers to its address, because that cannot be inlined.
  135. </p>
  136. <a name="index-Winline-1"></a>
  137. <p>Note that certain usages in a function definition can make it unsuitable
  138. for inline substitution. Among these usages are: variadic functions,
  139. use of <code>alloca</code>, use of computed goto (see <a href="Labels-as-Values.html#Labels-as-Values">Labels as Values</a>),
  140. use of nonlocal goto, use of nested functions, use of <code>setjmp</code>, use
  141. of <code>__builtin_longjmp</code> and use of <code>__builtin_return</code> or
  142. <code>__builtin_apply_args</code>. Using <samp>-Winline</samp> warns when a
  143. function marked <code>inline</code> could not be substituted, and gives the
  144. reason for the failure.
  145. </p>
  146. <a name="index-automatic-inline-for-C_002b_002b-member-fns"></a>
  147. <a name="index-inline-automatic-for-C_002b_002b-member-fns"></a>
  148. <a name="index-member-fns_002c-automatically-inline"></a>
  149. <a name="index-C_002b_002b-member-fns_002c-automatically-inline"></a>
  150. <a name="index-fno_002ddefault_002dinline"></a>
  151. <p>As required by ISO C++, GCC considers member functions defined within
  152. the body of a class to be marked inline even if they are
  153. not explicitly declared with the <code>inline</code> keyword. You can
  154. override this with <samp>-fno-default-inline</samp>; see <a href="C_002b_002b-Dialect-Options.html#C_002b_002b-Dialect-Options">Options Controlling C++ Dialect</a>.
  155. </p>
  156. <p>GCC does not inline any functions when not optimizing unless you specify
  157. the &lsquo;<samp>always_inline</samp>&rsquo; attribute for the function, like this:
  158. </p>
  159. <div class="smallexample">
  160. <pre class="smallexample">/* <span class="roman">Prototype.</span> */
  161. inline void foo (const char) __attribute__((always_inline));
  162. </pre></div>
  163. <p>The remainder of this section is specific to GNU C90 inlining.
  164. </p>
  165. <a name="index-non_002dstatic-inline-function"></a>
  166. <p>When an inline function is not <code>static</code>, then the compiler must assume
  167. that there may be calls from other source files; since a global symbol can
  168. be defined only once in any program, the function must not be defined in
  169. the other source files, so the calls therein cannot be integrated.
  170. Therefore, a non-<code>static</code> inline function is always compiled on its
  171. own in the usual fashion.
  172. </p>
  173. <p>If you specify both <code>inline</code> and <code>extern</code> in the function
  174. definition, then the definition is used only for inlining. In no case
  175. is the function compiled on its own, not even if you refer to its
  176. address explicitly. Such an address becomes an external reference, as
  177. if you had only declared the function, and had not defined it.
  178. </p>
  179. <p>This combination of <code>inline</code> and <code>extern</code> has almost the
  180. effect of a macro. The way to use it is to put a function definition in
  181. a header file with these keywords, and put another copy of the
  182. definition (lacking <code>inline</code> and <code>extern</code>) in a library file.
  183. The definition in the header file causes most calls to the function
  184. to be inlined. If any uses of the function remain, they refer to
  185. the single copy in the library.
  186. </p>
  187. <hr>
  188. <div class="header">
  189. <p>
  190. Next: <a href="Volatiles.html#Volatiles" accesskey="n" rel="next">Volatiles</a>, Previous: <a href="Alignment.html#Alignment" accesskey="p" rel="prev">Alignment</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>
  191. </div>
  192. </body>
  193. </html>