JPh*_*618 10 c++ dll stl visual-studio-2010
好的,所以我已经阅读了几个关于这个主题的问题和文章,我觉得我理解基础知识,但我仍然遇到麻烦.
我有一个DLL导出一个具有std :: string作为成员的类.我的主程序包含也有字符串的类,它使用DLL.
如果我在VS2010中编译DLL,我会收到以下警告:
warning C4251: 'MyClass::data' : class 'std::basic_string<_Elem,_Traits,_Ax>' needs to have dll-interface to be used by clients of class 'MyClass'
Run Code Online (Sandbox Code Playgroud)
当我编译EXE时,我得到相同的警告,但没有错误,程序编译并运行.实际上,这是一个大项目,所以我得到了40个警告,我不太热衷于此.(作为侧面观察,使用VS2008编译时不会出现这些警告)
所以,我读到了这个警告,它引导我阅读这篇MS文章:http: //support.microsoft.com/default.aspx? scid = KB; EN-US; 168958,它告诉我如何从一个STL模板导出DLL来满足我得到的警告.
问题是,当我添加以下行以删除警告时:
EXPIMP_TEMPLATE template class DECLSPECIFIER std::allocator<char>;
EXPIMP_TEMPLATE template class DECLSPECIFIER std::basic_string< char, std::char_traits<char>, std::allocator<char> >;
Run Code Online (Sandbox Code Playgroud)
DLL编译没有警告,但是当我编译我的EXE时,链接器会抛出一个拟合:
2>SampleDLL.lib(SampleDLL.dll) : error LNK2005: "public: __thiscall std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >::~basic_string<char,struct std::char_traits<char>,class std::allocator<char> >(void)" (??1?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QAE@XZ) already defined in OtherClass.obj
2>SampleDLL.lib(SampleDLL.dll) : error LNK2005: "public: unsigned int __thiscall std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >::size(void)const " (?size@?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QBEIXZ) already defined in OtherClass.obj
Run Code Online (Sandbox Code Playgroud)
DLL和EXE都使用相同的代码生成选项进行编译.我可以在两者或MD上使用MT,结果是一样的.
我将最小化的示例程序中的代码包括在内,以防我遗漏任何内容.
我的主要问题:我可以修复LNK2005错误,还是可以安全地忽略C4251警告?
编辑:所以我已经阅读了一些,看起来如果DLL类使用的std :: string是一个只能由成员函数访问的私有变量,忽略警告可能是安全的......任何对此有何评论?这是朝着正确方向迈出的一步吗?
DLL代码:
#pragma once
#include <exception>
#include <string>
#ifdef SAMPLEDLL_EXPORTS
# define DECLSPECIFIER __declspec(dllexport)
# define EXPIMP_TEMPLATE
#else
# define DECLSPECIFIER __declspec(dllimport)
# define EXPIMP_TEMPLATE extern
#endif
//disable warnings on extern before template instantiation (per MS KB article)
#pragma warning (disable : 4231)
//std::basic_string depends on this allocator, so it must also be exported.
EXPIMP_TEMPLATE template class DECLSPECIFIER std::allocator<char>;
//std::string is a typedef, so you cannot export it. You must export std::basic_string
EXPIMP_TEMPLATE template class DECLSPECIFIER std::basic_string< char, std::char_traits<char>, std::allocator<char> >;
#pragma warning (default : 4231)
class DECLSPECIFIER MyClass
{
public:
std::string getData(); //returns 'data', body in CPP file
private:
std::string data;
int data2;
};
//in SampleDLL.cpp file...
std::string MyClass::getData() { return data; }
Run Code Online (Sandbox Code Playgroud)
EXE代码:
#include <iostream>
#include "SampleDLL.h"
using namespace std;
void main()
{
MyClass class1;
cout << class1.getData() << endl;
}
Run Code Online (Sandbox Code Playgroud)
您似乎看到了connect.microsoft.com上描述的问题。
那里建议了一个解决方法,但看起来有点令人讨厌。
其他可能有帮助的选项:
| 归档时间: |
|
| 查看次数: |
11680 次 |
| 最近记录: |