Chinaunix首页 | 论坛 | 博客
  • 博客访问: 610851
  • 博文数量: 262
  • 博客积分: 8433
  • 博客等级: 中将
  • 技术积分: 2141
  • 用 户 组: 普通用户
  • 注册时间: 2009-05-31 09:37
文章分类

全部博文(262)

文章存档

2012年(1)

2011年(168)

2010年(92)

2009年(1)

分类: C/C++

2010-12-04 19:45:01

方法一:使用export 和 import

在VC中建立一个Console Application,建立2个文件:Dll.h 和 Dll.cpp

Dll.h

#ifdef MYLIBAPI
#else
#define MYLIBAPI extern "C" _declspec (dllimport)
#end if

MYLIBAPI int Add (int iLeft, int iRight)
MYLIBAPI int Sub (int iLeft, int iRight)

Dll.cpp

#define MYLIBAPI extern "C" _declspec (dllexport)

#include "Dll.h"

int Add (int iLeft, int iRight)
{
return iLeft + iRight ;
}

int Sub (int iLeft, int iRight)
{
return iLeft - iRight ;
}

保存文件。
在Project->setting->link 最下面加上 “/dll”, "/"之前一定要与前一项
有空格。
然后编译,就可以在debug 或 release下面找到dll 和 lib 文件了
使用的时候包含dll.h文件

方法二:使用def文件
建立一个console application, 建立2个文件dll.h 和 dll.cpp

Dll.h

int Add (int iLeft, int iRight) ;
int Sub (int iLeft, int iRight) ;

Dll.cpp

#include "Dll.h"

int Add (int iLeft, int iRight)
{
return iLeft + iRight ;
}

int Sub (int iLeft, int iRight)
{
return iLeft - iRight ;
}

然后再当前目录下面建立一个.def文件,文件名最好和要输出的dll名字一样,扩展名
为.def, 里面写上:

LIBRARY dllname.dll
EXPORTS
Add @1
Add @2
然后将这个文件添加到工程中,
在link中设置 /dll, 然后编译
在debug或release中就可以找到dll和lib了
使用的时候加上dll.h文件

阅读(603) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~