首页 > 解决方案 > std::_String_alloc 析构函数链接器错误

问题描述

我有一个第三方静态库和两个 dll(比如foo.dllbar.dll)。bar.dll与foo.dll链接。bar.dll式实例化std::basic_string模板并继承std::basic_string类并将其导出。

我正在尝试将静态库foo.dll链接。但在链接过程中,出现与std::_String_alloc析构函数相关的错误。错误如下:

错误 LNK2005 “公共:__cdecl std::_String_alloc<struct std::_String_base_types<wchar_t,class std::allocator<wchar_t> > >::~_String_alloc<struct std::_String_base_types<wchar_t,class std::allocator<wchar_t> > >(void)" (??1?$_String_alloc@U?$_String_base_types@_WV?$allocator@_W@std@@@std@@@std@@QEAA@XZ) 已经在bar.lib ( bar. dll )

为了解决上述链接器错误(明确指出std::_String_alloc析构函数有多个符号),我尝试在静态库中声明显式实例化(外部模板) ,如下所示:

extern template  std::_String_alloc< std::_String_base_types<wchar_t, class std::allocator<wchar_t> >>::~_String_alloc(void);

但是链接器错误仍然存​​在,并引发了新的错误,如下所示:

编译器生成的函数“std::_String_alloc<_Alloc_types>::~_String_alloc [with _Alloc_types=std::_String_base_types<wchar_t, std::allocator<wchar_t>>]”无法显式实例化。

我的问题如下:

  1. 可以以及如何显式声明std::_String_alloc析构函数,以便可以抑制其隐式实例化(和符号)。

  2. 根据新生成的错误,如果std::_String_alloc显式实例化是不可能的,那么如何解决链接器错误。我无法对bar.dll进行任何更改。(由于符号已在bar.dll中定义,因此引发错误)

如果有人能对模板std::_String_alloc 有所了解,那就太好了(它是std::basic_string的基类,互联网上没有太多信息。)以及为它引发的新错误(编译器生成的函数不能显式实例化。)

其他std::basic_string成员函数(包括std::basic_string构造函数、析构函数和移动构造函数)存在链接器错误。我通过在静态库(外部模板)中明确声明这些成员函数来解决这些问题。关联

标签: c++c++11visual-studio-2015

解决方案


推荐阅读