MSVC、Mingw与动态链接库

MSVC、Mingw与动态链接库

调用约定与函数名
  • 采用__cdecl__stdcall调用约定的函数的内部表示都有一些命名修饰。比如在MSVC 和 MinGW GCC里,__cdecl函数有一个下划线前缀,__stdcall函数不仅有一个下划线前缀而且还跟有一个@开头的参数列表字节数后缀。所以double __cdecl sin(double)内部表示为_sindouble __stdcall sin(double)内部表示为_sin@8

Calling Convention

Internal*

MSVC DLL (w/ DEF)

MSVC DLL (dllexport)

DMC DLL

MinGW DLL

BCC DLL

__stdcall

_Function@n

Function

_Function@n

_Function@n

Function@n

Function

__cdecl

_Function

Function

Function

Function

Function

_Function

导出

在Windows下,要导出的函数前需要加

__declspec(dllexport)

__declspec(dllexport) int __cdecl add(int a, int b);相关文件
  • dll .lib .h .def
  • .dll 是主角
  • .lib 是导出库
  • .def 控制

.def 文件,可以指定序号、别名。对 stdcall 修饰的,可以导出 stdcallFunction 或 _stdcallFunction@8 两种形式

LIBRARY testdll.dll
EXPORTS
cdeclFunction @1
_stdcallFunction@8 @2
aliasName = cdeclFunction @3编译

1. 最简单形式

cl /LD 1.c /oout.dll
gcc 1.c -shared -o out.dll

2. 加上文件.def 后

cl /LD 1.c 1.def /oout.dll
gcc 1.c 1.def -shared -oout.dll

3. 链接器

msvc的link

link /out:out.dll /dll /implib:out.lib /def:1.def 1.obj

mingw的ld

--add-stdcall-alias 导出的元素中是否包含@nn
--kill-at 去除导出元素中的@nn
--out-implib <file> 生成导入库
--output-def <file> 为生成的DLL产生对应的.DEF文件

题外:和windows比起来,linux 下动态库的似乎就简单太多了

gcc -Wall -fpic -shared 1.c -o hello.so参考

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值