您的位置:首页 > 其它

使用MinGW GCC 创建拥有 __stdcall 方式的 DLL动态链接库

2010-05-30 18:32 281 查看
被这个问题困扰了两天,才在网上找到解决方法

案例:test.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#if defined(TEST_EXPORT)
#define TEST_API(type)	__declspec(dllexport) type __stdcall
#else
#define TEST_API(type)	__declspec(dllimport) type __stdcall
#endif

TEST_API(void) hello(const char *name)
{
printf("Hello %s!/r/n", name);
}


编译步骤:
1、预编译成object file,获得test.o
gcc -c -DTEST_EXPORT test.c

2、链接object file为dll文件,并输出def文件
gcc -shared -o test.dll test.o -Wl,--kill-at -Wl,--output-def,test.def
3、根据dll文件和def文件创建lib库文件
dlltool -d test.def --dllname test.dll --output-lib test.lib --kill-at

这几步里,最关键的就是-Wl,--kill-at,它的功能就是删除所有@
输出的名称,这样就能被LoadLibrary和GetProcAddress调用,不过即使这样eclipse with CDT也无法查看接口,只有__cdcel方式的接口能被eclipse with CDT看到
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: