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

前端 未结 4 1275
长发绾君心
长发绾君心 2020-12-05 06:29

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 

        
相关标签:
4条回答
  • 2020-12-05 06:40

    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.

    0 讨论(0)
  • 2020-12-05 06:53

    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.

    0 讨论(0)
  • 2020-12-05 06:58

    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.

    0 讨论(0)
  • 2020-12-05 07:04

    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.

    0 讨论(0)
提交回复
热议问题