导出一个类到DLL,其中用到STL,比如 vector < int >
就会有警告:
C4251 warning when using stl-classes in the dll-interface
原因是因为STL的类并没有导出。可能出现的问题比如在一个导出类Y中用到一个非导出类X,那么在Y的一个内联函数y_f 可能调用了 X的x_f同样内联的函数;如果不是静态链接X,就会链接失败,因为找不到x_f
[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.
解决办法
1)直接忽略,加上 #pragma warning( disable: 4251)
最好用
#pragma warning(push)
#pragma warning(disable:4251)
// your header...
#pragma warning(pop)
因为你的头文件通常是给别人引用的,其他人可能不希望屏蔽该警告
2)确保所有使用到STL的成员都是private的 – 没看懂。。
3) 使用PIMPL模式 (Pointer To Implementation)
将数据改为指针的方式
class EXPORTED ExportedClass
{
private:
std::vector<int> *_integers;
public:
ExportedClass()
{
_integers = new std::vector<int>();
}
~ExportedClass()
{
delete _integers;
}
};
4) 直接导出所需要的模板类的特化。
比如
# include < vector >
template class __declspec(dllexport) std::allocator;
template class __declspec(dllexport) std::vector;
template class __declspec(dllexport) std::string;class __declspec(dllexport) HelloWorld
因为vector的内部用到了allocator,所以要先对 allocator进行实例化
但这里的显式实例化可能有个问题,比如另外一个DLL也这么解决,那么当一个程序调用这两个DLL时,就会出现 multiply-defined symbol errors 重定义的错误
—- 另一个 Impl 实现 的例子
#include <iostream>
#include <vector>
using namespace std;
// 这些放到.h中
class Test_imp;
class __declspec( dllexport ) Test
{
// 构造函数中初始化 析构中释放m_pImp;
void test();
public:
Test_imp* m_pImp;
};
// 这个类放到cpp中去
class Test_imp
{
public:
void test(){}
std::vector<int> m_objCon;
};
// 放到cpp中
void Test::test()
{
m_pImp->test();
}
int main()
{
....
return 0;
}
参考:
解决 Warning C4251 问题 http://blog.csdn.net/shellching/article/details/7090742
https://en.wikipedia.org/wiki/Opaque_pointer#C.2B.2B