【问题标题】:MFC-based project compilation error in Visual C++Visual C++ 中基于 MFC 的项目编译错误
【发布时间】:2013-03-23 19:12:50
【问题描述】:

我对下面的 c++ 代码有一些问题

我正在使用 VC++,现在我正在尝试编译以下基于 MFC 的项目

这是一个源文件winmfcproectum.cpp

#include "stdafx.h"
#include "winmfcproectum.h"

// ExX112_021.CppPP
// An elementary MFC program

COurApp::COurApp()
{
// TODO: add construction code here,
// Place all significant initialization in InitInstance
}

COurApp AnApplication;                     // Define an application object


// Function to create an instance of the main application window
BOOL COurApp::InitInstance()
{
  // AfxEnableControlContainer();

  // Construct a window object in the free store
  // m_pMainWnd = new COurWnd;
  // m_pMainWnd->ShowWindow(m_nCmdShow);     // ...and display it
  return TRUE;
}

这些是头文件,

stdafx.h

// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//


#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#define VC_EXTRALEAN        // Exclude rarely-used stuff from Windows headers

#include <afxwin.h>         // MFC core and standard components
#include <afxext.h>         // MFC extensions
#include <afxdisp.h>        // MFC Automation classes
#include <afxdtctl.h>       // MFC support for Internet Explorer 4 Common Controls
#ifndef _AFX_NO_AFXCMN_SUPPORT
#include <afxcmn.h>         // MFC support for Windows Common Controls
#endif // _AFX_NO_AFXCMN_SUPPORT


// C RunTime Header Files
#include <stdlib.h>
#include <malloc.h>
#include <memory.h>
#include <tchar.h>


// TODO: reference additional headers your program requires here

winmfcproectum.h

#ifndef _WINMFCPROECTUM_H
#define _WINMFCPROECTUM_H


#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#ifndef __AFXWIN_H__
    #error include 'stdafx.h' before including this file for PCH
#endif

#include "resource.h"       // main symbols



class COurWnd : public CFrameWnd
{
   public:
      // Constructor
      COurWnd()
      {
         Create(0, L"Our Dumb MFC Application");
      }
};


// Application class definition
class COurApp : public CWinApp
{
   public:
       COurApp();

   public:
      virtual BOOL InitInstance();



};


#endif

我在共享 DLL 中使用 MFC 创建了非空 Win32 项目,

resource.htargetver.h 是自动创建的,我不在这里发布它们。
因为 stdafx.h 是预编译的,所以 stdafx.cpp 也是自动创建的

现在我的问题是,看起来 COurApp 类在 winmfcproectum.cpp 中是不可见的,尽管我已经包含了 winmfcproectum.h,因此,如果我评论那些构造函数和函数实现,即 COurApp::COurApp()COurApp::InitInstance(),以及它们之间的变量声明,一切都编译得很好

这是编译输出:

Compiling...
winmfcproectum.cpp
Linking...
winmfcproectum.obj : error LNK2019: unresolved external symbol "public: __thiscall CWinApp::CWinApp(wchar_t const *)" (??0CWinApp@@QAE@PB_W@Z) referenced in function "public: __thiscall COurApp::COurApp(void)" (??0COurApp@@QAE@XZ)
winmfcproectum.obj : error LNK2001: unresolved external symbol "public: virtual class CDocument * __thiscall CWinApp::OpenDocumentFile(wchar_t const *)" (?OpenDocumentFile@CWinApp@@UAEPAVCDocument@@PB_W@Z)
winmfcproectum.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall CWinApp::AddToRecentFileList(wchar_t const *)" (?AddToRecentFileList@CWinApp@@UAEXPB_W@Z)
winmfcproectum.obj : error LNK2001: unresolved external symbol "public: virtual int __thiscall CWinApp::DoMessageBox(wchar_t const *,unsigned int,unsigned int)" (?DoMessageBox@CWinApp@@UAEHPB_WII@Z)
winmfcproectum.obj : error LNK2001: unresolved external symbol "public: virtual int __thiscall CWinApp::OnDDECommand(wchar_t *)" (?OnDDECommand@CWinApp@@UAEHPA_W@Z)
C:\Documents and Settings\Mango\My Documents\Visual Studio 2008\Projects\winmfcproectum\Debug\winmfcproectum.exe : fatal error LNK1120: 5 unresolved externals

【问题讨论】:

    标签: c++ windows visual-studio-2008 visual-c++ mfc


    【解决方案1】:

    正如链接器所说,它无法从 MFC 中找到类,如果你真的链接到正确的 MFC 库,你应该仔细检查你的项目设置。

    【讨论】:

    • 我的系统上只有一个 MFC 库并且它工作正常,因为我已经用它测试了另一个 mfc 项目。正确指定了 mfc 包含/库的路径,不需要其他依赖项,创建项目以在共享 DLL 中使用 MFC。还有什么需要检查的?
    【解决方案2】:

    啊哈,现在我明白了。我的项目和工作项目之间的唯一区别是,正常工作的项目在其“字符集”选项字段中具有“多字节字符集”,而我的项目具有“unicode 字符集”。因此,以上所有这些错误都是针对 unicode 字符集进行编译的结果。

    有趣的是,即使您不将任何字符串传递给任何函数,也无法编译为 unicode 字符集,尽管例如here,他们说如果我们要编译为 unicode 字符集,我们可以在前缀带有 L 或通用 _T() 宏的字符串。至少,我在我的 VC++ 上试过这个,但没有成功。

    【讨论】:

    • 你也看过msdn.microsoft.com/en-us/library/wsdfs47e(v=vs.80).aspx吗?同样,链接器抱怨要找到每个方法的版本,女巫采用 wchar 而不是 char
    • 嗯,他们说 MFC 动态链接库的 unicode 版本是 mfc80u.libmfcs80u.lib,但我的库是 mfc42u.libmfcs42u.lib。这就是我无法为 unicode charset 编译的原因吗?
    • 对于 VS 8,您甚至应该在安装中找到 mfc90xxx.lib?!见msdn.microsoft.com/en-us/library/8kche8ah%28v=vs.90%29.aspx
    • 哈哈,其实我用的是VC++ 2008 Express版,它不支持MFC,所以我不得不下载winDDK并使用它的MFC42库。我仍然可以成功编译多字节字符集。
    猜你喜欢
    • 1970-01-01
    • 2018-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-20
    • 1970-01-01
    • 2011-09-28
    相关资源
    最近更新 更多