mercredi 28 avril 2021

std::_String_alloc destructor linker error

I am trying to link a third party static library with a DLL (say DLL A). But during linking, there is an error related to std::_String_alloc destructor. The error is as follows:

Error LNK2005 "public: __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) already defined in abc.lib(abc.dll)

The abc.dll also links with DLL A. The abc.dll explicitly instantiates the std::basic_string template and inherits the std::basic_string.

In order to solve above linker error (which clearly states that there are multiple symbols for std::_String_alloc destructor), I tried to declare the explicit instantiation (extern template) as follows:

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

But the linker error persisted and there is new error thrown which is as follows:

compiler generated function "std::_String_alloc<_Alloc_types>::~_String_alloc [with _Alloc_types=std::_String_base_types<wchar_t, std::allocator<wchar_t>>]" cannot be explicitly instantiated.

My questions are as follows:

  1. Can and how the std::_String_alloc destructor be declared explicitly so that its implicit instantiation(and Symbols) can be suppressed.

  2. As per new generated error, if std::_String_alloc explicit instantiation is not possible then how the linker error can be resolved. I can not make any changes in abc.dll. (The error is thrown as the symbol is already defined in abc.dll)

It would be great if someone can shed some light on the template std::_String_alloc (It is base class for std::basic_string and there is not much information present on the internet.)and the new error thrown for it (compiler generated functions cannot be explicitly instantiated.)

There were linker errors for other std::basic_string member functions including the std::basic_string constructor, destructor and move constructors. I resolved those by explicitly declaring those member functions in static lib (extern template).

Aucun commentaire:

Enregistrer un commentaire