问题
When creating a new MFC application of the simplest kind in Visual Studio 2012, and setting it to link statically to MFC, linking fails.
Here's a gallery of 7 screenshots starting from the creation of the project via the wizard, up to the building (resulting in the linker error). No source editing was done in-between.
The error logs state:
1>------ Build started: Project: Test, Configuration: Debug Win32 ------
1> stdafx.cpp
1> TestDlg.cpp
1> Test.cpp
1> Generating Code...
1>uafxcwd.lib(afxctrlcontainer2.obj) : error LNK2005: "void __cdecl AfxRegisterMFCCtrlClasses(void)" (?AfxRegisterMFCCtrlClasses@@YAXXZ) already defined in afxnmcdd.lib(afxctrlcontainer2.obj)
1>uafxcwd.lib(afxctrlcontainer2.obj) : error LNK2005: "protected: void __thiscall CMFCControlContainer::PreUnsubclassControl(class CWnd *)" (?PreUnsubclassControl@CMFCControlContainer@@IAEXPAVCWnd@@@Z) already defined in afxnmcdd.lib(afxctrlcontainer2.obj)
1>uafxcwd.lib(afxctrlcontainer2.obj) : error LNK2005: "public: int __thiscall CMFCControlContainer::SubclassDlgControls(void)" (?SubclassDlgControls@CMFCControlContainer@@QAEHXZ) already defined in afxnmcdd.lib(afxctrlcontainer2.obj)
1>C:\Users\XXXXXXXX\Documents\Visual Studio 2012\Projects\Test\Debug\Test.exe : fatal error LNK1169: one or more multiply defined symbols found
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Is this just me? Would you have a suggestion on how to address this?
回答1:
I got the same problem when I try static link my project together.
As Michael Burr suggested, it seems fine after commented out the line of #define _AFX_NO_MFC_CONTROLS_IN_DIALOGS in stdafx.h.
回答2:
I've come across the same issue. If you decide to #define _AFX_NO_MFC_CONTROLS_IN_DIALOGS
and get rid of a lot MFC overhead you should also be sure you change the dialog base classes, method calls,etc from CDialogEx
to CDialog
.
If you don't change the background color or the image of your dialog, you will find that CDialogEx
is pretty useless anyway.
回答3:
After tested, v100 works well and v110 does not work. Try to report to MS connect as this link: http://connect.microsoft.com/VisualStudio/feedback/details/581084/linker-cant-locate-some-external-symbol-when-use-mfc-in-a-static-library
来源:https://stackoverflow.com/questions/13589518/link-errors-in-unmodified-visual-studio-2012-mfc-template-when-statically-linkin