Exporting Classes Containing 'Std::' Objects (Vector, Map etc.) from a Dll

Exporting classes containing `std::` objects (vector, map etc.) from a DLL

When you touch a member in your class from the client, you need to provide a DLL-interface.
A DLL-interface means, that the compiler creates the function in the DLL itself and makes it importable.

Because the compiler doesn't know which methods are used by the clients of a DLL_EXPORTED class it must enforce that all methods are dll-exported.
It must enforce that all members which can be accessed by clients must dll-export their functions too. This happens when the compiler is warning you of methods not exported and the linker of the client sending errors.

Not every member must be marked with with dll-export, e.g. private members not touchable by clients. Here you can ignore/disable the warnings (beware of compiler generated dtor/ctors).

Otherwise the members must export their methods.
Forward declaring them with DLL_EXPORT does not export the methods of these classes. You have to mark the according classes in their compilation-unit as DLL_EXPORT.

What it boils down to ... (for not dll-exportable members)

  1. If you have members which aren't/can't be used by clients, switch off the warning.

  2. If you have members which must be used by clients, create a dll-export wrapper or create indirection methods.

  3. To cut down the count of externally visible members, use approaches such as the PIMPL idiom.


template class DLL_EXPORT std::allocator<tCharGlyphProviderRef>;

This does create an instantiation of the template specialization in the current compilation unit. So this creates the methods of std::allocator in the dll and exports the corresponding methods. This does not work for concrete classes as this is only an instantiation of template classes.

How to export class containing `std::map` property with `std::unique_ptr` values

When Visual Studio needs to export a class, it instantiates everything possible. It will also try to create a copy constructor and copy assignment operator if they are not explicitly deleted.

Unfortunately, the member variable info cannot be copied because of the unique pointer. So when it tries to export the class, it fails.

Add:

Cat(const Cat&) = delete;
Cat& operator=(const Cat&) = delete;

To your class definition.

std::vector needs to have dll-interface to be used by clients of class 'X T warning

Exporting from a DLL is platform-specific. You will have to fix this for Windows (basically use declspec(dllexport/dllimport) on the instantiated class template) and encapsulate the required code in your Windows-specific preprocessor macro.

My experience is that exporting STL classes from DLLs on Windows is fraught with pain, generally I try to design the interface such that this is not needed.



Related Topics



Leave a reply



Submit