开发者

Visual Studio 2005 + MFC: Linker error, cannot link with constructor even if exported

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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜