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