从VS2008升级到VS2010后出现链接错误

17

我今天在将VS版本从2008更新到2010后遇到了一个链接问题,错误信息是这样的:

error LNK2019: unresolved external symbol "public: static void __cdecl std::_String_base::_Xran(void)" (?_Xran@_String_base@std@@SAXXZ) referenced in function "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > & __thiscall std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >::assign(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,unsigned int,unsigned int)" (?assign@?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QAEAAV12@ABV12@II@Z)

error LNK2019: unresolved external symbol "public: static void __cdecl std::_String_base::_Xlen(void)" (?_Xlen@_String_base@std@@SAXXZ) referenced in function "protected: bool __thiscall std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >::_Grow(unsigned int,bool)" (?_Grow@?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@IAE_NI_N@Z)

我在网上搜过这个问题,并在这个地址找到了一个类似的帖子:http://social.msdn.microsoft.com/Forums/en/vcgeneral/thread/5ac28a31-3339-4db1-90f4-68edad360309

但是这些答案都不能解决我的问题。请问有人可以给我一些解决这个问题的提示吗?

非常感谢您的帮助!


针对这个链接错误,Xran和Xlen是存在于VS2008版本的std::basic_string中但不存在于VS2010版本中的两个函数。因此,在尝试在VS2010中链接此库时,它找不到这些函数并调用了一个错误。 - Philip
4个回答

16
问题很可能是你的.exe链接到的库之一是使用早期版本的Visual Studio构建的。因为这个“其他”库是使用早期版本的VS编译的,所以它正在寻找VS2010 C运行时中以前版本的函数_XRan和_XLen。MS已经再次更改了它们,并且旧的函数签名在VS2010运行时中不存在。
旧:public: static void __cdecl std::_String_base::_Xran(void)
新:public: void __thiscall std::basic_string::_Xran(void)(这可能是错误的,但你知道我的意思)
有三种可能的解决方案:
1)使用VS 2010编译所有库
2)使用旧版本的VS编译您的代码
3)重写现有的_XRan和_XLen实现,并在链接器中覆盖(请参见JN123在http://social.msdn.microsoft.com/Forums/en/vcgeneral/thread/5ac28a31-3339-4db1-90f4-68edad360309中的说明)。

5

在从2008迁移到2012时,我遇到了同样的问题。似乎微软仍在玩弄这些函数的签名。我的决定是只给链接器想要的代码。我将下面的代码放入我的项目cpp中,链接器就不再报错:

namespace std
{
    class _String_base
    { 
    public:
        static void _cdecl _Xlen(void) ; 
        static void _cdecl _Xran(void) ; 
    };
};

void _cdecl std::_String_base::_Xlen(void) 
{   // report a length_error
_Xlength_error("string too long");
}
void _cdecl std::_String_base::_Xran(void) 
{   // report an out_of_range error
_Xout_of_range("invalid string position");
}

3

前往项目设置:

配置属性 - 常规 - 平台工具集

  1. Visual Studio 2010 - vc100。
  2. Visual Studio 2008 - vc90。
  3. Visual Studio 2005 - vc80。

这是做什么的?它比Jess的答案好在哪里?我有这个问题,不确定如何解决。 - Tim
选择升级前项目所使用的平台工具集对我很有效。 - Denise Skidmore

1
转到您的项目设置: 配置属性常规-平台工具集 Visual Studio 2010 - vc100。 Visual Studio 2008 - vc90。 Visual Studio 2005 - vc80。
这需要在您的系统上安装所有这些Visual Studio版本。否则,您将收到以下错误: “指定的平台工具集(v90)需要安装Visual Studio 2008。请确保机器上已安装Visual Studio 2008。”

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接