#include <stdio.h>
static void test()
{
printf("is this function considered for inline?");
}
int main()
{
test(); // definition does not have inline keyword. But declaration at the bottom (which is never used) has a inline keyword.
}
inline static void test(); // definition WITH inline keyword
编译器会将 test()
视为附加了 inline
关键字吗?
编辑:抱歉,我的意思是在最后的声明中有一个内联关键字!
最佳答案
使用 gcc -S 进行编译,看看汇编器是什么样子的,例如这是一个 -O2 - 所以我们可以看到 test() 已经消失并内联了。您应该有一个由 gcc 生成的 .s 文件
main:
.LFB12:
.cfi_startproc
movl $.LC0, %edi
xorl %eax, %eax
jmp printf
.cfi_endproc
.LFE12:
未经优化,我们可以看到对 test() 的调用
main:
.LFB1:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
movl $0, %eax
call test
popq %rbp
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE1:
关于c - 该函数是否被视为内联?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8161223/