C生成DLL

用 vc 6.0 下的cl.exe 和 link.exe工具,请读下文: 
声明:下面这篇文章不是我写的,源自:一个叫,有容乃大 的博客 

如何手工编写动态链接库(windows dll) 

1.本例介绍在命令行(Console)环境下制作dll的方法 
2.读者动手前,请确保在windows中安装有编译、链接工具和必要的函数库文件。 
3.本例使用C语言实现. 
4.本例中使用路径均为我机器上的绝对路径,读者需根据实际情况调整。 

工具要求: 

Microsoft的 编译器 cl.exe 
MIcrosoft链接器link.exe 


dll制作步骤: 
1.编写dll函数实现 源代码 hello.c 


#include 

int say_hello(char* name) 

printf( "hello %s\n ", name); 
return 1; 



2.编写dll函数输出定义文件hello.def. 

LIBRARY hello 
EXPORTS 
say_hello @1 


3.编译dll源码,生成dll, lib文件

3.1 新建命令行窗口 
3.2 设置PATH ¦ INCLUDE ¦ LIB 3个环境变量. 

SET PATH=K:\vcnet\vc7\bin;%PATH% 
SET INCLUDE=K:\vcnet\vc7\include;%INCLUDE% 
SET LIB=K:\vsnet\Vc7\lib;%LIB% 

3.3 编译hello.c 

cd K:\Source\dllsample (hello.c和hello.def所在目录) 
cl /c hello.c 

3.4 链接hello.obj,生成hello.dll,hello.lib两个文件. 

link /def:hello.def /dll hello.obj 

4.测试dll函数. 

4.1 编写测试代码 test.c 

extern int say_hello(char* name); 
int main(int argc,char** argv) 

say_hello( "robbie "); 
return 0; 


4.2 编译测试代码test.c 

cl /c test.c 

4.3 链接test.obj和 hello.lib,生成可执行文件test.exe 

link test.obj hello.lib 

4.4 运行test.exe,屏幕输出: 

hello robbie 

至此,一个dll构造完毕. 


下面是我自己的一点补充: 
如果要在c++下,或者win32 mfc下使用标准c写的dll,必须把上面的声明 
extern int say_hello(char* name);改成:extern "C " int say_hello(char* name);
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值