Say I have this small function in a source file

static void foo() {}

and I build an optimized version of my binary yet I don't want this function inlined (for optimization purposes). is there a macro I can add in a source code to prevent the inlining?

推荐答案

You want the gcc-specific noinline attribute.

This function attribute prevents a function from being considered for inlining. If the function does not have side-effects, there are optimizations other than inlining that causes function calls to be optimized away, although the function call is live. To keep such calls from being optimized away, put asm ("");

Use it like this:

void __attribute__ ((noinline)) foo() 
{
  ...
}

C++相关问答推荐

为什么写入系统调用打印的字符数不正确?

如何判断宏参数是否为C语言中的整型文字

ZED for SDL上的C语言服务器

如何在IF语句中正确使用0.0

将fget()与strcMP()一起使用不是正确的比较

我的程序在收到SIGUSR1信号以从PAUSE()继续程序时总是崩溃()

轮询libusb_pollfd struct 列表的正确方式是什么?

如何使用C for Linux和Windows的标准输入与gdb/mi进行通信?

编译器如何处理具有更复杂值的枚举?

为什么未初始化的 struct 的数组从另一个数组获取值?

链表删除 node 错误

与外部SPI闪存通信时是否应禁用中断?

如何解释数组中的*(ptr)和*(ptr+2)?

在NASM中链接Linux共享库时出错-';将R_ X86_64_;foo';

是什么阻止编译器优化手写的 memcmp()?

是否可以在多字 C 类型中的任何位置混合存储和类型限定符?

如何转义包含指令中的字符?

如何让 unlinkat(dir_fd, ".", AT_REMOVEDIR) 工作?

C 程序调用 malloc 导致总线错误?

为什么 C 风格的类会产生额外的指令?