使用自定义工具集获取动态退出析构函数链接错误 - eh 矢量析构函数
Getting dynamic atexit destructor link error with custom toolset - eh vector destructor
尝试使用 Visual Studio 2015 工具集针对 VS2005 CRT 进行编译时,我遇到了一个奇怪的链接器错误。
相同的代码在任何其他工具集版本(2005,2010,2012,2013(上都可以完美编译。
代码必须在VS2005 CRT下编译,才能与其他项目正确链接。
如何复制: 创建一个新的空动态库 (dll( 项目(在 VS2015 中,工具集 v140(,添加一个源 (.cpp( 文件:
//1.cpp
#include <string>
static std::wstring thisWillFail[] = { L"test" };
将 VC++ 包含目录和库目录更改为:
C:Program Files (x86)Microsoft Visual Studio 8VCinclude
C:Program Files (x86)Microsoft Visual Studio 8VCatlmfcinclude
C:Program Files (x86)Microsoft Visual Studio 8VCPlatformSDKinclude
C:Program Files (x86)Microsoft Visual Studio 8VClib
C:Program Files (x86)Microsoft Visual Studio 8VCatlmfclib
C:Program Files (x86)Microsoft Visual Studio 8VCPlatformSDKLib
然后只是编译,你会得到这个错误:
1>StdAfx.obj : error LNK2019: unresolved external symbol "void __stdcall `eh vector destructor iterator'(void *,unsigned int,unsigned int,void (__thiscall*)(void *))" (??_M@YGXPAXIIP6EX0@Z@Z) referenced in function "void __cdecl `dynamic atexit destructor for 'fasdfp''(void)" (??__Ffasdfp@@YAXXZ)
如果您设置库并包含 VS2010 CRT 和 Windows SDK 的路径,也会发生同样的情况。
那么,为什么VS2015会生成这个额外的功能呢?最重要的是,我该如何解决这个问题?
相同的链接器错误出现在我拥有的每个静态成员上,并且对于几个类来说,类似的链接器错误。
在VS2015中,CRT进行了重大重构。
其中一部分是改变实现和__ehvec_dtor
签名。
如此处所述,一个简单的解决方案是添加实际实现。
最简单的方法是将此代码添加到头文件中,并将其包含在 stdafx.h 中:
#if defined __cplusplus_cli
#define CALEETYPE __clrcall
#else
#define CALEETYPE __stdcall
#endif
#define __RELIABILITY_CONTRACT
#define SECURITYCRITICAL_ATTRIBUTE
#define ASSERT_UNMANAGED_CODE_ATTRIBUTE
#if defined __cplusplus_cli
#define CALLTYPE __clrcall
#elif defined _M_IX86
#define CALLTYPE __thiscall
#else
#define CALLTYPE __stdcall
#endif
__RELIABILITY_CONTRACT
void CALEETYPE __ArrayUnwind(
void* ptr, // Pointer to array to destruct
size_t size, // Size of each element (including padding)
int count, // Number of elements in the array
void(CALLTYPE *pDtor)(void*) // The destructor to call
);
__RELIABILITY_CONTRACT
inline void CALEETYPE __ehvec_ctor(
void* ptr, // Pointer to array to destruct
size_t size, // Size of each element (including padding)
// int count, // Number of elements in the array
size_t count, // Number of elements in the array
void(CALLTYPE *pCtor)(void*), // Constructor to call
void(CALLTYPE *pDtor)(void*) // Destructor to call should exception be thrown
) {
size_t i = 0; // Count of elements constructed
int success = 0;
__try
{
// Construct the elements of the array
for (; i < count; i++)
{
(*pCtor)(ptr);
ptr = (char*)ptr + size;
}
success = 1;
}
__finally
{
if (!success)
__ArrayUnwind(ptr, size, (int)i, pDtor);
}
}
__RELIABILITY_CONTRACT
SECURITYCRITICAL_ATTRIBUTE
inline void CALEETYPE __ehvec_dtor(
void* ptr, // Pointer to array to destruct
size_t size, // Size of each element (including padding)
// int count, // Number of elements in the array
size_t count, // Number of elements in the array
void(CALLTYPE *pDtor)(void*) // The destructor to call
) {
_Analysis_assume_(count > 0);
int success = 0;
// Advance pointer past end of array
ptr = (char*)ptr + size*count;
__try
{
// Destruct elements
while (count-- > 0)
{
ptr = (char*)ptr - size;
(*pDtor)(ptr);
}
success = 1;
}
__finally
{
if (!success)
__ArrayUnwind(ptr, size, (int)count, pDtor);
}
}
相关文章:
- 不命名构造函数和析构函数上的类型错误
- 导致 Seg 错误的 SingleLinkedList 的析构函数
- LINUX 操作系统上的错误:令牌之前预期的构造函数、析构函数或类型转换'('?
- 矢量擦除调用的错误析构函数
- 在析构函数中调用"delete"运算符时"compiler is out of heap space"编译器错误
- 在Visual Studio中,与std::async一起使用时不调用"thread_local"变量"析构函数,这是一个错误吗?
- 导致堆缓冲区错误的动态分配数组的析构函数
- Qt C++析构函数错误
- C++ 链接列表队列实现和析构函数错误:"Aborted (Core Dumped)"
- 使用此指针时出现C++析构函数错误的原因
- 是否有某种方法可以检测是否使用非虚拟基析构函数错误地删除了类
- 类析构函数错误
- 析构函数错误
- C++动态数组析构函数错误
- c++中对析构函数错误的未定义引用
- 字符数组的析构函数错误
- C++复制构造函数、析构函数错误等
- 无法退出,因为析构函数错误导致main()在退出时停止执行
- 映射析构函数错误
- c++析构函数错误