I am working on a much larger project that spans many shared libraries. We also rely heavily on STL, Boost, and our own template classes and functions. Many exported classes contain template members, and exported functions contain template parameters.
Here is a stripped down example of how I export the library:
#if defined(_MSC_VER) && defined(_DLL) // Microsoft #define EXPORT __declspec(dllexport) #define IMPORT __declspec(dllimport) #elif defined(_GCC) // GCC #define EXPORT __attribute__((visibility("default"))) #define IMPORT #else // do nothing and hope for the best at link time #define EXPORT #define IMPORT #endif #ifdef _CORE_COMPILATION #define PUBLIC_CORE EXPORT #define EXTERNAL_CORE #else #define PUBLIC_CORE IMPORT #define EXTERNAL_CORE extern #endif #include <deque> // force exporting of templates EXTERNAL_CORE template class PUBLIC_CORE std::allocator<int>; EXTERNAL_CORE template class PUBLIC_CORE std::deque<int, std::allocator<int> >; class PUBLIC_CORE MyObject { private: std::deque<int> m_deque; };
SO, my problem is that when I compile in Visual Studio (both in 2008 and 2010), I get the following warning:
warning C4251: 'Stand :: _ Deque_val <_Ty, _Alloc> :: _ ALMap': class' std :: allocator <_Ty> 'must have a dll interface to be used by clients of the class' Stand :: _ Deque_val <_Ty, _Alloc> '
It looks like I did not export the std::allocator<int> that I have. And it’s not like my export is wrong, because it does not include
EXTERNAL_CORE template class PUBLIC_CORE std::allocator<int>; EXTERNAL_CORE template class PUBLIC_CORE std::deque<int, std::allocator<int> >;
displays a warning:
warning C4251: "MyObject :: m_deque": class 'std :: deque <_Ty>' must have a dll interface that will be used by clients of class 'MyObject'
The only thing I can think of is that the _Ty warning about std::allocator says that it is somehow not int , but I cannot find any indication that it would be otherwise, since a std::deque<int> logically allocates using std::allocator<int> .
A consuming application may use the class just fine, but I have a feeling that this warning should not be ignored. When compiling with g ++ on Linux, they do not emit errors (although this does not mean that they work correctly). Does g ++ automatically do what MSVC cannot do? I aimed at GCC on Linux, LLVM on OSX, and MSVC on Windows, but I can potentially move on to developing MinGW for Windows, so abandoning MSVC is not entirely accurate (if this proves too much inconvenience).
c ++ dll visual-c ++ templates
Travis gockel
source share