简体   繁体   中英

MFC-based project compilation error in Visual C++

I have some problems with the following c++ code

I'm using VC++ and now I'm trying to compile the following MFC-based project

This is a source file 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 : 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

#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,

resource.h and targetver.h were created automatically, I don't post them here.
Because stdafx.h is precompiled, stdafx.cpp was automatically created as well

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

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. So, all those errors above, are the result of compiling for unicode character set.

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. At least, I tried this on my VC++ but no success.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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