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

C++Dll

C++ Problem Overview


I'm trying to make my library exportable as a DLL but I'm getting a lot of these warnings for one specific class that uses std::vector:

template <typename T>
class AGUI_CORE_DECLSPEC AguiEvent {
	typedef void (*AguiCallbackFptr)(T arg, AguiWidget* sender);
std::vector<AguiCallbackFptr> events;
public:
	void call(AguiWidget* sender, T arg) const;
	void addHandler(AguiCallbackFptr proc);
	void removeHandler(AguiCallbackFptr proc);
	void removeHandler();
	AguiEvent();
};

I get warnings like these:

> Warning 57 warning C4251: > 'AguiEvent::events' : class > 'std::vector<_Ty>' needs to have > dll-interface to be used by clients of > class 'AguiEvent'

I tried to find how to do this properly but MSDN's documentation is very Windows Only, and I need this to be cross platform so that it only does MS specific stuff when AGUI_CORE_DECLSPEC is in fact defined.

What should I do to get rid of these warnings?

Thanks

C++ Solutions


Solution 1 - C++

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.

Solution 2 - C++

One fix is relying on dynamic allocation/deallocation of the STL structures. So:

class EXPORTED ExportedClass
{
private:
    std::vector<int> *_integers;
public:
    ExportedClass()
    {
        _integers = new std::vector<int>();
    }
    ~ExportedClass()
    {
        delete _integers;
    }
};

won't give any warning and it's more safe in case you are distributing the same binary (the dll) that has to be used with different version of the compiler that may has different versions of the STL. In this way you have 100% guarantee that sizeof(ExportedClass) is always the same.

Solution 3 - C++

You could just export the members, which the dll-clients need to access. To do this remove the export declaration from the class declaration and add it to each individual member function you want to export.

EDIT:

In your case you should probably not try to export the class (leave out AGUI_CORE_DECLSPEC) since it is a template class. Provide all methods in your header as inline and it will work.

If you do not want this, some compilers provide a special way to export template classes. But you will have to specify the template parameter for this.

Solution 4 - C++

The usual method of dealing with platform specific stuff like this is to try and restrict all platform specific settings to a handful of low level files/classes, and then use #defines and #ifdef/#ifndef preprocessor directives to add/replace platform specific variations.

To effectively implement this, you may need an abstraction layer. For example a production system I worked on in the 1990s had a "File System" library. This presented a common interface to the applications and production code, but had to rely on a few platform-specific files. As well as making it easier to compile and maintain, it also made it easier to port to new platforms. A new file hardware vendor or OS flavour? Simply add the settings to the include files and add new directives accordingly.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionjmasterxView Question on Stackoverflow
Solution 1 - C++Steve TownsendView Answer on Stackoverflow
Solution 2 - C++ceztkoView Answer on Stackoverflow
Solution 3 - C++frastView Answer on Stackoverflow
Solution 4 - C++winwaedView Answer on Stackoverflow