Gcc Inline 函数

1.      编译时使用–Winline,让gcc对标志成inline但不能被展开的函数给出警告信息。

2.      Inline是一种优化,只在有优化的编译选项中起作用,若编译时没使用-O选项,任何函数都不会被内联。

3.      Inline 不能被展开的常见情况

(1)    使用了可变参数;

(2)    非局部goto语句;

(3)    对inline函数地址的引用,递归是特殊的一种;

(4)    Inline函数定义之前的调用语句是不会被替换展开的。

4.      常见使用情况

4.1  inline

When an inline function is not static, then the compiler must assume that there may be calls from other source files; since a global symbol can be defined only once in any program, the function must not be defined in the other source files,so the calls therein cannot be integrated. Therefore, a non-static inline functionis always compiled on its own in the usual fashion

4.2  static inline

如果对这个函数的所有调用都可以直接在调用函数中展开,而且此函数的地址没有被引用过。gcc不会生成此函数的汇编代码,除非你指定-fkeep-inline-functions选项。

4.3  extern inline

If you specify both inline and extern in the functiondefinition, then the definition is used only for inlining. In no case is thefunction compiled on its own, not even if you refer to its address explicitly.Such an address becomes an external reference, as if you had only declared thefunction, and had not defined it.
    函数由inline和extern同时指定,这个函数定义只会在调用中展开,即使你明确引用它的地址,gcc也不会生成此函数的汇编代码。这个地址会被当成一个外部引用,就好像你只是声明了这个函数,却没有定义它。
This combination of inlineand extern has almost the effect of a macro. The way to use it is to puta function definition in a header file with these keywords, and put anothercopy of the definition (lacking inline and extern) in a library file. Thedefinition in the header file will cause most calls to the function to be inlined.If any uses of the function remain, they will refer to the single copy in thelibrary.

此种情况下,能被展开的就被展开,不能被展开的如地址引用,就引用库中的另外的拷贝。作用就是让一个库函数在能够被内联的时候尽量被内联使用。


<注>本文源于作者按自己的思路对网上和相关书籍资料的整理归纳

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值