1<html lang="en"> 2<head> 3<title>Inline - Using the GNU Compiler Collection (GCC)</title> 4<meta http-equiv="Content-Type" content="text/html"> 5<meta name="description" content="Using the GNU Compiler Collection (GCC)"> 6<meta name="generator" content="makeinfo 4.13"> 7<link title="Top" rel="start" href="index.html#Top"> 8<link rel="up" href="C-Extensions.html#C-Extensions" title="C Extensions"> 9<link rel="prev" href="Alignment.html#Alignment" title="Alignment"> 10<link rel="next" href="Volatiles.html#Volatiles" title="Volatiles"> 11<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage"> 12<!-- 13Copyright (C) 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 141998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 152010 Free Software Foundation, Inc. 16 17Permission is granted to copy, distribute and/or modify this document 18under the terms of the GNU Free Documentation License, Version 1.3 or 19any later version published by the Free Software Foundation; with the 20Invariant Sections being ``Funding Free Software'', the Front-Cover 21Texts being (a) (see below), and with the Back-Cover Texts being (b) 22(see below). A copy of the license is included in the section entitled 23``GNU Free Documentation License''. 24 25(a) The FSF's Front-Cover Text is: 26 27 A GNU Manual 28 29(b) The FSF's Back-Cover Text is: 30 31 You have freedom to copy and modify this GNU Manual, like GNU 32 software. Copies published by the Free Software Foundation raise 33 funds for GNU development.--> 34<meta http-equiv="Content-Style-Type" content="text/css"> 35<style type="text/css"><!-- 36 pre.display { font-family:inherit } 37 pre.format { font-family:inherit } 38 pre.smalldisplay { font-family:inherit; font-size:smaller } 39 pre.smallformat { font-family:inherit; font-size:smaller } 40 pre.smallexample { font-size:smaller } 41 pre.smalllisp { font-size:smaller } 42 span.sc { font-variant:small-caps } 43 span.roman { font-family:serif; font-weight:normal; } 44 span.sansserif { font-family:sans-serif; font-weight:normal; } 45--></style> 46<link rel="stylesheet" type="text/css" href="../cs.css"> 47</head> 48<body> 49<div class="node"> 50<a name="Inline"></a> 51<p> 52Next: <a rel="next" accesskey="n" href="Volatiles.html#Volatiles">Volatiles</a>, 53Previous: <a rel="previous" accesskey="p" href="Alignment.html#Alignment">Alignment</a>, 54Up: <a rel="up" accesskey="u" href="C-Extensions.html#C-Extensions">C Extensions</a> 55<hr> 56</div> 57 58<h3 class="section">6.39 An Inline Function is As Fast As a Macro</h3> 59 60<p><a name="index-inline-functions-2603"></a><a name="index-integrating-function-code-2604"></a><a name="index-open-coding-2605"></a><a name="index-macros_002c-inline-alternative-2606"></a> 61By declaring a function inline, you can direct GCC to make 62calls to that function faster. One way GCC can achieve this is to 63integrate that function's code into the code for its callers. This 64makes execution faster by eliminating the function-call overhead; in 65addition, if any of the actual argument values are constant, their 66known values may permit simplifications at compile time so that not 67all of the inline function's code needs to be included. The effect on 68code size is less predictable; object code may be larger or smaller 69with function inlining, depending on the particular case. You can 70also direct GCC to try to integrate all “simple enough” functions 71into their callers with the option <samp><span class="option">-finline-functions</span></samp>. 72 73 <p>GCC implements three different semantics of declaring a function 74inline. One is available with <samp><span class="option">-std=gnu89</span></samp> or 75<samp><span class="option">-fgnu89-inline</span></samp> or when <code>gnu_inline</code> attribute is present 76on all inline declarations, another when 77<samp><span class="option">-std=c99</span></samp>, <samp><span class="option">-std=c1x</span></samp>, 78<samp><span class="option">-std=gnu99</span></samp> or <samp><span class="option">-std=gnu1x</span></samp> 79(without <samp><span class="option">-fgnu89-inline</span></samp>), and the third 80is used when compiling C++. 81 82 <p>To declare a function inline, use the <code>inline</code> keyword in its 83declaration, like this: 84 85<pre class="smallexample"> static inline int 86 inc (int *a) 87 { 88 return (*a)++; 89 } 90</pre> 91 <p>If you are writing a header file to be included in ISO C90 programs, write 92<code>__inline__</code> instead of <code>inline</code>. See <a href="Alternate-Keywords.html#Alternate-Keywords">Alternate Keywords</a>. 93 94 <p>The three types of inlining behave similarly in two important cases: 95when the <code>inline</code> keyword is used on a <code>static</code> function, 96like the example above, and when a function is first declared without 97using the <code>inline</code> keyword and then is defined with 98<code>inline</code>, like this: 99 100<pre class="smallexample"> extern int inc (int *a); 101 inline int 102 inc (int *a) 103 { 104 return (*a)++; 105 } 106</pre> 107 <p>In both of these common cases, the program behaves the same as if you 108had not used the <code>inline</code> keyword, except for its speed. 109 110 <p><a name="index-inline-functions_002c-omission-of-2607"></a><a name="index-fkeep_002dinline_002dfunctions-2608"></a>When a function is both inline and <code>static</code>, if all calls to the 111function are integrated into the caller, and the function's address is 112never used, then the function's own assembler code is never referenced. 113In this case, GCC does not actually output assembler code for the 114function, unless you specify the option <samp><span class="option">-fkeep-inline-functions</span></samp>. 115Some calls cannot be integrated for various reasons (in particular, 116calls that precede the function's definition cannot be integrated, and 117neither can recursive calls within the definition). If there is a 118nonintegrated call, then the function is compiled to assembler code as 119usual. The function must also be compiled as usual if the program 120refers to its address, because that can't be inlined. 121 122 <p><a name="index-Winline-2609"></a>Note that certain usages in a function definition can make it unsuitable 123for inline substitution. Among these usages are: use of varargs, use of 124alloca, use of variable sized data types (see <a href="Variable-Length.html#Variable-Length">Variable Length</a>), 125use of computed goto (see <a href="Labels-as-Values.html#Labels-as-Values">Labels as Values</a>), use of nonlocal goto, 126and nested functions (see <a href="Nested-Functions.html#Nested-Functions">Nested Functions</a>). Using <samp><span class="option">-Winline</span></samp> 127will warn when a function marked <code>inline</code> could not be substituted, 128and will give the reason for the failure. 129 130 <p><a name="index-automatic-_0040code_007binline_007d-for-C_002b_002b-member-fns-2610"></a><a name="index-g_t_0040code_007binline_007d-automatic-for-C_002b_002b-member-fns-2611"></a><a name="index-member-fns_002c-automatically-_0040code_007binline_007d-2612"></a><a name="index-C_002b_002b-member-fns_002c-automatically-_0040code_007binline_007d-2613"></a><a name="index-fno_002ddefault_002dinline-2614"></a>As required by ISO C++, GCC considers member functions defined within 131the body of a class to be marked inline even if they are 132not explicitly declared with the <code>inline</code> keyword. You can 133override this with <samp><span class="option">-fno-default-inline</span></samp>; see <a href="C_002b_002b-Dialect-Options.html#C_002b_002b-Dialect-Options">Options Controlling C++ Dialect</a>. 134 135 <p>GCC does not inline any functions when not optimizing unless you specify 136the ‘<samp><span class="samp">always_inline</span></samp>’ attribute for the function, like this: 137 138<pre class="smallexample"> /* <span class="roman">Prototype.</span> */ 139 inline void foo (const char) __attribute__((always_inline)); 140</pre> 141 <p>The remainder of this section is specific to GNU C90 inlining. 142 143 <p><a name="index-non_002dstatic-inline-function-2615"></a>When an inline function is not <code>static</code>, then the compiler must assume 144that there may be calls from other source files; since a global symbol can 145be defined only once in any program, the function must not be defined in 146the other source files, so the calls therein cannot be integrated. 147Therefore, a non-<code>static</code> inline function is always compiled on its 148own in the usual fashion. 149 150 <p>If you specify both <code>inline</code> and <code>extern</code> in the function 151definition, then the definition is used only for inlining. In no case 152is the function compiled on its own, not even if you refer to its 153address explicitly. Such an address becomes an external reference, as 154if you had only declared the function, and had not defined it. 155 156 <p>This combination of <code>inline</code> and <code>extern</code> has almost the 157effect of a macro. The way to use it is to put a function definition in 158a header file with these keywords, and put another copy of the 159definition (lacking <code>inline</code> and <code>extern</code>) in a library file. 160The definition in the header file will cause most calls to the function 161to be inlined. If any uses of the function remain, they will refer to 162the single copy in the library. 163 164 </body></html> 165 166