Gre*_*ill 37
该inline属性只是编译器的一个提示,它应该尝试内联您的函数.仍然可以获取函数的地址,在这种情况下,编译器还需要发出非内联版本.
例如:
#include <stdio.h>
inline void f() {
printf("hello\n");
}
int main() {
f();
void (*g)() = f;
g();
}
Run Code Online (Sandbox Code Playgroud)
上面的代码打印hello两次.
我的gcc编译器(with -O)发出如下代码:
_main:
pushl %ebp
movl %esp, %ebp
pushl %ebx
subl $20, %esp
call ___i686.get_pc_thunk.bx
"L00000000002$pb":
leal LC0-"L00000000002$pb"(%ebx), %eax
movl %eax, (%esp)
call L_puts$stub ; inlined call to f()
call L__Z1fv$stub ; function pointer call to f() (g is optimised away)
movl $0, %eax
addl $20, %esp
popl %ebx
popl %ebp
ret
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,首先是调用puts()然后调用L__Z1fv()(这是错误的名称f()).