动态链接库导出类报错_declspec(dllexport)error LNK2019: unresolved external symbol

ec06cumt 2012-06-27 07:06:42
我用的是VS2010,想导出类,从而使用其中方法,方法也是public类型,在MFC中button事件中,引用类方法,却发现编译不通过,
我知道是链接的原因,但是网上搜索了半天都没有解决,有说是字符编码,在properties中改成No-set,没有用。
现在的字符编码都是Use Multi-Byte Character Set。
被导出类的字符编码也是Use Multi-Byte Character Set。还是没有用。



//DllPlus.h
#ifdef Dll_API
#else
#define Dll_API _declspec(dllimport)
#endif

Dll_API int add(int a,int b);
Dll_API int subtract(int a,int b);

class Dll_API PointObject
{
public:
void output(int x,int y);
};

//DllPlus.cpp

#define Dll_API _declspec(dllexport)
#include "DllPlus.h"
#include <Windows.h>
#include <iostream>

int add(int x,int y)
{
return x + y;
}

int subtract(int x,int y)
{
return x - y;
}
//在窗口上输出一字符串。
void output(int x,int y)
{
HWND hwnd = GetForegroundWindow();
HDC hdc = GetDC(hwnd);
char buf[20];
memset(buf,0,20);
sprintf(buf,"x=%d,y=%d",x,y);
TextOut(hdc,0,0,buf,strlen(buf));
ReleaseDC(hwnd,hdc);
}


//在MFC中的button事件中的代码:


#include "stdafx.h"
#include "DllTest.h"
#include "DllTestDlg.h"
#include "afxdialogex.h"
#include "..\..\DllPlus\DllPlus\DllPlus.h"

...


void CDllTestDlg::OnBnClickedButtonClass()
{
PointObject pt;
pt.output(3,5);
}




错误信息:

1>DllTestDlg.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: void __thiscall PointObject::output(int,int)" (__imp_?output@PointObject@@QAEXHH@Z) referenced in function "public: void __thiscall CDllTestDlg::OnBnClickedButtonClass(void)" (?OnBnClickedButtonClass@CDllTestDlg@@QAEXXZ)
1>E:\MFCWorkSpace\DllTest\Debug\DllTest.exe : fatal error LNK1120: 1 unresolved externals

不知道我的引用正不正确。要是不正确应该怎样引用啊?谢谢啊。
...全文
968 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
wdchensicong 2012-07-16
  • 打赏
  • 举报
回复
请问楼主解决了没有啊,我也有同样的问题~~~~
LittleCommit 2012-06-28
  • 打赏
  • 举报
回复
1、你有没有在CDllTestDlg的模块的属性里添加对lib文件的连接?
2、要将dll文件拷贝到与exe文件同一目录下。
无趣 2012-06-28
  • 打赏
  • 举报
回复
所以当CPP先编译后,Dll_API已经被定义了,所以Dll_API就应该是_declspec(dllexport)
当调用时#define Dll_API _declspec(dllexport)其实已经没有必要了。

==============
那是预处理啊,还调用?!在你的DLL工程属性中,加上Dll_API宏定义,exe不要加。
_free 2012-06-28
  • 打赏
  • 举报
回复
class AFX_EXT_CLASS CMyClass{
// <body of class>
};

ec06cumt 2012-06-27
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]
#ifdef Dll_API
#else
#define Dll_API _declspec(dllimport)
#endif

应改成

#ifdef Dll_API
#define Dll_API _declspec(dllexport)
#else
#define Dll_API _declspec(dllimport)
#endif
[/Quote]

你这写法也对,但是还是没有解决问题。
其实编译器,一般先编译Cpp文件,头文件不参与编译。
所以当CPP先编译后,Dll_API已经被定义了,所以Dll_API就应该是_declspec(dllexport)
当调用时#define Dll_API _declspec(dllexport)其实已经没有必要了。

为什么没人回答?导出类,到底该怎样导出??求解答。。
无趣 2012-06-27
  • 打赏
  • 举报
回复
#ifdef Dll_API
#else
#define Dll_API _declspec(dllimport)
#endif

应改成

#ifdef Dll_API
#define Dll_API _declspec(dllexport)
#else
#define Dll_API _declspec(dllimport)
#endif

15,473

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 进程/线程/DLL
社区管理员
  • 进程/线程/DLL社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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