c – g没有内联函数

c – g没有内联函数,第1张

概述有这样的代码: int fun1(){ return 2 + 3;}inline int fun2(){ return 4 + 5;}int main(){ int a = fun1(); int b = fun2(); return 0;} 和相应的汇编代码: .file "prog47.cpp" .text.globl _Z4 有这样的代码:

int fun1(){   return 2 + 3;}inline int fun2(){     return 4 + 5;}int main(){    int a = fun1();    int b = fun2();    return 0;}

和相应的汇编代码:

.file   "prog47.cpp"    .text.globl _Z4fun1v    .type   _Z4fun1v,@function_Z4fun1v:.LFB0:    .cfi_startproc    .cfi_personality 0x0,__gxx_personality_v0    pushl   %ebp    .cfi_def_cfa_offset 8    movl    %esp,%ebp    .cfi_offset 5,-8    .cfi_def_cfa_register 5    movl    ,%eax    popl    %ebp    ret    .cfi_endproc.LFE0:    .size   _Z4fun1v,.-_Z4fun1v    .section    .text._Z4fun2v,"axG",@progbits,_Z4fun2v,comdat    .weak   _Z4fun2v    .type   _Z4fun2v,@function_Z4fun2v:.LFB1:    .cfi_startproc    .cfi_personality 0x0,-8    .cfi_def_cfa_register 5    movl    ,%eax    popl    %ebp    ret    .cfi_endproc.LFE1:    .size   _Z4fun2v,.-_Z4fun2v    .text.globl main    .type   main,@functionmain:.LFB2:    .cfi_startproc    .cfi_personality 0x0,-8    .cfi_def_cfa_register 5    andl    $-16,%esp    subl    ,%esp    call    _Z4fun1v    movl    %eax,12(%esp)    call    _Z4fun2v        # why fun2 is called?    movl    %eax,8(%esp)    movl    
inline int fun2()  __attribute__((always_inline));inline int fun2() {     return 4 + 5;}
,%eax leave ret .cfi_endproc.LFE2: .size main,.-main .section .note.GNU-stack,"",@progbits

为什么函数fun2没有内联并像普通函数一样被调用?我已经读过内联关键字只是提示编译器,它不需要内联函数,但是fun2的定义非常简单,因此可以内联.如何强制g内联函数?

解决方法 GCC有一个强制内联的属性:always_inline

将使其适用于任何优化设置.

总结

以上是内存溢出为你收集整理的c – g没有内联函数全部内容,希望文章能够帮你解决c – g没有内联函数所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1224011.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-05
下一篇2022-06-05

发表评论

登录后才能评论

评论列表(0条)

    保存