Support for the inline function by the GCC compiler

Source: Internet
Author: User

C99version ofCthe language introducedinlinekeyword start supportinlinefunction, before which the traditionalCLanguage (C89) is notinlinekeyword, nor does it supportinlinefunction. But mostC89the compiler willinlineas an additional feature, it was added in early. GCCIt's no exception, butGCCIncreaseinlinefeature whenC99It's not finalized yet.GCCininlinethe Semantics andC99There is a slight difference. Here's the main point.GCCin theinlinefeature.

In addition, this article only discusses the C language,theC + + Standard is very early support for inline, but the semantics of inline C and C + + are slightly different.

First, give one of the simplest examples :

Main.c#include <stdio.h>int foo (void); int main () {    int a = foo ();    printf ("A =%d", a);    return 0;} foo.cinline int Inline_func (int a) {    return a + 1;} int foo (void) {    int x = 5;    x = Inline_func (x);    return x;}

This is the simplest use of inline, either gcc or other inline -enabled compilers can compile and pass.

Inline_func () is a function (inline ), and since it is a function, it should be allowed to join the function declaration. We made minor changes to the foo.c to see the results.

foo.cinline int Inline_func (int a), inline int inline_func (int a) {    return a + 1;} int foo (void) {    int x = 5;    x = Inline_func (x);    return x;}

Compiled, but this code actually does not conform to the C99 standard. the inline keyword in the C99 standard can only modify function definitions and cannot modify function declarations. the option to compile with gcc, such as -std=c99 , requires gcc to adhere strictly to C99 Standard), you cannot compile the pass. The following errors are reported:

In function ' foo '

Undefined reference to ' Inline_func

If you remove inlinefrom the declaration, it becomes the following code:

int Inline_func (int a); inline int inline_func (int a) {    return a + 1;} int foo (void) {    int x = 5;    x = Inline_func (x);    return x;}

is completely no problem, whether open or not C99, can be compiled through.

More often, we have an inline function to be used in multiple places, and if it is declared in other files like a normal function, for example, the following adds a function declaration and a call to MAIN.C.


int Inline_func (int a); int foo (void); int main () {    int a = foo ();    A = Inline_func (a);    printf ("A =%d", a);    return 0;}

This can be done by compiling it, but by looking at the assembly code. Only the call to Inline_func () in the foo () function is inline ,and inMain () is the traditional method of function invocation. This is not the effect we expected. the correct way should be to put the inline function in the header file defined. So that every place where it is used is really inline.


#include <stdio.h> #include "inline_func.h" int foo (void); int main () {    int a = foo ();    A = Inline_func (a);    printf ("A =%d", a);    return 0;} Foo.c#include "inline_func.h" int foo (void) {    int x = 5;    x = Inline_func (x);    return x;} Inline_func.h#ifndef inline_func_h_included#define inline_func_h_includedinline int inline_func (int a) {    return a + 1;} #endif//inline_func_h_included

This code in the last link will be an error said many times definedInline_func (). Look at the assembly code and you'll see that the compilationmain.cand thefoo.care generated when theInline_functhe code, thoughMain ()and theFooit's all inline.Inline_func (), soInline_func ()Actually, it's useless. But there are two copies .Inline_func ()The code will also cause a conflict of links. So the above code also needs to be modified. An easy way to do this is toInline_func ()insteadStaticfunction, which is to have its scope only within the compilation unit.

Inline_func.h#ifndef inline_func_h_included#define inline_func_h_includedstatic INLINE int inline_func (int a) {    return a + 1;} #endif//inline_func_h_included

After that, there's no problem at all. You can actually get here, but some people like to add a function declaration to a header file, even if the inline function is no exception.

Inline_func.h#ifndef inline_func_h_included#define inline_func_h_includedint inline_func (int a); static INLINE int Inline_func (int a) {    return a + 1;} #endif//inline_func_h_included

This compiles and then error.

Error:static declaration of ' Inline_func ' follows non-static declaration

Continue to modify, change to this appearance is no problem.

Inline_func.h#ifndef inline_func_h_included#define inline_func_h_includedstatic int inline_func (int a); static INLINE int Inline_func (int a) {    return a + 1;} #endif//inline_func_h_included

OK, that's all. the inline function knows that so much of the basics is enough.



GCC compiler support for inline functions

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.