Exporting Classes Containing std:: Objects from a DLL
When exporting classes containing objects like std::vectors and std::strings from a DLL, a warning, such as "class 'std::map<_Kty,_Ty>' needs dll-interface to be used by clients of class 'FontManager'", may arise. This warning indicates that the member types of the class require a DLL interface to be accessible by client code.
Forward Declaration of Standard Containers
To resolve this issue, forward class declarations with DLL_EXPORT can be placed before the member variables, as shown below:
template class DLL_EXPORT std::allocator<tCharGlyphProviderRef>; template class DLL_EXPORT std::vector<tCharGlyphProviderRef,std::allocator<tCharGlyphProviderRef> >; std::vector<tCharGlyphProviderRef> m_glyphProviders;
While this approach may remove warnings, it does not guarantee the availability of the DLL interface for member functions.
DLL Interface Requirements
To ensure proper DLL functionality, classes and their member functions must have a DLL interface. This means the compiler generates the function within the DLL itself, making it importable. Failure to provide this interface for members accessible by client code will result in warnings or errors during compilation or linking.
Private Members and Warnings
Private members not accessible by clients can be exempted from DLL_EXPORT declarations. Warnings for such members can be disabled. However, caution should be exercised for compiler-generated destructors and constructors.
Dll-Exportable Member Handling
Members that must be used by clients require either:
Instantiation of Template Classes
Forward declarations of template classes with DLL_EXPORT only create instantiations within the current compilation unit. This approach is insufficient for non-template classes.
The above is the detailed content of How to Export Classes Containing std:: Objects from a DLL?. For more information, please follow other related articles on the PHP Chinese website!