关于DLL的初级问题,望指教!!

bastenf 2006-08-25 03:50:31
我建了个DLL,不知道对不对,代码如下:
__declspec (dllexport) int add (int,int);
__declspec (dllexport) int add (int a,int b)
{return (a + b);}

然后我就在另一个控制台中调用了
#include <iostream>
__declspec( dllimport ) int add(int a ,int b );
int main()
{
std::cout << Function1(5,10) << std::endl;
system("pause");
return 0;
}
编译器说 [Linker error] undefined reference to `_imp___Z10HelloWorldii'

我用的是 DEVCPP,望知道的,告诉一下!!谢谢!!
...全文
141 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
cnhgj 2006-08-25
  • 打赏
  • 举报
回复
加上extern "C"
SammyLan 2006-08-25
  • 打赏
  • 举报
回复
没有包含lib文件
#pragma comment(lib,".....")
codearts 2006-08-25
  • 打赏
  • 举报
回复
#include <iostream>
__declspec (dllimport) int add (int a,int b);
int main()
{
std::cout << add(5,10) << std::endl;
system("pause");
}

这里有#include "ddl.h"了吗?有将.lib文件加入到工程中了吗?

另外,dll.h中:
__declspec (dllexport) int add (int a,int b);
最好用
extern "C" __declspec (dllexport) int add (int a,int b);



bastenf 2006-08-25
  • 打赏
  • 举报
回复
重新实现了一下,望 bohlee(我心澎湃) 给我看看

文件 dll.h
#ifndef _DLL_H_
#define _DLL_H_

#if BUILDING_DLL
# define DLLIMPORT __declspec (dllexport)
#else /* Not BUILDING_DLL */
# define DLLIMPORT __declspec (dllimport)
#endif /* Not BUILDING_DLL */

__declspec (dllexport) int add (int a,int b);

class DLLIMPORT DllClass
{
public:
DllClass();
virtual ~DllClass(void);

private:

};
#endif /* _DLL_H_ */

文件dll.cpp
/* Replace "dll.h" with the name of your header */
#include "dll.h"
#include <windows.h>

__declspec (dllexport) int add (int a,int b)
{
return (a + b);
}

DllClass::DllClass()
{

}


DllClass::~DllClass ()
{

}


BOOL APIENTRY DllMain (HINSTANCE hInst /* Library instance handle. */ ,
DWORD reason /* Reason this function is being called. */ ,
LPVOID reserved /* Not used. */ )
{
switch (reason)
{
case DLL_PROCESS_ATTACH:
break;

case DLL_PROCESS_DETACH:
break;

case DLL_THREAD_ATTACH:
break;

case DLL_THREAD_DETACH:
break;
}

/* Returns TRUE on success, FALSE on failure */
return TRUE;
}


文件main.cpp
#include <iostream>
__declspec (dllimport) int add (int a,int b);
int main()
{
std::cout << add(5,10) << std::endl;
system("pause");
}

报的错是 [Linker error] undefined reference to `_imp___Z3addii'
bohlee 2006-08-25
  • 打赏
  • 举报
回复
_imp___Z10HelloWorldii没有定义??
把程序帖全,估计你的定义错了
bastenf 2006-08-25
  • 打赏
  • 举报
回复
上面那个写错了
我建了个DLL,不知道对不对,代码如下:
__declspec (dllexport) int add (int,int);
__declspec (dllexport) int add (int a,int b)
{return (a + b);}

然后我就在另一个控制台中调用了
#include <iostream>
__declspec( dllimport ) int add(int a ,int b );
int main()
{
std::cout << add(5,10) << std::endl;
system("pause");
return 0;
}
编译器说 [Linker error] undefined reference to `_imp___Z10HelloWorldii'

我用的是 DEVCPP,望知道的,告诉一下!!谢谢!!

64,677

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧