apavlenko / vmf

http://01org.github.io/vmf/
Apache License 2.0
0 stars 3 forks source link

Use of STL classes in VMF API produces problems on Windows in DLL case #17

Open apavlenko opened 8 years ago

apavlenko commented 8 years ago

A warning "warning C4251: 'XXX' : class 'std::YYY<...>' needs to have dll-interface to be used by clients of class 'XXX'" says that due to several binary-incompatible STL versions on Windows (per VS version) this is dangerous.

apavlenko commented 8 years ago

A possible solution is described here: http://stackoverflow.com/questions/16419318/one-way-of-eliminating-c4251-warning-when-using-stl-classes-in-the-dll-interface

I think you've got at least three possible options to solve this problem:

...
#include <vector>

template class __declspec(dllexport) std::allocator<int>;
template class __declspec(dllexport) std::vector<int>;
template class __declspec(dllexport) std::string;

class __declspec(dllexport) HelloWorld
...

By the way, the order of those exports seems to be important: the vector class template uses the allocator class template internally, so the allocator instantiation has to be exported before the vector instantiation.

int *p_abc;
int abc_len;

in something like class MyFancyDataArray and the fields

char *p_str;
int str_len;

in something like class MyFancyString, then this would be a more decent solution (similar to the one described at the second point). But it is probably not the best habit to reinvent the wheel too often.