将Visual Studio 2008中的静态库与Visual Studio 2010一起使用

Using static library from Visual Studio 2008 with Visual Studio 2010

本文关键字:Studio Visual 2010 一起 静态 2008      更新时间:2023-10-16

我正在将一个大型项目从Visual studio 2008(平台工具集v90)转移到Visual studio 2010(平台工具集中v100)

该项目依赖于使用v90工具集编译的库。这个库主要是用C编写的,只有一个C++类,它在一些地方使用了std:string。不幸的是,由于工具集中的更改,这种用法使我无法使用链接的库进行编译

我从错误中删除了库名称:

 : error LNK2001: unresolved external symbol "__declspec(dllimport) public: __thiscall std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (__imp_??0?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QAE@ABV01@@Z)
 : error LNK2001: unresolved external symbol "__declspec(dllimport) public: __thiscall std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >::~basic_string<char,struct std::char_traits<char>,class std::allocator<char> >(void)" (__imp_??1?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QAE@XZ)
 : error LNK2001: unresolved external symbol "__declspec(dllimport) 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> >::erase(unsigned int,unsigned int)" (__imp_?erase@?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QAEAAV12@II@Z)
 : error LNK2001: unresolved external symbol "__declspec(dllimport) public: static unsigned int const std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >::npos" (__imp_?npos@?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@2IB)
 : error LNK2001: unresolved external symbol "__declspec(dllimport) 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> >::operator=(char const *)" (__imp_??4?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QAEAAV01@PBD@Z)
 : error LNK2001: unresolved external symbol "__declspec(dllimport) 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> >::operator=(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (__imp_??4?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QAEAAV01@ABV01@@Z)
 : error LNK2001: unresolved external symbol "__declspec(dllimport) public: __thiscall std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >(char const *)" (__imp_??0?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QAE@PBD@Z)
 : error LNK2001: unresolved external symbol "__declspec(dllimport) public: __thiscall std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >(void)" (__imp_??0?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QAE@XZ)

在平台工具集v100下,我能做些什么来编译这个库吗?这个库是专有的,所以我无法获得源代码并重新编译自己。

如果无法获得该库的VC2010版本,则需要使用VS2008构建一个DLL,该DLL提供VS2008静态库中的功能。

您可能无法使用C++链接到此DLL,当然也无法在DLL和主应用程序之间直接传递std::string。这是因为内存布局/接口不同,最终会出现ODR冲突和崩溃等情况(前提是您甚至可以以这种方式编译/链接它而不会出现大量错误)。

因此,您需要创建一个C风格的API,DLL将其公开以供您的主VS2010应用程序使用。字符串需要作为c样式的const char*const wchar_t*字符串或其他结构/不透明指针传递。

如果您可以以静态链接到VS2008运行时的方式构建DLL,那么它将简化部署,否则您将需要使用应用程序安装程序以任何方式分发VS2008和VS2010运行时。