Microsoft KB Archive/134980

= PRB: C4251 & C4275 Compiler Warnings While Compiling an AFXDLL =

Article ID: 134980

Article Last Modified on 10/24/2003

-

APPLIES TO

 Microsoft Foundation Class Library 4.2, when used with:  Microsoft Visual C++ 2.0 Professional Edition

 Microsoft Visual C++ 2.1

 Microsoft Visual C++ 2.2 

-

<div class="notice_section">

This article was previously published under Q134980

<div class="symptoms_section">

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: : 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.

<div class="cause_section">

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.

<div class="resolution_section">

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: <pre class="fixed_text"> #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.

<div class="status_section">

STATUS
This behavior is by design.

<div class="moreinformation_section">

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; ... };

<div class="references_section">