C++ 生成导出包含 ATL::CString 成员的类的 DLL 时警告 C4251
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2132747/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Warning C4251 when building a DLL that exports a class containing an ATL::CString member
提问by Rob
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 CStringclass (found in atlstr.h):
我正在将基于 ATL 的静态库转换为 DLL,并且在任何使用 ATLCString类(在 atlstr.h 中找到)的导出类上都收到以下警告:
warning C4251: 'Foo::str_' : class 'ATL::CStringT' needs to have dll-interface to be used by clients of class 'Foo'
警告 C4251:“Foo::str_”:“ATL::CStringT”类需要具有 dll 接口以供“Foo”类的客户端使用
I am correctly declaring the Fooclass 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.
我正确地将Foo类声明为通过__declspec(dllexport). 这是我可以放心忽略的警告还是我做错了什么?DLL 项目设置设置为与 ATL 动态链接,但这似乎没有任何区别。
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.
此 DLL 的所有客户端也将使用 ATL。
采纳答案by Ofek Shilon
This threadgives what I consider a better answer, by Doug Harrison (VC++ MVP):
这个帖子给出了我认为更好的答案,作者是 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.
[此警告] 当您在 dllexported 类 Y 中使用非 dllexported 类 X 时发出。这有什么不好?好吧,假设 Y 有一个内联函数 y_f,它调用一个属于 X 的函数 x_f,但它也不是内联的。如果 y_f 被内联在一些不静态链接 X 的客户端中,链接将失败,因为 x_f 不会被找到。
回答by Yochai Timmer
This Microsoft page helped me with it.
这个 Microsoft 页面帮助我解决了这个问题。
回答by John Dibling
Here is a threadwith 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 #pragmaaway 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.
简而言之,编译器警告您,实际上,您的导出类并未将接口与实现分开。如果客户无法访问有问题的成员,请将它们设为私有并#pragma远离该成员/类的警告。如果客户可以访问和使用成员,那么您将需要通过访问器和修改器提供对成员的间接访问。
回答by William Knight
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.
当我犯了使用运行时库 Single/Multithreaded 而不是 Single/MultithreadedDLL 构建 DLL 的愚蠢错误时,我通常会收到此警告。您可能想在您的项目设置中检查它。

