c - GCC fastcall 函数定义

标签 c gcc

好的,所以我可以调用函数作为fastcall CC,方法是用__attribute__((fastcall))声明它。 如何将函数本身定义为 fastcall?

例如,我有来电代码:

// caller.c

unsigned long func(unsigned long i) __attribute__((fastcall));

void caller() {
    register unsigned long i = 0;
    while ( i != 0xFFFFFFD0 ) {
        i = func(i);
    }
}

还有函数:

// func.c

unsigned long func(unsigned long i) {
    return i++;
}

在这段代码中,func() 被编译为 cdecl,它从堆栈中提取 i,而不是从 ecx(这是 i386)。

如果我在 func.c 中编写 unsigned long func(unsigned long i) __attribute__((fastcall)); 它就不会编译,说

error: expected ‘,’ or ‘;’ before ‘{’ token

如果我像在 caller.c 中那样在 func.c 中声明它,它会以另一种方式提示:

error: previous declaration of ‘func’ was here

最佳答案

属性必须在声明中应用,而不是在定义中。

尝试:

__attribute__((fastcall)) unsigned long func(unsigned long i) ;
__attribute__((fastcall)) unsigned long func(unsigned long i) {
    return i++;
}

这样做的标准方法是将声明放在标题中,并让两个源文件都包含标题

关于c - GCC fastcall 函数定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7760736/

相关文章:

c - 海合会: "warning: assignment from incompatible pointer type"

c - 在 ARM 中使用 'restrict' 关键字会改进我的代码吗?

gcc - ARM Cortex M4(Tiva C 系列 TM4C123GH6PM)启动时堆栈指针不加载

c++ - dlclose() 不调用全局对象的析构函数

c - Linux 设备驱动访问控制

c - 新的 C23 #embed 指令的目的是什么?

c - 将链表作为参数传递时出错

c++ - 函数指针中的参数列表

C++ 代码比它的 C 等效代码慢?

c - 头文件中的全局变量