简体   繁体   English

Visual C ++中基于MFC的项目编译错误

[英]MFC-based project compilation error in Visual C++

I have some problems with the following c++ code 我的以下c ++代码有一些问题

I'm using VC++ and now I'm trying to compile the following MFC-based project 我正在使用VC ++,现在我正在尝试编译以下基于MFC的项目

This is a source file winmfcproectum.cpp 这是源文件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;
}

and these are header files, 这些是头文件

stdafx.h 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

and winmfcproectum.h 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

I've created non-empty Win32 project with use of MFC in a Shared DLL, 我在共享DLL中使用MFC创建了非空的Win32项目,

resource.h and targetver.h were created automatically, I don't post them here. resource.htargetver.h是自动创建的,我不在此处发布。
Because stdafx.h is precompiled, stdafx.cpp was automatically created as well 由于stdafx.h是预编译的,因此stdafx.cpp也会自动创建

Now my problem is, it looks like COurApp class is invisible inside winmfcproectum.cpp , although I've included winmfcproectum.h , so if I comment those constructor & function implementations, ie COurApp::COurApp() and COurApp::InitInstance() , and also a variable declaration between them, everything compiles well 现在我的问题是,尽管我已经包含winmfcproectum.h ,但看起来在winmfcproectum.cpp中 COurApp类是不可见的,所以如果我评论那些构造函数和函数实现,即COurApp :: COurApp()COurApp :: InitInstance() ,以及它们之间的变量声明,一切都编译良好

Here is the compilation output: 这是编译输出:

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

正如链接器所说的,它无法从MFC中找到类,因此,如果您确实链接了正确的MFC库,则应仔细检查项目设置。

Aha, now I see. 啊哈,现在我明白了。 The only difference between my project and the working one is that, the one that works correctly, has 'multi-byte character set' in its 'character set' option field, while my project has 'unicode character set' instead. 我的项目与正在运行的项目之间的唯一区别是,可以正常工作的项目在其“字符集”选项字段中具有“多字节字符集”,而我的项目则具有“ unicode字符集”。 So, all those errors above, are the result of compiling for unicode character set. 因此,以上所有这些错误都是编译unicode字符集的结果。

Interestingly, it is not possible to compile for unicode charset even if you don't pass any string to any function, although for example here , they say that if we want to compile for unicode charset, we may prefix the string with L or generic _T() macro. 有趣的是,即使您不向任何函数传递任何字符串,也无法为unicode字符集进行编译,尽管例如, 在这里 ,他们说如果我们要为unicode字符集进行编译,则可以在字符串前加上L或泛型_T()宏。 At least, I tried this on my VC++ but no success. 至少,我在VC ++上尝试过此方法,但没有成功。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM