c++ - std::_String_alloc 析构函数链接器错误
问题描述
我有一个第三方静态库和两个 dll(比如foo.dll和bar.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>>]”无法显式实例化。
我的问题如下:
可以以及如何显式声明std::_String_alloc析构函数,以便可以抑制其隐式实例化(和符号)。
根据新生成的错误,如果std::_String_alloc显式实例化是不可能的,那么如何解决链接器错误。我无法对bar.dll进行任何更改。(由于符号已在bar.dll中定义,因此引发错误)
如果有人能对模板std::_String_alloc 有所了解,那就太好了(它是std::basic_string的基类,互联网上没有太多信息。)以及为它引发的新错误(编译器生成的函数不能显式实例化。)
其他std::basic_string成员函数(包括std::basic_string构造函数、析构函数和移动构造函数)存在链接器错误。我通过在静态库(外部模板)中明确声明这些成员函数来解决这些问题。关联
解决方案
推荐阅读
- flutter - Flutter snapshot.data() 始终为空
- python - 如何将整数值保存到文本文件
- php - 如何在 php 中获取导航菜单项?
- typescript - 这个打字稿代码中的“1”是什么意思?
- c# - 如何覆盖/修改 Frame 的 Content 属性以接受 Xamarin.Forms 中的多个视图?
- python - python没有连接到不和谐令牌
- minikube - 如何在 Cloud Code 中使用 Kind 而不是 Minikube?
- c# - 在 asp.net core 2.2 上上传文件 API 时无法使用替换功能
- c# - 如何将非一次性对象与冷可观察对象的每个订阅绑定?
- nuxt.js - 在加载 nuxt.config.js 'css' 之前在运行时加载 scss 变量