在网上查了一下,并且自己试了一试,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...
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...