环境:MinGW【windows下的GCC】, Notepad++
一:
随便写个C源文件test.c里面只有一个函数,如下:
编译test.c为test.dll,使用命令:gcc -shared -Wl,--kill-at -o test.dll test.c
二:
再写调用测试C程序caldll.c,代码如下:
编译这个程序,运行得到输出结果result: 8
gcc/bin目录设置为环境变量,这样方便命令窗口下使用
效果如图:
[img]http://dl.iteye.com/upload/attachment/0068/9499/99123350-2630-37a6-a8df-543c468de732.png[/img]
一:
随便写个C源文件test.c里面只有一个函数,如下:
int add(int x,int y)
{
return x+y;
}
编译test.c为test.dll,使用命令:gcc -shared -Wl,--kill-at -o test.dll test.c
二:
再写调用测试C程序caldll.c,代码如下:
#include <windows.h>
#include <stdio.h>
#include <winbase.h>
typedef int (*MYPROC)(int ,int);
void main()
{
int res = 0;
HINSTANCE LibHandle;
MYPROC ProcAdd;
LibHandle = LoadLibrary("test.dll"); /*载入dll*/
ProcAdd = (MYPROC)GetProcAddress(LibHandle, "add"); /*获取函数sysbuf的地址*/
res =(ProcAdd) (4,4); /*ProcAdd就相当于函数 show123*/
printf("result: %i ",res);
}
编译这个程序,运行得到输出结果result: 8
gcc/bin目录设置为环境变量,这样方便命令窗口下使用
效果如图:
[img]http://dl.iteye.com/upload/attachment/0068/9499/99123350-2630-37a6-a8df-543c468de732.png[/img]