Warning C4251 when building a DLL that exports a class containing an ATL::CString member

房东的猫 提交于 2019-11-29 05:53:53

问题


I am converting an ATL-based static library to a DLL and am getting the following warning on any exported classes that use the ATL CString class (found in atlstr.h):

warning C4251: 'Foo::str_' : class 'ATL::CStringT' needs to have dll-interface to be used by clients of class 'Foo'

I am correctly declaring the Foo class as exported via __declspec(dllexport). Is this a warning I can safely ignore or am I doing something wrong? The DLL project settings are set to dynamically link with ATL, but this doesn't seem to make any difference.

For example:

#ifdef DLLTEST_EXPORTS
#define DLLTEST_API __declspec(dllexport)
#else
#define DLLTEST_API __declspec(dllimport)
#endif

// This class is exported from the DLLTest.dll
class DLLTEST_API Foo
{
public:
 Foo();
 CString str_; // WARNING C4251 HERE
};

All clients of this DLL will also be using ATL.


回答1:


This thread gives what I consider a better answer, by Doug Harrison (VC++ MVP):

[This warning is] emitted when you use a non-dllexported class X in a dllexported class Y. What's so bad about that? Well, suppose Y has an inline function y_f that calls a function x_f belonging to X that is not also inline. If y_f is inlined inside some client that doesn't statically link X, the link will fail, because x_f won't be found.




回答2:


This Microsoft page helped me with it.

How to export an instantiation of a Standard Template Library (STL) class and a class that contains a data member that is an STL object




回答3:


Here is a thread with a good discussion of this.

In short, the compiler is warning you that, in effect, your exported class does not seperate the interface from the implementation. If the members in question are not accessible to the clients, make them private and #pragma away the warning for that member/class. If the members are accessible and used by clients, then you will need to provide indirect access to the members through accessors and mutators.




回答4:


I usually get this warning when I make the silly mistake of building the DLL with runtime library Single/Multithreaded instead of Single/MultithreadedDLL. You might want to check that in your project settings.



来源:https://stackoverflow.com/questions/2132747/warning-c4251-when-building-a-dll-that-exports-a-class-containing-an-atlcstrin

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!