c 语言 dll编译与使用

来源:互联网 发布:斯隆女士原型知乎 编辑:程序博客网 时间:2024/06/05 04:45

首先,上一条博客。

http://blog.sina.com.cn/s/blog_62c832270101d92u.html

根据博客中的步骤,可以使用C语言编写dll并于main函数中调用。

作死的时候来了:




win8,mingw32,4.9.3 gcc

在.h中写一段代码:

/*test.h*/
#include <stdio.h>void hello(){printf("Hello from DLL\n");}
编译,生成test.dll文件。


生成完成。

编写main函数:

/*main.c*/
#include <stdio.h>int main(){    hello();return 0;}
执行,报错:


百思不得其解。

尝试向文件中添加:

/*test.h*/
#include <stdio.h>__declspec(dllexport) void hello(){printf("Hello from DLL\n");}
/*main.c*/
#include <stdio.h>__declspec(dllimport) void hello();int main(){    hello();return 0;}
编译,仍然报错:



查看相关文档,似乎.h文件与.c文件并没有什么太大区别。。

http://www.cnblogs.com/laojie4321/archive/2012/03/30/2425015.html


老实的将.h改回.c,可以运行。


再次进行以下实验:

/*zuo.h*/
#include <stdio.h>int main(){printf("zuo si\n");return 0;}
编译,生成zuo.exe:



运行zuo.exe:

运行报错。


总结:

.c与.h文件存在差别,原因不明。。


0 0