您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

209 行
9.9KB

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