小编yiy*_*c91的帖子

从DLL导出STL类 - 为什么返回类型没有警告?

我的问题与导出带有STL的C++类有关.例如:

class __declspec(dllexport) Hello
{
    std::string   name;

public:
    std::string&  getName();
    void          setName(const std::string& name);
}
Run Code Online (Sandbox Code Playgroud)

各种文章似乎表明这是非常糟糕的,这是很容易理解的.必须使用相同的编译器设置和CRT版本编译所有内容.否则一切都会崩溃和燃烧.

题:

我不明白为什么只有数据成员似乎有问题.使用下面的代码,我得到:" C4251:需要让dll接口被类的客户端使用 "; 这显然是通过导出实例化的std :: string来修复的:

struct __declspec(dllexport) SomeClass
{
    // Removes the warning?
    // http://www.unknownroad.com/rtfm/VisualStudio/warningC4251.html
    //   template class __declspec(dllexport) std::string;

    std::string name;  // Compiler balks at this
}
Run Code Online (Sandbox Code Playgroud)

固定版本是:

// Export the instantiations of allocator and basic_string
template class __declspec(dllexport) std::allocator<char>;
template class __declspec(dllexport) std::basic_string<char, std::char_traits<char>, std::allocator<char> >;

struct __declspec(dllexport) SomeClass
{
    std::string name;  // No more balking!
}
Run Code Online (Sandbox Code Playgroud)

(当您尝试使用DLL时,这将为LNK2005"basic_string已定义",这意味着您不必在客户端上的CRT中链接 …

c++ dll stl instantiation visual-studio-2010

20
推荐指数
2
解决办法
7169
查看次数

标签 统计

c++ ×1

dll ×1

instantiation ×1

stl ×1

visual-studio-2010 ×1