Re: passing stl through dll
"Konran Udo Gerber" wrote:
I followed both articles referred in this thread and it seems to
work with vector for me. But in case of string or
basic_string<char> I run into deep trouble.
[...]
EXPIMP_STL template class STL_API allocator<char>;
/*EXPIMP_STL*//*explicit: */ extern template class STL_API
basic_string<char, s_chTraits_t, cl_chAlloc_t>; //
instantiation line with extreme trouble
The culprit is that many Standard C++ templates are already
instantiated and packed in "msvcpXX.dll" (where XX is the version
of VC++). Among them is `std::basic_string<char>' instantiation.
So, when you try to export `std::basic_string<char>' from your
DLL, thne it conflicts with already exported version from
"msvcpXX.dll".
There are two ways to solve it:
1. Easy way. Just disable C4251 warning alltogether. It's a bit
dirty solution, but it works with VC++. Eventually, both EXE and
DLL will use `std::string' instantiation from "msvcpXX.dll".
2. Hard way. First of all, define _STATIC_CPPLIB for the project.
It will tell the compiler to instantiate standard templates from
headers instead of picking them up from "msvcpXX.dll". Then, by
using trial and error method, add relevant instantiations in your
DLL header file. For example, if you want to use
`std::vector<std::string>' members in your exported class, then
you have to add the following exported instantiations:
class STL_API std::_String_base;
class STL_API std::_Container_base_secure;
template class STL_API std::allocator<char>;
template class STL_API std::basic_string<char>;
template class STL_API std::allocator<std::basic_string<char> >;
template class STL_API std::vector<std::basic_string<char> >;
class STL_API MyClass
{
public:
...
private:
std::vector<std::string> m_v;
};
Now you will be able to compile it without C4251 and link
successfully.
HTH
Alex