一文理解|GCC内联函数(inline)
我们都知道
inline
是用来声明内联函数的关键字,不过在嵌入式应用中,采用 GCC 编译器,往往有2种声明内联函数关键字:inline
和 __attribute __((always_inline))
,它们有什么区别呢?对编译器
inline
: 建议编译器内联,实际是否内联由编译器决定(根据优化等级);__attribute __((always_inline))
:强制编译器将函数当做内联函数;
意义:
优势:
内联函数拥有更快的调用速度(减少了函数调用); 可以实现把函数代码集成到调用者处; 有利于编译器优化代码,减少代码量(取决于具体情况);
缺点:
可能导致代码量膨胀(取决于具体情况);
用法:
inline
__inline__是ISO C90的写法
写法1
inlineintfoo(int *a)
{
return (*a) ++;
}
写法2
inlinefoo(int *a);
intfoo(int *a)
{
return (*a) ++;
}
__attribute __((always_inline))
或者__attribute__((__gnu_inline__))
适用于 gcc 编译器(参考 gcc 参考手册)
inlinevoidfoo(int a) __attribute __((always_inline));
或者
__attribute __((always_inline)) voidfoo(int a);
static
,extern
与 inline
static inline
表示只会是当前模块才会调用这个内联函数;non-static inline
表示可能有其他模块会调用这个内联函数,别的模块也不能定义这个函数。如果函数定义中,同时指定
inline
和 extern
,该定义仅用于内联。函数也不会独自编译(因为已经被认为可能会被其他模块调用,会在每个调用处内联、编译)与内联相关的所有函数属性
何时内联?
关于函数属性
always_inline
,一般 inline
只有特定优化等级才会内联。而对于声明 always_inline
的函数,会一直保持内联,而且不论有没有指定优化等级。注意 declare 和 define/definition 的区别:declare 表示声明,definition 表示定义(分配存储空间);
gnu_inline
在gcc 4.1.3以后版本才可用, __GNUC_GNU_INLINE__
or __GNUC_STDC_INLINE__
任一已定义才可用。该属性应该用于同时声明为
inline
的函数。如果一个函数声明为 extern
,该定义仅用于 inline
。用 gnu_inline
修饰的函数,不会编译成独立函数,意思是一定会编译成内联函数。可以用在这种情况,头文件声明加上该函数属性,而库文件(或.c文件)包含一个副本,但无需
extern
,头文件的 gnu_inline
也会引起内联。如果一个函数既不是
extern
,也不是 static
,函数被编译成一个独立函数,也会尽可能被内联。不会内联的情况
关于
inline
(内联) 正如 gcc
手册描述的那样(别的编译器需要查看具体的手册),gcc 不会真正内联任何函数,如果使用了 "-fno-inline"
选项,或者如果使用了 "-O0"
(优化等级)。当然除此之外,gcc还有其他多种情况不会真正内联函数。"-Winline"
选项阻止内联
阻止inline,可以使用函数属性
__noinline__
。即使使用了建议inline关键字修饰,__noinline__
也会阻止真正内联。__attribute__((__noinline__)) inlinestaticintf(int a, int b)
{
return (a + b);
}
main()
{
// ..
f(1,2) ; // 该处函数不会真正内联
}
如何判断是否内联?
可以通过查看生成的汇编代码,比如
.lst
文件,assembly文件。函数调用往往包含入栈、出栈的操作(汇编指令),而内联函数没有这些。原文:https://www.cnblogs.com/fortunely/p/13430735.html
阅读原文 最新评论
推荐文章
作者最新文章
你可能感兴趣的文章
Copyright Disclaimer: The copyright of contents (including texts, images, videos and audios) posted above belong to the User who shared or the third-party website which the User shared from. If you found your copyright have been infringed, please send a DMCA takedown notice to [email protected]. For more detail of the source, please click on the button "Read Original Post" below. For other communications, please send to [email protected].
版权声明:以上内容为用户推荐收藏至CareerEngine平台,其内容(含文字、图片、视频、音频等)及知识版权均属用户或用户转发自的第三方网站,如涉嫌侵权,请通知[email protected]进行信息删除。如需查看信息来源,请点击“查看原文”。如需洽谈其它事宜,请联系[email protected]。
版权声明:以上内容为用户推荐收藏至CareerEngine平台,其内容(含文字、图片、视频、音频等)及知识版权均属用户或用户转发自的第三方网站,如涉嫌侵权,请通知[email protected]进行信息删除。如需查看信息来源,请点击“查看原文”。如需洽谈其它事宜,请联系[email protected]。