Inline of GCC

Source: Internet
Author: User
Tags bit definition

In the GCC reference document, the inline keyword only describes the usage of the function definition, but does not mention whether it can be used in the function declaration (declare ).

The Inline keyword should not appear in the function declaration.

The Inline keyword is only recommended for the compiler to perform inline expansion, rather than forcing. In the GCC compiler, if the compilation optimization is set to O0, even the inline function will not be expanded inline unless the mandatory inline (_ attribute _ (always_inline) attribute is set. If the extension and function must be considered as functions, they must be expanded in the extension and processed in the function call.

1.1. Static inline

Compared with the static function, static inline of GCC only recommends that the compiler conduct Inline expansion during the call. GCC does not generate Independent Assembly codes for the static inline function, unless necessary (for example, function pointer call and recursive call );
I) do not expand
Recursion of the function itself; when the function address is used (assign a function pointer)
Ii) Expand
GCC will compile the code at its call without generating an independent assembly code for this function.

1.2. inline
GCC's inline is easier to understand: it can be considered as a common global function with the inline attribute. That is, in the file where its definition is located, its behavior is consistent with static inline: it will be expanded and compiled inline when it can be expanded. But in order to be able to call it outside the file, GCC will certainly generate an independent assembly code for it for external calls.
Example:
/***** Foo. c *****/
/* Define an inline function Foo ()*/
Inline Foo (void)
{
/* The compiler expands the functions it calls in this file */
/* But it will also generate Independent Assembly codes for Foo () like non-inline functions, so that functions outside the file can call Foo ();*/
}
 
Void func1 ()
{
/* Foo () in the same file may be expanded and compiled by the compiler rather than calling the assembly code generated above */
Foo ();
}

/***** Bar. c *****/
/* When Foo () is called in another file, the assembly code generated in the above file is called directly :*/

Extern Foo (); // declare Foo (). Note that the inline keyword cannot be included in the declaration.
Void func2 ()
{
/* Call the assembly code generated by the Foo () function in Foo. C */
Foo ();
}

Compared with normal extern functions, the inline function of GCC only recommends that the compiler conduct Inline expansion when calling the same file; GCC must generate an independent assembly code for the inline function for external file calls. In other files, the inline function is similar to the normal extern function. The Inline Function of GCC is global: it can be expanded inline as an inline function in the file, you can call it outside the file.
Static inline and inline of GCC are easier to understand and can be considered as adding inline attributes to common functions.

1.3. extern inline
The extern inline of GCC is very odd: an extern inline function will only be inline, and will never generate an independent assembly code !!! If a function must be treated as a common function, the call to this function is processed as an external reference. In addition, the extern inline function can have the same name as the external function, that is, when an external-defined global library function exists, defining an extern inline function with the same name is also valid.
Example:
/******* Foo. c *******/
Extern inline int Foo (int)
{
Printf ("% d \ n",-);
Return-;
}
Void func1 ()
{
......;
A = Foo (a); // ******** 1
P_foo = Foo; // ******** 2
B = p_foo (B); // ******** 3
}

/***** Foo2.c ******/
Int Foo (int)
{
Printf ("% d \ n", );
Return;
}
In this file, GCC will not generate the assembly code of the foo function.
At the first part of func1, the compiler will inline compile the foo function defined above, and its behavior is similar to the ordinary inline function, because such a call can carry out inline processing.
The address of the function named foo is applied at 2 in func1, but since the compiler will never generate an independent assembly code for the extern inline function, therefore, if you need to obtain the function address, compile the function to process it as an external reference and link it to the external Foo function (fill in the external function address ). At this time, if the global Foo function is not defined externally, the link will generate the foo function bit definition error. Since the global Foo (INT) function defined in func2.c, for the above example, A =-A will be made at 1, because it has inline Foo. foo function in C; B = B in 3, because actually the foo function in foo2.c is called!
Value of extern inline:
First, its behavior can be like a macro, you can use the version defined by extern inline in the file to replace the library functions defined by the external definition (provided that the file cannot be called inline );
Second, it allows a library function to be used inline whenever possible. Example:
In the c file of a library function, define a common version of library function libfunc:
/***** Lib. c *****/
Void libfunc (void)
{
....;
}
Then define it in the header file (note that it is not a declaration !) A version that implements the same extern inline:
/***** Lib. h *****/
Extern inline libfunc (void)
{
....;
}
When other files need to use this library function, the compiler will use the extern inline version in the header file as long as the library function is included in Lib. h. When expansion fails (function pointer reference, etc.), the compiler will reference the self-compiled normal version in Lib. C. That is, it seems to be a function that can be inline externally, so this should be the origin of GCC's extern inline meaning.
However, pay attention to the cost: the implementation of global functions in the C file must be exactly the same as that in the extern inline version in the same file. Otherwise, the function behavior inconsistency occurs during direct and indirect calls in the preceding example.

GCC will never generate Independent Assembly codes for the extern inline function; the extern inline function can be renamed with a global function, and can replace the externally defined global function within the file range; when using extern inline, you must comment out the document !!!!

**************************************** **************************************** *******
C99 inline
2.1. Static inline (same as static inline of GCC)
2.3. extern inline (not described)
2.2. inline
If an inline function is not declared as extern within the scope of the file, the expression of this function in the file is similar to the extern inline of GCC: during file calls, ** allow ** the compiler to use the inline version defined in the file, but ** allow ** external global functions with the same name. However, c99 does not explicitly indicate whether the compiler must use the inline version in the file, but is determined by the compiler manufacturer.
If the inline function is declared as extern in the file, the inline function will behave the same as the GCC inline function: this function becomes an "external definition" (which can be simply understood as a global function): it can be called externally and only has one such name in the program.

The following is an example of the features of inline in c99:
Inline double Fahr (double T)
{
Return (9.0 * T)/5.0 + 32.0;
}

Inline double CELS (double T)
{
Return (5.0 * (t-32.0)/9.0;
}

Extern double Fahr (double); ①

Double convert (INT is_fahr, double temp)
{
Return is_fahr? CELS (temp): Fahr (temp); ②
}

The Fahr function is a global function: Because Fahr is declared as extern at ①, therefore, when you call Fahr at ②, you must use the version defined in this file (except that the compiler can expand the call here in line ). This function can also be called outside the file (it indicates that, like the GCC inline, the compiler will generate an independent assembly code for Fahr in this case ).
The CELS function is called "inline definition" because it is not declared as extern within the file range ", at this time, it can only act on the scope of this file (just like a static function). There may also be a global function with the same name as CELS. When you call CELS at ②, the compiler may choose to use the inline version in this file, or it may run to call the external defined CELS function (c99 does not specify the behavior at this time, however, the compiler will always try to use the inline version defined in the file, otherwise the inline function will have no meaning ). Here, we can see that the inline function that is not declared as extern in c99 is very similar to the extern inline function in GCC: the inline function in this file can be used as an alternative to the external library function.
The inline function defined in the c99 standard is vague, and whether the inline function is declared as extern within the file range is essentially different.

If compared with the inline function of GCC, an inline function declared as extern is basically equivalent to the normal inline function of GCC; an inline function that is not declared as extern is basically equivalent to the extern inline function of GCC.

Compatibility:
Because the inline function of c99 is so weird, we recommend that you create an extern Declaration for all inline functions in the header file:
/******* Foo. h ********/
Extern Foo ();

Include this header file in the C file that defines the inline function:
/******* Foo. c ********/
# Include "foo. H"
Inline void Foo ()
{
...;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.