最近要做一个实践,调用一个封装好的DLL ,去实现双向链表某个功能。然后我就建了2个项目DBLDll和USEDll。
DBLDll目前下的代码文件DBLDll.h如下
// The following ifdef block is the standard way of creating macros which make exporting
// from a DLL simpler. All files within this DLL are compiled with the DBLDLL_EXPORTS
// symbol defined on the command line. this symbol should not be defined on any project
// that uses this DLL. This way any other project whose source files include this file see
// DBLDLL_API functions as being imported from a DLL, whereas this DLL sees symbols
// defined with this macro as being exported.
#ifdef DBLDLL_EXPORTS
#define DBLDLL_API __declspec(dllexport)
#else
#define DBLDLL_API __declspec(dllimport)
#endif// This class is exported from the DBLDll.dll
class DBLDLL_API CDBLDll {
public:
CDBLDll(void);
// TODO: add your methods here.
};extern DBLDLL_API int nDBLDll;DBLDLL_API int fnDBLDll(void);////////////////////////////////////////
int N=0;
struct NODE
{
char name[20];
NODE *llink, *rlink;
};DBLDLL_API NODE* Create(int n);
DBLDLL_API NODE* Search(NODE* h, const char* name);
DBLDLL_API void Insert(NODE* p);
DBLDLL_API void Delete(NODE* p);
DBLDLL_API void Display(NODE* h);
DBLDLL_API void Sort(NODE* h);DBLDll.cpp文件如下;// DBLDll.cpp : Defines the entry point for the DLL application.
//#include "stdafx.h"
#include "DBLDll.h"
#include <iostream>
using namespace std;#ifdef _MANAGED
#pragma managed(push, off)
#endifBOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}#ifdef _MANAGED
#pragma managed(pop)
#endif// This is an example of an exported variable
DBLDLL_API int nDBLDll=0;// This is an example of an exported function.
DBLDLL_API int fnDBLDll(void)
{
return 42;
}// This is the constructor of a class that has been exported.
// see DBLDll.h for the class definition
CDBLDll::CDBLDll()
{
return;
}////////////////////////////////DBLDLL_API NODE* Create(int n)
{
N=n;
NODE *h, *p, *s; //h:头结点,p:下一结点,s:当前结点
int i;
if((h = new NODE) == NULL)
{
cout<<"分配内存失败"<<endl;
}
h->name[0] = 0;
h->llink = NULL;
h->rlink = NULL;
p = h;
for(i=0; i != n; ++i)
{
if((s = new NODE) == NULL)
{
cout<<"分配内存失败"<<endl;
}
p->rlink = s;
cout<<"请输入第"<<i+1<<"个字符串:";
cin>>s->name;
s->llink = p;
s->rlink = NULL;
p = s;
}
s->rlink = h;
h->llink = s;
return h;
}DBLDLL_API NODE* Search(NODE* h, const char* name)
{
NODE *p = h->rlink;
for(int i=0; i != N;++i)
{
if(strcmp(p->name,name) == 0)
{
return p;
}
p = p->rlink;
}
return p;
}/*插入*/
DBLDLL_API void Insert(NODE* p)
{
NODE *s = new NODE;
cout<<"请输入要插入的字符串:";
cin>>s->name;
char ch;
cout<<"请输入y或n来决定是否插入该字符"<<endl;
cin>>ch;
if(ch=='y')
{
NODE *r = p->rlink; // 结点示意:p->s->r(s为插入的结点)
p->rlink = s;
s->llink = p;
r->llink = s;
s->rlink = r;
++N;
}
else if(ch=='n')
{
cout<<"你已经取消了此次插入;"<<endl;
}
else
{
cout<<"你输入的选择符有误;"<<endl;
}
}/*删除*/
DBLDLL_API void Delete(NODE* p)
{
NODE *l = p->llink; // 结点示意:l->p->r(p为要删除的结点)
NODE *r = p->rlink;
l->rlink = r;
r->llink = l;
delete p;
--N;
}/*显示*/
DBLDLL_API void Display(NODE* h)
{
NODE *p;
p = h->rlink;
for(int i=0; i != N; ++i)
{
cout<<"第"<<i+1<<"个字符是:"<<p->name<<endl;
p = p->rlink;
}
}/*排序*/
DBLDLL_API void Sort(NODE* h)
{
NODE *s;
s = h->rlink;
while(s != h)
{
NODE* temp = s->rlink;
while(temp != h)
{
if(strcmp(s->name,temp->name) > 0)
{
char info[20];
strcpy(info,s->name);
strcpy(s->name,temp->name);
strcpy(temp->name,info);
}
temp = temp->rlink;
}
s = s->rlink;
}
}然后我把生产的dll文件和lib文件 拷贝到了USEDll的项目下, 分别是dll文件放到了debug目录下,lib文件放到了外面,和.cpp文件放在一个文件夹。
USEDll下的DBLDll.h是从DBLDll拷贝过来了一份,代码和上面的一样,
main.cpp代码如下:#include "DBLDll.h"
#include <iostream>
#include <cstdlib>
using namespace std;double Count=0;
int main()
{
//判断输入节点数的字符是否合法;
while(true)
{
cout<<"请输入你要创建的节点个数:"<<endl;
if( ( !(cin>>Count) )||( ((int)Count) != Count ) )
{
cerr<<"error"<<endl;
cin.clear();
cin.sync();
}
else if(Count == 0)
{
cerr<<"error"<<endl;
cin.clear();
cin.sync();
}
else
{
break;
}
} NODE *head, *pSearch;
int number =(int) Count;
char strName[20];
head = Create(number);
cout<<endl;
cout<<"你创建的结构如下:"<<endl;
Display(head);
cout<<endl;
while(true)
{
cout<<"请根据提示输入数字"<<endl
<<"1:将节点排序;"<<endl
<<"2:将节点输出;"<<endl
<<"3:查找并插入;"<<endl
<<"4:查找并删除;"<<endl
<<"0:退出该链表:"<<endl;
float i;
if( (cin>>i) &&((int)i == i) )
{
int j = (int)i;
switch(j)
{
//排序
case 1:
Sort(head);
cout<<"排序已经实现;"<<endl<<endl;
break; //输出
case 2:
Display(head);
cout<<endl;
break; //查找并插入
case 3:
cout<<"请输入你要查找的字符:";
cin>>strName;
pSearch = Search(head, strName); if(pSearch != head)
{
cout<<"你所要查找的字符是:"<<pSearch->name<<endl;
cout<<endl;
Insert(pSearch);
cout<<"插入后的结果如下:"<<endl;
Display(head);
cout<<endl;
}
else
{
cout<<"你要查找的字符串不存在。"<<endl<<endl;
}
break; //查找并删除
case 4:
while(true)
{
cout<<"输入你要删除的字符:";
cin>>strName;
pSearch = Search(head, strName);
if(pSearch != head)
{
char ch;
cout<<"输入“y”决定删除,"
<<"输入“n”取消删除"<<endl;
cin>>ch;
if(ch == 'y')
{
Delete(pSearch);
cout<<"删除后的结果如下:"<<endl;
Display(head);
cout<<endl;
break;
}
else if(ch == 'n')
{
break;
}
else
{
cout<<"你输入的选择有误"<<endl;
break;
}
}
else
{
cout<<"当前链表为空,或你输入的字符不存在,不能删除;"<<endl<<endl;
break;
}
}
break;
//退出整个程序
case 0:
{
cout<<"你已成功退出本程序。"<<endl<<endl;
exit(0);
} default:
{
cout<<"你输入的字符是非法的,请重新输入:"<<endl;
}
}//对应switch的括号
}
else
{
cerr<<"error"<<endl;
cin.clear();
cin.sync();
}
} //对应while(true)的括号
return 0;
}
然后出了如下的错误。。
1>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) void __cdecl Delete(struct NODE *)" (__imp_?Delete@@YAXPAUNODE@@@Z) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) void __cdecl Insert(struct NODE *)" (__imp_?Insert@@YAXPAUNODE@@@Z) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) struct NODE * __cdecl Search(struct NODE *,char const *)" (__imp_?Search@@YAPAUNODE@@PAU1@PBD@Z) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) void __cdecl Sort(struct NODE *)" (__imp_?Sort@@YAXPAUNODE@@@Z) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) void __cdecl Display(struct NODE *)" (__imp_?Display@@YAXPAUNODE@@@Z) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) struct NODE * __cdecl Create(int)" (__imp_?Create@@YAPAUNODE@@H@Z) referenced in function _main
1>D:\My Documents\Visual Studio 2005\Projects\UseDll\Debug\UseDll.exe : fatal error LNK1120: 6 unresolved externals我试着去改VS2005的一些设置,但是也出现了新的错误。不能解决。
因为之前我在 vs2008上也是这样看书做过一次。不知道这个错误该怎么解决!
help!~~~~~~
DBLDll目前下的代码文件DBLDll.h如下
// The following ifdef block is the standard way of creating macros which make exporting
// from a DLL simpler. All files within this DLL are compiled with the DBLDLL_EXPORTS
// symbol defined on the command line. this symbol should not be defined on any project
// that uses this DLL. This way any other project whose source files include this file see
// DBLDLL_API functions as being imported from a DLL, whereas this DLL sees symbols
// defined with this macro as being exported.
#ifdef DBLDLL_EXPORTS
#define DBLDLL_API __declspec(dllexport)
#else
#define DBLDLL_API __declspec(dllimport)
#endif// This class is exported from the DBLDll.dll
class DBLDLL_API CDBLDll {
public:
CDBLDll(void);
// TODO: add your methods here.
};extern DBLDLL_API int nDBLDll;DBLDLL_API int fnDBLDll(void);////////////////////////////////////////
int N=0;
struct NODE
{
char name[20];
NODE *llink, *rlink;
};DBLDLL_API NODE* Create(int n);
DBLDLL_API NODE* Search(NODE* h, const char* name);
DBLDLL_API void Insert(NODE* p);
DBLDLL_API void Delete(NODE* p);
DBLDLL_API void Display(NODE* h);
DBLDLL_API void Sort(NODE* h);DBLDll.cpp文件如下;// DBLDll.cpp : Defines the entry point for the DLL application.
//#include "stdafx.h"
#include "DBLDll.h"
#include <iostream>
using namespace std;#ifdef _MANAGED
#pragma managed(push, off)
#endifBOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}#ifdef _MANAGED
#pragma managed(pop)
#endif// This is an example of an exported variable
DBLDLL_API int nDBLDll=0;// This is an example of an exported function.
DBLDLL_API int fnDBLDll(void)
{
return 42;
}// This is the constructor of a class that has been exported.
// see DBLDll.h for the class definition
CDBLDll::CDBLDll()
{
return;
}////////////////////////////////DBLDLL_API NODE* Create(int n)
{
N=n;
NODE *h, *p, *s; //h:头结点,p:下一结点,s:当前结点
int i;
if((h = new NODE) == NULL)
{
cout<<"分配内存失败"<<endl;
}
h->name[0] = 0;
h->llink = NULL;
h->rlink = NULL;
p = h;
for(i=0; i != n; ++i)
{
if((s = new NODE) == NULL)
{
cout<<"分配内存失败"<<endl;
}
p->rlink = s;
cout<<"请输入第"<<i+1<<"个字符串:";
cin>>s->name;
s->llink = p;
s->rlink = NULL;
p = s;
}
s->rlink = h;
h->llink = s;
return h;
}DBLDLL_API NODE* Search(NODE* h, const char* name)
{
NODE *p = h->rlink;
for(int i=0; i != N;++i)
{
if(strcmp(p->name,name) == 0)
{
return p;
}
p = p->rlink;
}
return p;
}/*插入*/
DBLDLL_API void Insert(NODE* p)
{
NODE *s = new NODE;
cout<<"请输入要插入的字符串:";
cin>>s->name;
char ch;
cout<<"请输入y或n来决定是否插入该字符"<<endl;
cin>>ch;
if(ch=='y')
{
NODE *r = p->rlink; // 结点示意:p->s->r(s为插入的结点)
p->rlink = s;
s->llink = p;
r->llink = s;
s->rlink = r;
++N;
}
else if(ch=='n')
{
cout<<"你已经取消了此次插入;"<<endl;
}
else
{
cout<<"你输入的选择符有误;"<<endl;
}
}/*删除*/
DBLDLL_API void Delete(NODE* p)
{
NODE *l = p->llink; // 结点示意:l->p->r(p为要删除的结点)
NODE *r = p->rlink;
l->rlink = r;
r->llink = l;
delete p;
--N;
}/*显示*/
DBLDLL_API void Display(NODE* h)
{
NODE *p;
p = h->rlink;
for(int i=0; i != N; ++i)
{
cout<<"第"<<i+1<<"个字符是:"<<p->name<<endl;
p = p->rlink;
}
}/*排序*/
DBLDLL_API void Sort(NODE* h)
{
NODE *s;
s = h->rlink;
while(s != h)
{
NODE* temp = s->rlink;
while(temp != h)
{
if(strcmp(s->name,temp->name) > 0)
{
char info[20];
strcpy(info,s->name);
strcpy(s->name,temp->name);
strcpy(temp->name,info);
}
temp = temp->rlink;
}
s = s->rlink;
}
}然后我把生产的dll文件和lib文件 拷贝到了USEDll的项目下, 分别是dll文件放到了debug目录下,lib文件放到了外面,和.cpp文件放在一个文件夹。
USEDll下的DBLDll.h是从DBLDll拷贝过来了一份,代码和上面的一样,
main.cpp代码如下:#include "DBLDll.h"
#include <iostream>
#include <cstdlib>
using namespace std;double Count=0;
int main()
{
//判断输入节点数的字符是否合法;
while(true)
{
cout<<"请输入你要创建的节点个数:"<<endl;
if( ( !(cin>>Count) )||( ((int)Count) != Count ) )
{
cerr<<"error"<<endl;
cin.clear();
cin.sync();
}
else if(Count == 0)
{
cerr<<"error"<<endl;
cin.clear();
cin.sync();
}
else
{
break;
}
} NODE *head, *pSearch;
int number =(int) Count;
char strName[20];
head = Create(number);
cout<<endl;
cout<<"你创建的结构如下:"<<endl;
Display(head);
cout<<endl;
while(true)
{
cout<<"请根据提示输入数字"<<endl
<<"1:将节点排序;"<<endl
<<"2:将节点输出;"<<endl
<<"3:查找并插入;"<<endl
<<"4:查找并删除;"<<endl
<<"0:退出该链表:"<<endl;
float i;
if( (cin>>i) &&((int)i == i) )
{
int j = (int)i;
switch(j)
{
//排序
case 1:
Sort(head);
cout<<"排序已经实现;"<<endl<<endl;
break; //输出
case 2:
Display(head);
cout<<endl;
break; //查找并插入
case 3:
cout<<"请输入你要查找的字符:";
cin>>strName;
pSearch = Search(head, strName); if(pSearch != head)
{
cout<<"你所要查找的字符是:"<<pSearch->name<<endl;
cout<<endl;
Insert(pSearch);
cout<<"插入后的结果如下:"<<endl;
Display(head);
cout<<endl;
}
else
{
cout<<"你要查找的字符串不存在。"<<endl<<endl;
}
break; //查找并删除
case 4:
while(true)
{
cout<<"输入你要删除的字符:";
cin>>strName;
pSearch = Search(head, strName);
if(pSearch != head)
{
char ch;
cout<<"输入“y”决定删除,"
<<"输入“n”取消删除"<<endl;
cin>>ch;
if(ch == 'y')
{
Delete(pSearch);
cout<<"删除后的结果如下:"<<endl;
Display(head);
cout<<endl;
break;
}
else if(ch == 'n')
{
break;
}
else
{
cout<<"你输入的选择有误"<<endl;
break;
}
}
else
{
cout<<"当前链表为空,或你输入的字符不存在,不能删除;"<<endl<<endl;
break;
}
}
break;
//退出整个程序
case 0:
{
cout<<"你已成功退出本程序。"<<endl<<endl;
exit(0);
} default:
{
cout<<"你输入的字符是非法的,请重新输入:"<<endl;
}
}//对应switch的括号
}
else
{
cerr<<"error"<<endl;
cin.clear();
cin.sync();
}
} //对应while(true)的括号
return 0;
}
然后出了如下的错误。。
1>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) void __cdecl Delete(struct NODE *)" (__imp_?Delete@@YAXPAUNODE@@@Z) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) void __cdecl Insert(struct NODE *)" (__imp_?Insert@@YAXPAUNODE@@@Z) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) struct NODE * __cdecl Search(struct NODE *,char const *)" (__imp_?Search@@YAPAUNODE@@PAU1@PBD@Z) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) void __cdecl Sort(struct NODE *)" (__imp_?Sort@@YAXPAUNODE@@@Z) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) void __cdecl Display(struct NODE *)" (__imp_?Display@@YAXPAUNODE@@@Z) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) struct NODE * __cdecl Create(int)" (__imp_?Create@@YAPAUNODE@@H@Z) referenced in function _main
1>D:\My Documents\Visual Studio 2005\Projects\UseDll\Debug\UseDll.exe : fatal error LNK1120: 6 unresolved externals我试着去改VS2005的一些设置,但是也出现了新的错误。不能解决。
因为之前我在 vs2008上也是这样看书做过一次。不知道这个错误该怎么解决!
help!~~~~~~
#pragma comment(lib, "dbldll.lib")