我最近在 Visual Studio 2008 中创建了一个静态 C++ 库。
在这个项目中,我使用了 std::string
类的一些方法。
现在我想在 Visual Studio 2013 项目中使用这个库。
问题是:
两个 Visual Studio 版本都使用不同的平台工具集,项目将不会编译,因为链接器错误如下:
Error 4 error LNK2001: unresolved external symbol "__declspec(dllimport) public: __thiscall std::basic_string,class std::allocator >::basic_string,class std::allocator >(void)" (__imp_??0?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QAE@XZ)
有什么方法可以使库与所有平台工具集兼容并使用一些标准类,如 std::string
?
仅供引用:VS2008 使用 v90 平台工具集,VS2013 使用 v120 平台工具集。
谢谢。
编辑:
如果我在库中使用一些标准方法,如 std::vector
,我将无法再实现该库。
这个有效:
unsigned int TestClass::TestMethod()
{
return 2;
}
这不是:
unsigned int TestClass::TestMethod()
{
std::vector<unsigned char> vtest;
vtest.push_back(0xff);
return 2;
}
错误:
Error 1 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,class std::allocator > & __thiscall std::basic_string,class std::allocator >::assign(class std::basic_string,class std::allocator > const &,unsigned int,unsigned int)" (?assign@?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QAEAAV12@ABV12@II@Z)
RT lib 设置为/MT 并禁用 whloe 程序优化 (/GL)。
有什么办法可以解决这个问题吗?
谢谢。
最佳答案
如评论中所述,您需要确保:
与旧库没有运行时库不匹配(
/MT/MD
选项)/GL
(即整个程序优化)被禁用
关于C++ 静态库和平台工具集兼容性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26177592/