使用和导出 std::string 和 std::vector<std::string>使用 Visual Studio 2015 [英] Using and exporting std::string and std::vector<std::string> with Visual Studio 2015
问题描述
使用 VS2013 和合适的导出宏,我可以使用此示例代码导出 std::string 和 std::vector:
With VS2013 and a suitable export macro, I could use this example code to export std::string and std::vector:
#ifdef _MSC_VER
// Explicit template exports.
c_EXPORT_TEMPLATE template class c_EXPORT std::allocator<char>;
c_EXPORT_TEMPLATE template struct c_EXPORT std::char_traits<char>;
c_EXPORT_TEMPLATE template class c_EXPORT std::basic_string<char, std::char_traits<char>, std::allocator<char> >;
c_EXPORT_TEMPLATE template class c_EXPORT std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >;
c_EXPORT_TEMPLATE template class c_EXPORT std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >;
#endif
(此处可编译测试用例:https://github.com/rleigh-dundee/dlltest)--这既可以作为静态库使用,也可以作为 DLL 使用
(compilable testcase here: https://github.com/rleigh-dundee/dlltest)--this works both as a static library or as DLLs using
cmake -G "Visual Studio 12 2013 Win64" -DBUILD_SHARED_LIBS=ON|OFF/path/to/source
cmake -G "Visual Studio 12 2013 Win64" -DBUILD_SHARED_LIBS=ON|OFF /path/to/source
使用 VS2015(Visual Studio 14 2015 Win64")时,我在编译与 std::string 相关的 DLL 时收到警告:
With VS2015 ("Visual Studio 14 2015 Win64") I get warnings when compiling with a DLL related to std::string:
c:\users\rleigh\libtest\a.h(30):警告 C4251:'std::_String_alloc>::_Mypair':班级'std::_Compressed_pair>,std::_String_val>,true>'需要有 dll 接口供类的客户端使用'std::_String_alloc>'
c:\users\rleigh\libtest\a.h(30): warning C4251: 'std::_String_alloc>::_Mypair': class 'std::_Compressed_pair>,std::_String_val>,true>' needs to have dll-interface to be used by clients of class 'std::_String_alloc>'
和 std::vector 类似:
and similarly for std::vector:
C:\Program Files (x86)\Microsoft Visual Studio14.0\VC\include\vector(679):警告 C4251:'std::_Vector_alloc>::_Mypair':类'std::_Compressed_pair,std::allocator>>>,std::_Vector_val,std::allocator>>>,true>'需要有 dll 接口供类的客户端使用'std::_Vector_alloc>'
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\vector(679): warning C4251: 'std::_Vector_alloc>::_Mypair': class 'std::_Compressed_pair,std::allocator>>>,std::_Vector_val,std::allocator>>>,true>' needs to have dll-interface to be used by clients of class 'std::_Vector_alloc>'
虽然这些在这个人为的测试用例中看似无害且测试运行良好,但我想修复它们.
While these are, in this contrived testcase, seemingly harmless and test tests run fine, I would like to fix them.
更严重的是,如果我使用静态库代替 DLL,链接将完全失败:
More seriously, if I use static libraries in place of DLLs, linking fails entirely:
c.lib(c.obj):错误 LNK2001:未解析的外部符号公共:静态无符号 __int64 const std::basic_string,类 std::allocator >::npos"(?npos@?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@2_KB)
c.lib(c.obj) : error LNK2001: unresolved external symbol "public: static unsigned __int64 const std::basic_string,class std::allocator >::npos" (?npos@?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@2_KB)
我在尝试使用 VS2015 构建其他项目时也遇到了后一个错误.
I have also encountered this latter error trying to build other projects with VS2015.
我的问题是:
- VS2015 中字符串和向量类到底发生了什么变化?
- 我在这里正确导出了字符串和向量类吗?我认为它符合这里的指导:https://support.microsoft.com/en-us/kb/168958
- 我应该做哪些更改来修复静态链接错误和 DLL 警告,以便它在 VS2015 和 VS2013 中都能正常工作?(我将把 2012 留到另一天,因为它有一组单独的问题).
非常感谢您的见解,罗杰
Many thanks for your insights, Roger
推荐答案
您无法在多个版本的 Visual C++ 中执行此操作.每个版本的类定义都不同,因此如果您尝试将混合版本链接在一起,就会违反单一定义规则.
There is nothing that you can do to make this work across multiple versions of Visual C++. The class definitions are different in each version, so if you try to link mixed versions together, you violate the One Definition Rule.
跨 DLL 边界导出标准库对象从来没有好处,我建议您找到一种更简单的方法(仅使用标准布局类型)来传递数据.
No good ever comes of exporting Standard Library objects across DLL boundaries, I suggest you find a simpler way (using standard-layout types only) of passing the data around.
这篇关于使用和导出 std::string 和 std::vector<std::string>使用 Visual Studio 2015的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!