PRB: C4251 & C4275 Compiler Warnings While Compiling an AFXDLL

Last reviewed: July 31, 1997
Article ID: Q134980
2.00 2.10 2.20 WINDOWS NT kbprg kberrmsg kbprb

The information in this article applies to:

  • The Microsoft Foundation Classes (MFC) included with: Microsoft Visual C++, 32-bit Edition, versions 2.0, 2.1, 2.2

SYMPTOMS

The Compiler issues the following warning for a class derived from one of the MFC classes:

   warning C4275: non dll-interface class <XXX> used as base for
   dll-interface class <YYY>

A related warning is issued when an object of some MFC class is embedded as a data member of a user-defined class:

   warning C4251: <identifier> : class <XXX> needs to have dll-interface
   to be used by clients of class <YYY>

See the "Sample Code" section of this article an example of a class declaration that generates these warnings.

This problem no longer occurs in vc 4.0 or later.

CAUSE

The DLL version of MFC does not export its classes by declaring them with "class __declspec(dllexport)." Instead, it exports its classes through entries in the EXPORTS section of its module definition (.def) file.

Because the compiler has only the header files to work with, it cannot determine whether the MFC classes were actually exported, and therefore it issues the warning.

RESOLUTION

Because the MFC classes are in fact exported, these warnings can be safely ignored. As shown in the DLLHUSK sample, you can disable these warnings by using the following pragma statements:

  #pragma warning(disable: 4275)
  #pragma warning(disable: 4251)

On the other hand, if the warnings refer to some user-defined class rather an MFC class, you should ensure that the class is exported before disabling these warnings.

STATUS

This behavior is by design.

MORE INFORMATION

Sample Code to Reproduce Behavior

/* Compile options needed: None
*/

// The following class declaration causes a C4275 warning
// because the CObject class is not also declared with
// __declspec(dllexport).

class __declspec(dllexport) CMyClass : public CObject {
  ...
  // The following data member causes a C4251 warning,
  // because the CString class is not declared with
  // __declspec(dllexport).

  CString m_strName;
  ...
};

REFERENCES

For more information, please see MFC TechNote 33 for a discussion of the reasons behind the decision to export MFC's classes in the module definition file rather than in the class declaration.


Additional reference words: 2.00 2.10 2.20 3.00 3.10 3.20
KBCategory: kbprg kberrmsg kbprb
KBSubcategory: MfcDLL
Keywords : MfcDLL kberrmsg kbprb kbprg
Technology : kbMfc
Version : 2.00 2.10 2.20
Platform : NT WINDOWS


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: July 31, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.