minGW下dll编写与调用

在网上查了一下,并且自己试了一试,minGW下编写与调用dll的过程如下(基于eclipse环境)

1.在eclipse下新建一个C工程,在它的属性里面检查Tool chain editor里面current toolchain是不是no toolchain,还有C/C++ Build下要将build command 改成mingw32-make ,我在这个地方卡了好久,编绎会提示exec failed,可能默认用的是make吧

2.然后编写代码,三个文件
hello.c   //用于调用生成的dll
#include <stdio.h>
#include "dll.h"

int main(int argc,char *argv[])
{
    int a=1;
    int b=5;
    printf("%d+%d=%d/n",a,b,hello(a,b));
    return 0;
}

dll.h  //dll的声明,当编绎时加入BUILD_DLL宏时,将EXPORT 设置为 __declspec,使GCC生成dll库,否则是调动dll

#ifdef BUILD_DLL
/* dll export */
#define EXPORT __declspec(dllexport)
#else
/*dll import */
#define EXPORT __declspec(dllimport)
#endif

EXPORT int hello(int a,int b);

dll.c   //dll的实现

#include "dll.h"

EXPORT int hello(int a,int b)
{
    return a+b;
}

3.编写makefile,如下,里面所用到的gcc参数包括 -shared 表明生成动态链接库,
 -Wl,--out-implib,libmessage.a 这个参数主要是生成.a这个文件,具体信息可以看最后的那篇文章


all: hello.exe

clean:
    del *.o
    del message.dll
    del hello.exe

hello.exe: hello.o message.dll
    gcc -o hello.exe hello.o -L./ -lmessage

hello.o: hello.c
    gcc -c hello.c

message.dll: dll.o
    gcc -shared -o message.dll dll.o -Wl,--out-implib,libmessage.a
dll.o: dll.c dll.h
    gcc -c -DBUILD_DLL dll.c

好了,可以运行build all,应该可以看到hello.exe及message.dll了吧
运行hello,可以看到结果

可以参看这篇更详细的例子
http://www.mingw.org/MinGWiki/index.php/sample%20DLL?PHPSESSID=abce11154a632520eb...
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值