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
(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
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): 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>'
and similarly for std::vector:
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.
More seriously, if I use static libraries in place of DLLs, linking fails entirely:
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)
I have also encountered this latter error trying to build other projects with VS2015.
My questions are:
- What exactly has changed in VS2015 to the string and vector classes?
- Am I exporting the string and vector classes properly here? I thought it matched the guidance here: https://support.microsoft.com/en-us/kb/168958
- What changes should I make to fix the static link error and DLL warnings so that it will work in both VS2015 and VS2013? (I'll leave 2012 for another day since it has a separate set of issues).
Many thanks for your insights, Roger