Linux下如何指定调用约定(calling convention)

本文介绍了在Linux系统中如何指定函数调用约定,如stdcall和cdecl。在Windows下,调用约定可以通过函数名前缀来指定,而在Linux下,应使用`__attribute__((__calling_convention__))`语法。默认情况下,Linux函数遵循cdecl约定。通过示例代码展示了函数定义,并提供了GCC相关参考链接,帮助进行Windows到Linux的程序移植。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Windows下的调用约定可以是stdcall/cdecl/fastcall,这些标识加在函数名前面,如:

 

int __stdcall funca()

 

 

但在Linux下,如按照上面写法后,编译程序将导致编译错误,Linux下正确的语法如下:

int __attribute__((__stdcall__)) funca()

int __attribute__((__cdecl__)) funca()

 

 

Linux下如果函数不指定调用约定,默认的情况应该是__attribute__((__cdecl__))

 

int __attribute__((__cdecl__)) myfunc(int i, int j, int k)
{
    return i+j+k;
}

 080483d0 <myfunc>:
 80483d0:    55                       push   %ebp
 80483d1:    89 e5                    mov    %esp,%ebp
 80483d3:    8b 45 0c                 mov    0xc(%ebp),%eax
 80483d6:    03 45 08                 add    0x8(%ebp),%eax
 80483d9:    03 45 10                 add    0x10(%ebp),%eax
 80483dc:    5d                       pop    %ebp
 80483dd:    c3                       ret   
 80483de:    66 90                    xchg   %ax,%ax

 

 

 

int __attribute__((__stdcall__)) myfunc(int i, int j, int k)
{
    return i+j+k;
}

 080483d0 <myfunc>:
 80483d0:    55                       push   %ebp
 80483d1:    89 e5                    mov    %esp,%ebp
 80483d3:    8b 45 0c                 mov    0xc(%ebp),%eax
 80483d6:    03 45 08                 add    0x8(%ebp),%eax
 80483d9:    03 45 10                 add    0x10(%ebp),%eax
 80483dc:    5d                       pop    %ebp
 80483dd:    c2 0c 00                 ret    $0xc

 

GCC相关参考如下:

关于attribute扩展:

http://gcc.gnu.org/onlinedocs/gcc-4.6.0/gcc/Function-Attributes.html#Function-Attributes

 

移植Windows程序到Linux时,关于调用约定的处理:

http://stackoverflow.com/questions/3054257/is-there-stdcall-in-linux

 

调用约定的通用参考:

http://en.wikipedia.org/wiki/X86_calling_conventions

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值