GCC快速调用函数定义
好的,我可以通过使用 __attribute__((fastcall))
声明它,将函数作为 fastcall CC 来调用。 如何将函数本身定义为快速调用?
就像,我有调用者代码:
// 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
如果我像我一样在 func.c 中声明它在 caller.c 中,它会以另一种方式抱怨:
error: previous declaration of ‘func’ was here
Okay, so I can call function as fastcall CC, by declaring it with __attribute__((fastcall))
.
How do I define the function itself as fastcall?
Like, I have caller code:
// caller.c
unsigned long func(unsigned long i) __attribute__((fastcall));
void caller() {
register unsigned long i = 0;
while ( i != 0xFFFFFFD0 ) {
i = func(i);
}
}
And the function:
// func.c
unsigned long func(unsigned long i) {
return i++;
}
In this code, func()
is being compiled as cdecl, it extracts i from stack, not from ecx(this is i386).
If I write unsigned long func(unsigned long i) __attribute__((fastcall));
in func.c it just won't compile, saying
error: expected ‘,’ or ‘;’ before ‘{’ token
If I declare it in func.c the same way I did in caller.c, it will complain the other way:
error: previous declaration of ‘func’ was here
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
属性必须在声明中应用,而不是在定义中应用。
尝试:
执行此操作的标准方法是将声明放在标头中,并使两个源文件都包含该标头
Attributes must be applied in the declaration, not in the definition.
Try:
The standard way to do this is to put the declaration in a header and have both source files include the header
问题是您在属性后面放置的分号。你需要
The problem is the semicolon you put after the attribute. You need