minGW 编译的DLL问题

wangyang327329 2012-07-13 11:48:25
一、使用minGW编译了某个动态库,部分代码声明:

/*输入输出宏定义*/
#ifndef VMF_EXPORTS
#define VMF_API _declspec(dllimport)
#else
#define VMF_API _declspec(dllexport)
#endif

/*调用方式宏定义*/
#ifndef FUNC_INVOKE_MODE
#define VMF_INVOKE_MODE _stdcall
#else
#define VMF_INVOKE_MODE _cdel


/*导出函数声明*/

#ifdef __cplusplus
extern "C"
{
#endif

VMF_API int VMF_INVOKE_MODE VMF_Initialize(const char*);

#ifdef __cplusplus
};
#endif

使用 --kill-at 进行编译后,利用dependency查看dll导出的函数均为干净的函数名,即导出名称为VMF_Iniialize.

二、利用codeblocks新建一个console application,在向导过程中选择C++ 语言,然后将上述动态库的导出函数声明的头文件包含进来,并且链接路径均设置正确,但是在调用dll中的VMF_Initialize时,编译错误:无法链接的VMF_Initialize@4,
原因本人清楚:C++对dll中的导出函数声明进行了名在改编,导致链接错误!但目前还没有好的解决办法!

望各位大虾不吝赐教!感激不敬!
...全文
683 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
欧阳春晖 2012-07-18
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]

我生成的这个动态库的到处函数已经是纯净名称,
我的现象是在使用的过程中,将动态库的导出函数声明头包含进来,并且链接库已经设置好,但是由于选择了C++所以编译器对导出函数声明头中的函数进行名字改编,而导致链接错误。
我的问题是怎样使得这个C++测试程旭能够通过这种方式通过编译?
[/Quote]
你可以使用模块定义文件
xunxun 2012-07-16
  • 打赏
  • 举报
回复
不想带@用stdcall作甚?
fox000002 2012-07-14
  • 打赏
  • 举报
回复
1 楼直接链接的是 DLL 文件

用 implib 链接的话需要从 def 文件重新生成一下

可以看看: Build DLLs with MinGW(--kill-at)
wangyang327329 2012-07-14
  • 打赏
  • 举报
回复
我生成的这个动态库的到处函数已经是纯净名称,
我的现象是在使用的过程中,将动态库的导出函数声明头包含进来,并且链接库已经设置好,但是由于选择了C++所以编译器对导出函数声明头中的函数进行名字改编,而导致链接错误。
我的问题是怎样使得这个C++测试程旭能够通过这种方式通过编译?
薛定谔之死猫 2012-07-14
  • 打赏
  • 举报
回复
如果你搞清楚了厉害关系,可以使用mingw的工具dlltool从dll输出def或者.a等重新用于新的连接,不过按照你的方法编译是没问题,下面举例

dll源代码
#include <stdio.h>

extern "C" {
__declspec(dllexport) void hello(char *msg);
}

void hello(char *msg){
printf("Hello %s\n",msg);
}


保存为dll.cpp,编译它
g++ -shared -Wl,--kill-at,--output-def,dll.def -o dll.dll dll.cpp

这里的def文件生成与否都不影响

测试dll的代码
#include <stdio.h>

extern "C" {
__declspec(dllimport) void hello(char *msg);
}


int main(int argc, char *argv[])
{
hello("Fucking!");
return 0;
}


保存为test.cpp,编译它
g++ -L. -ldll -o test.exe test.cpp


运行test.exe输出
Hello Fucking!


用dumpbin检查导出函数名称的纯洁性
>dumpbin /exports dll.dll
Microsoft (R) COFF/PE Dumper Version 10.00.40219.01
Copyright (C) Microsoft Corporation. All rights reserved.


Dump of file dll.dll

File Type: DLL

Section contains the following exports for dll.dll

00000000 characteristics
50006CA6 time date stamp Sat Jul 14 02:44:54 2012
0.00 version
1 ordinal base
1 number of functions
1 number of names

ordinal hint RVA name

1 0 00001194 hello

Summary

1000 .CRT
1000 .bss
1000 .data
1000 .debug_abbrev
1000 .debug_aranges
1000 .debug_frame
3000 .debug_info
1000 .debug_line
1000 .debug_loc
1000 .debug_pubnames
1000 .debug_pubtypes
1000 .debug_ranges
1000 .debug_str
1000 .edata
1000 .idata
1000 .rdata
1000 .reloc
1000 .text
1000 .tls


很明显非常纯洁的~

24,854

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 工具平台和程序库
社区管理员
  • 工具平台和程序库社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧