Visual Studio 2005 + MFC:链接器错误,即使导出也无法与构造函数链接
我有两个MFC项目,一个exe和一个dll。 exe引用了dll。我通过从 exe 项目中提取一些类来创建 dll,这是我的起点。
dll 现在可以正常构建,但 exe 无法与 dll 类之一的构造函数链接。我尝试对整个类进行 __declspec(dllexport) 操作,但这给了我太多警告,所以我改为对所有公共成员进行 __declspec(dllexport) 操作。这解决了除构造函数之外的大多数链接错误。
错误(MsgBoxTest 是 exe,CustomMessageBoxDlg 是 dll):
MsgBoxTestDlg.obj : error LNK2019: unresolved external symbol "public: __thiscall CMessageBoxDialog::CMessageBoxDialog(class CWnd *,class ATL::CStringT<char,class StrTraitMFC<char,class ATL::ChTraitsCRT<char> > >,class ATL::CStringT<char,class StrTraitMFC<char,class ATL::ChTraitsCRT<char> > >,unsigned int,class CPoint,unsigned int)" (??0CMessageBoxDialog@@QAE@PAVCWnd@@V?$CStringT@DV?$StrTraitMFC@DV?$ChTraitsCRT@D@ATL@@@@@ATL@@1IVCPoint@@I@Z) referenced in function "private: void __thiscall CMsgBoxTestDlg::OnDisplayMessageBox(void)" (?OnDisplayMessageBox@CMsgBoxTestDlg@@AAEXXZ)
1>Debug\MsgBoxTest.exe : fatal error LNK1120: 1 unresolved externals
构造函数声明(已重载):
// Constructor of the class for direct providing of the message strings.
__declspec(dllexport) CMessageBoxDialog ( CWnd* pParent, CString strMessage,
CString strTitle = _T(""), UINT nStyle = MB_OK, CPoint initialPosition = CPoint(0,0), UINT nHelp = 0 );
// Constructor of the class for loading the strings from the resources.
__declspec(dllexport) CMessageBoxDialog ( CWnd* pParent, UINT nMessageID, UINT nTitleID = 0,
UINT nStyle = MB_OK, CPoint initialPosition = CPoint(0,0), UINT nHelp = 0 );
错误引用的构造函数用法:
//this is a CMsgBoxTestDlg, m_strMessage and m_strTitle are CStrings, nStyle is an UINT, initialPosition is a CPoint
CMessageBoxDialog dlgMessageBox(this, m_strMessage, m_strTitle, nStyle, initialPosition);
我尝试了 Clean + Build,但没有雪茄
编辑:该类使用 DECLARE_DYNAMIC和 IMPLMENT_DYNAMIC 宏,并扩展 CDialog
I have two MFC projects, an exe and a dll. The exe references the dll. I created the dll by extracting some classes from the exe project, which was my starting point.
The dll builds ok now, but the exe can't link with the constructor of one of the dll's classes. I tried __declspec(dllexport)'ing the whole class, but that gave me too many warnings, so I __declspec(dllexport)'ed all its public members instead. That solved most of the link errors, except for the constructor.
Error (MsgBoxTest is the exe, CustomMessageBoxDlg is the dll):
MsgBoxTestDlg.obj : error LNK2019: unresolved external symbol "public: __thiscall CMessageBoxDialog::CMessageBoxDialog(class CWnd *,class ATL::CStringT<char,class StrTraitMFC<char,class ATL::ChTraitsCRT<char> > >,class ATL::CStringT<char,class StrTraitMFC<char,class ATL::ChTraitsCRT<char> > >,unsigned int,class CPoint,unsigned int)" (??0CMessageBoxDialog@@QAE@PAVCWnd@@V?$CStringT@DV?$StrTraitMFC@DV?$ChTraitsCRT@D@ATL@@@@@ATL@@1IVCPoint@@I@Z) referenced in function "private: void __thiscall CMsgBoxTestDlg::OnDisplayMessageBox(void)" (?OnDisplayMessageBox@CMsgBoxTestDlg@@AAEXXZ)
1>Debug\MsgBoxTest.exe : fatal error LNK1120: 1 unresolved externals
Constructor declaration (it's overloaded):
// Constructor of the class for direct providing of the message strings.
__declspec(dllexport) CMessageBoxDialog ( CWnd* pParent, CString strMessage,
CString strTitle = _T(""), UINT nStyle = MB_OK, CPoint initialPosition = CPoint(0,0), UINT nHelp = 0 );
// Constructor of the class for loading the strings from the resources.
__declspec(dllexport) CMessageBoxDialog ( CWnd* pParent, UINT nMessageID, UINT nTitleID = 0,
UINT nStyle = MB_OK, CPoint initialPosition = CPoint(0,0), UINT nHelp = 0 );
Constructor usage referenced by error:
//this is a CMsgBoxTestDlg, m_strMessage and m_strTitle are CStrings, nStyle is an UINT, initialPosition is a CPoint
CMessageBoxDialog dlgMessageBox(this, m_strMessage, m_strTitle, nStyle, initialPosition);
I tried a Clean + Build, but no cigar
Edit: The class uses the DECLARE_DYNAMIC and IMPLEMENT_DYNAMIC macros, and extends CDialog
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决了!看来 CString 会解析为不同的类型,具体取决于您在项目选项(常规 -> 字符集)中使用的是 Unicode 还是多字节字符。我的 dll 使用 Unicode,而我的 exe 使用多字节。将 dll 更改为使用 MB,它构建得很好。
Solved it! It seems that CString resolves to different types depending on whether you're using Unicode or Multi-Byte characters in your project options (General->Character Set). My dll was using Unicode and my exe was using Multi-Byte. Changed the dll to use MB and it built fine.