当前位置:网站首页>Why__ Inline usually needs static

Why__ Inline usually needs static

2022-06-10 16:22:00 Li Jian Joey

Why? __INLINE Generally, we need to add static
Here's why :
Developers can't decide whether a function is inlined , Developers only have the right to make suggestions , Only the compiler has the right to decide .
below , Let's take a look at a quilt static inline Decorated non inline function :

static __inline int Fake_StaticInline_Add(int n1,int n2,int n3,int n4,int n5)
{
/ Just to get a few more instructions /

n1++;n1++;n1++;n1++;n1++;n1++;n1++;n1++;n1++;n1++;n1++;
return (n1+n2+n3+n4+n5);;

}
This function we put in main.c in , And in main Function is called like this :i = Fake_StaticInline_Add(6,6,6,6,6);. Now let's look at his disassembly code :

MOVS r0,#5

    MOV      r3,r0
    MOV      r2,r0
    MOV      r1,r0
    STR      r0,[sp,#0]
    BL       Fake_StaticInline_Add
    MOV      r4,r0
    NOP

Isn't surprise , No surprise ? This function is actually called , It is not inlined to where it is called . that , Why didn't this function become an inline function ?
I infer that I wrote too many instructions in this function , The compiler determines if it becomes an inline function , It takes up a lot of space , So don't compile it as an inline function .
But do you remember when we put Inline_Add Where is the function ? In a header file . Just imagine , If this Inline_Add Without being compiled as an inline function , By include To multiple source files , It is bound to lead to the problem of repeated definition of functions . therefore , We need to add another keyword static, To avoid this problem .

原网站

版权声明
本文为[Li Jian Joey]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203021029387019.html