c - 该函数是否被视为内联?

标签 c gcc inline declaration

#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/

相关文章:

仅当超出范围时 C 缓冲区才会溢出

c - 为什么 FT_Read() 在子进程中失败,但在父进程中有效?

c++ - 使用RowMajor和ColMajor数据排列的矩阵行求和的奇怪性能差异

c++ - 按值返回内联函数

html - CSS IE6 向右浮动

c - 运行程序时出现段错误

c - 如何将程序发送到后台并停止执行?

c++ - 是否可以关闭 gcc 的自动并行化?

c++ - 无法正确连接到 mariadb

objective-c - Objective-C 中的静态、外部和内联