locked
what does warning C4251 class needs to have dll interface to be used by clients of class mean? RRS feed

  • Question

  • what does C4251 class needs to have a dll interface warning mean?

    Warning 1 warning C4251: 'reg::RegPartBase::mPartText' : class 'std::basic_string<_Elem,_Traits,_Alloc>' needs to have dll-interface to be used by clients of class 'reg::RegPartBase' c:\srcpp\libfilepreview\x64\debug\RegPart.h 32 1 LibFilePreview

    I have a class in a DLL. In the project that uses the DLL there is a header file that declares the class with __declspec(dllimport).

    class __declspec(dllimport) RegPartBase
    {
    
    public:
    	wstring mPartText ;
    	PartType mPartType ;
    ...
    } ;
    

    Saturday, June 2, 2012 7:19 PM

Answers

  •  

    Hello

     

    I think this issue caused by the STL template does not export in your DLL. Did you try to use your DLL in another project. As far as I know, if your another project has include the STL and builds the project with DLL, this warning will disappear. Because there is another implement of STL.

     

    On the other hand, you can disable this warning in this way:

    #pragma warning(push)

    #pragma warning(disable:4251)

    //your declarations that cause 4251

    #pragma warning(pop)

     

    Best regards,

    Jesse


    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by Steve Richter Monday, June 4, 2012 10:54 PM
    Monday, June 4, 2012 6:06 AM

All replies

  • Egad. Do you realize that experienced C++ programmers strongly advice against exposing C++ structures and STL containers across DLL boundaries? In order to even hope to get this to work, you will need to ensure that both the DLL and the client code use exactly the same C++ compiler and exactly the same compiler switch settings. You can't mix DEBUG and RELEASE compilations even if using the same compiler. This basically defeats the purpose of developing a DLL in a first place.

    In our organization, this would be shot down immediately before the ink dried on the whiteboard. This is not a problem worth solving.


    • Edited by Brian Muth Saturday, June 2, 2012 7:53 PM spelling
    Saturday, June 2, 2012 7:53 PM
  • Egad. Do you realize that experienced C++ programmers strongly advice against exposing C++ structures and STL containers across DLL boundaries?

    What do experienced programmers advise? Do I have to add a set of .cpp source files as existing items to every project?

    thanks,

    Sunday, June 3, 2012 12:50 AM
  •  

    Hello

     

    I think this issue caused by the STL template does not export in your DLL. Did you try to use your DLL in another project. As far as I know, if your another project has include the STL and builds the project with DLL, this warning will disappear. Because there is another implement of STL.

     

    On the other hand, you can disable this warning in this way:

    #pragma warning(push)

    #pragma warning(disable:4251)

    //your declarations that cause 4251

    #pragma warning(pop)

     

    Best regards,

    Jesse


    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by Steve Richter Monday, June 4, 2012 10:54 PM
    Monday, June 4, 2012 6:06 AM
  • Hi

    I know it's rather late in the day, but I was wondering if the suppression of this warning can also be effected using these directive on non-Windows platforms.

    Thanks.

    Victor

    Friday, October 26, 2018 6:19 PM
  • @Brian: it's perfectly fine to use STL or new / delete across DLL boundary especially when they are only used internally in your application and you build everything yourself.

    Only when DLL's are to be used independently for any client I would restrict to a c-API only or COM. When using C++ it is recommended to stick to one compiler, use the DLL c-runtime and don't mix debug and release.


    • Edited by gast128 Thursday, March 12, 2020 11:55 AM
    Thursday, March 12, 2020 11:54 AM