Abstract plugin inheritance error



  • I'm trying to create a plugin system and I have an interface for the plugin and also an abstract plugin that others plugin will inherit however I'm getting a lot of errors:

    Before the errors, here is how my structure looks like:

    |-- myproject
        |-- myproject.pro
        |-- plugins
        |   |-- plugins.pro
        |   |-- pluginfoo
        |   |   |-- pluginfoo.cpp
        |   |   |-- pluginfoo.hpp
        |   |   |-- pluginfoo.json
        |   |   |-- pluginfoo.pro
        |-- myproject
            |-- myproject.pro
            |-- gui
            |   |-- myprojectwindow.cpp
            |   |-- myprojectwindow.hpp
            |-- src
                |-- main.cpp
                |-- plugininterface.hpp
                |-- abstractplugin.cpp
                |-- abstractplugin.hpp
    

    myproject.pro

    TEMPLATE = subdirs
    SUBDIRS  = myproject \
               plugins
    

    plugins.pro

    TEMPLATE = subdirs
    SUBDIRS  = pluginfoo
    

    pluginfoo.cpp

    #include "pluginfoo.hpp"
    

    pluginfoo.hpp

    #ifndef PLUGINFOO_HPP
    #define PLUGINFOO_HPP
    
    #include "abstractplugin.hpp"
    
    class PluginFoo : public AbstractPlugin
    {
        Q_OBJECT
        Q_PLUGIN_METADATA(IID PluginInterface_iid FILE "pluginfoo.json")
        Q_INTERFACES(PluginInterface)
    };
    
    #endif // PLUGINFOO_HPP
    

    pluginfoo.json
    {}

    pluginfoo.pro

    QT          += core gui
    TARGET       = pluginfoo
    TEMPLATE     = lib
    CONFIG      += plugin
    DESTDIR      = ../../bin/plugins
    DEFINES     += QT_DEPRECATED_WARNINGS
    INCLUDEPATH += ../../myproject/src
    SOURCES     += pluginfoo.cpp
    HEADERS     += pluginfoo.hpp
    DISTFILES   += pluginfoo.json
    

    myproject.pro

    QT      += core gui widgets xml network
    TARGET   = myproject
    TEMPLATE = app
    DESTDIR  = ../bin
    DEFINES += QT_DEPRECATED_WARNINGS
    SOURCES += src/main.cpp \
               gui/myprojectwindow.cpp
               src/abstractplugin.hpp
    HEADERS += gui/myprojectwindow.hpp \
               src/plugininterface.hpp
               src/abstractplugin.hpp
    

    myprojectwindow.cpp

    #include "myprojectwindow.hpp"
    
    MyProjectWindow::MyProjectWindow(QWidget *parent) : QMainWindow(parent)
    {
    }
    
    MyProjectWindow::~MyProjectWindow()
    {
    
    }
    

    myprojectwindow.hpp

    #ifndef MYPROJECTWINDOW_HPP
    #define MYPROJECTWINDOW_HPP
    
    #include <QMainWindow>
    
    class MyProjectWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        MyProjectWindow(QWidget *parent = 0);
        ~MyProjectWindow();
    };
    
    #endif // MYPROJECTWINDOW_HPP
    

    main.cpp

    #include "gui/myprojectwindow.hpp"
    #include <QApplication>
    
    int main(int argc, char *argv[])
    {
        QApplication application(argc, argv);
    
        MyProjectWindow window;
        window.show();
    
        return application.exec();
    }
    

    plugininterface.hpp

    #ifndef PLUGININTERFACE_HPP
    #define PLUGININTERFACE_HPP
    
    class PluginInterface
    {
    public:
        virtual ~PluginInterface() {}
    };
    
    #define PluginInterface_iid "PluginInterface/1.0"
    
    Q_DECLARE_INTERFACE(PluginInterface, PluginInterface_iid)
    
    #endif // PLUGININTERFACE_HPP
    

    abstractplugin.cpp

    #include "abstractplugin.hpp"
    

    abstractplugin.hpp

    #ifndef ABSTRACTPLUGIN_HPP
    #define ABSTRACTPLUGIN_HPP
    
    #include <QtPlugin>
    #include "plugininterface.hpp"
    
    class AbstractPlugin : public QObject, public virtual PluginInterface
    {
        Q_OBJECT
        Q_INTERFACES(PluginInterface)
    };
    
    #endif // ABSTRACTPLUGIN_HPP
    

    And I get this when compiling:

    moc_pluginfoo.obj:-1: error: LNK2001: unresolved external symbol "public: static struct QMetaObject const AbstractPlugin::staticMetaObject" (?staticMetaObject@AbstractPlugin@@2UQMetaObject@@B)

    moc_pluginfoo.obj:-1: error: LNK2001: unresolved external symbol "public: virtual struct QMetaObject const * __cdecl AbstractPlugin::metaObject(void)const " (?metaObject@AbstractPlugin@@UEBAPEBUQMetaObject@@XZ)

    moc_pluginfoo.obj:-1: error: LNK2019: unresolved external symbol "public: virtual void * __cdecl AbstractPlugin::qt_metacast(char const *)" (?qt_metacast@AbstractPlugin@@UEAAPEAXPEBD@Z) referenced in function "public: virtual void * __cdecl PluginFoo::qt_metacast(char const *)" (?qt_metacast@PluginFoo@@UEAAPEAXPEBD@Z)

    moc_pluginfoo.obj:-1: error: LNK2019: unresolved external symbol "public: virtual int __cdecl AbstractPlugin::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall@AbstractPlugin@@UEAAHW4Call@QMetaObject@@HPEAPEAX@Z) referenced in function "public: virtual int __cdecl PluginFoo::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall@PluginFoo@@UEAAHW4Call@QMetaObject@@HPEAPEAX@Z)

    ....\bin\plugins\pluginfoo.dll:-1: error: LNK1120: 4 unresolved externals


  • Qt Champions 2016

    You haven't exported any of your classes.
    Also is the virtual inheritance really necessary? I shouldn't think so.



  • @kshegunov It's not an export problem, I forgot to type the src/plugininterface.hpp in the pro file here on the example. I edited the question.
    The virtual inheritance is to avoid the diamond problem.


  • Qt Champions 2016

    @Defohin said in Abstract plugin inheritance error:

    It's not an export problem

    I would argue that it is. You're trying to use a class that's defined in the application project in the library. The Q_OBJECT macro generates members(!) that have to be accessible to the linker, and in your case they aren't. This is moot when dealing with interfaces, but it's significant when dealing with classes like AbstractPlugin.

    The virtual inheritance is to avoid the diamond problem.

    If you have a diamond somewhere, then you've done something very, very, very wrong. I have had a need to deal with it only a couple of times in my 10 years of experience with C++. Virtual inheritance is evil, avoid it at (almost) any cost!



  • @kshegunov Thank you, for real, but what you think I should do? The entire code is here, but still I have no idea what is happening.


  • Qt Champions 2016

    Simply remove the AbstractPlugin class, it's not necessary anyway, it does nothing. Instead for each plugin derive from QObject and the interface as you've done. That should fix it for the most part. Namely:

    class PluginFoo : public QObject, public PluginInterface
    {
        Q_OBJECT
        Q_PLUGIN_METADATA(IID PluginInterface_iid FILE "pluginfoo.json")
        Q_INTERFACES(PluginInterface)
    };
    

    Then (as you're working on windows) you'll probably get a linker error for the plugin class itself, exporting it from the dll should fix that as well.



  • @kshegunov I will need the abstract class to add a few common methods. It's just an example code.


  • Qt Champions 2016

    @Defohin said in Abstract plugin inheritance error:

    I will need the abstract class to add a few common methods. It's just an example code.

    Then you must provide the linker with the appropriate binary. Meaning you must, in this case, move the abstract plugin class in a separate library to which your plugins will link (look up the docs, there's examples how to make an ordinary dynamic library look here). In this library you need to export the AbstractPlugin class (through Q_DECL_EXPORT and Q_DECL_IMPORT again you can look how exactly in the docs). The interface can stay either in the application project or in the new library, that's up to you and is a matter of convenience.



  • @kshegunov I tried to add this:

    myprojectglobal.h

    #ifndef MYPROJECTGLOBAL_HPP
    #define MYPROJECTGLOBAL_HPP
    
    #include <QtCore/qglobal.h>
    
    #if defined(MYPROJECT_LIBRARY)
    #  define MYPROJECT_EXPORT Q_DECL_EXPORT
    #else
    #  define MYPROJECT_EXPORT Q_DECL_IMPORT
    #endif
    
    #endif // MYPROJECTGLOBAL_HPP
    

    myproject.pro

    DEFINES += MYPROJECT_LIBRARY
    

    plugininterface.hpp

    #ifndef PLUGININTERFACE_HPP
    #define PLUGININTERFACE_HPP
    
    #include "myprojectglobal.hpp"
    #include <QtPlugin>
    
    class MYPROJECT_EXPORT PluginInterface
    {
    public:
        virtual ~PluginInterface() {}
    };
    
    #define PluginInterface_iid "PluginInterface/1.0"
    
    Q_DECLARE_INTERFACE(PluginInterface, PluginInterface_iid)
    
    #endif // PLUGININTERFACE_HPP
    

    abstractplugin.hpp

    #ifndef ABSTRACTPLUGIN_HPP
    #define ABSTRACTPLUGIN_HPP
    
    #include "plugininterface.hpp"
    
    class MYPROJECT_EXPORT AbstractPlugin : public QObject, public PluginInterface
    {
        Q_OBJECT
        Q_INTERFACES(PluginInterface)
    };
    
    #endif // ABSTRACTPLUGIN_HPP
    

    And now I'm getting these errors:

    moc_pluginfoo.obj:-1: error: LNK2019: unresolved external symbol "__declspec(dllimport) public: virtual __cdecl PluginInterface::~PluginInterface(void)" (_imp??1PluginInterface@@UEAA@XZ) referenced in function "int public: __cdecl PluginFoo::PluginFoo(void)'::1'::dtor$0" (?dtor$0@?0???0PluginFoo@@QEAA@XZ@4HA)

    moc_pluginfoo.obj:-1: error: LNK2019: unresolved external symbol "__declspec(dllimport) public: __cdecl PluginInterface::PluginInterface(void)" (_imp??0PluginInterface@@QEAA@XZ) referenced in function "public: __cdecl PluginFoo::PluginFoo(void)" (??0PluginFoo@@QEAA@XZ)

    moc_pluginfoo.obj:-1: error: LNK2019: unresolved external symbol "__declspec(dllimport) public: virtual void * __cdecl AbstractPlugin::qt_metacast(char const *)" (_imp?qt_metacast@AbstractPlugin@@UEAAPEAXPEBD@Z) referenced in function "public: virtual void * __cdecl PluginFoo::qt_metacast(char const *)" (?qt_metacast@PluginFoo@@UEAAPEAXPEBD@Z)

    moc_pluginfoo.obj:-1: error: LNK2019: unresolved external symbol "__declspec(dllimport) public: virtual int __cdecl AbstractPlugin::qt_metacall(enum QMetaObject::Call,int,void * *)" (_imp?qt_metacall@AbstractPlugin@@UEAAHW4Call@QMetaObject@@HPEAPEAX@Z) referenced in function "public: virtual int __cdecl PluginFoo::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall@PluginFoo@@UEAAHW4Call@QMetaObject@@HPEAPEAX@Z)

    moc_pluginfoo.obj:-1: error: LNK2019: unresolved external symbol "__declspec(dllimport) public: __cdecl AbstractPlugin::AbstractPlugin(void)" (_imp??0AbstractPlugin@@QEAA@XZ) referenced in function "public: __cdecl PluginFoo::PluginFoo(void)" (??0PluginFoo@@QEAA@XZ)

    moc_pluginfoo.obj:-1: error: LNK2019: unresolved external symbol "__declspec(dllimport) public: virtual __cdecl AbstractPlugin::~AbstractPlugin(void)" (_imp??1AbstractPlugin@@UEAA@XZ) referenced in function "public: virtual __cdecl PluginFoo::~PluginFoo(void)" (??1PluginFoo@@UEAA@XZ)

    moc_pluginfoo.obj:-1: error: LNK2001: unresolved external symbol "__declspec(dllimport) public: static struct QMetaObject const AbstractPlugin::staticMetaObject" (_imp?staticMetaObject@AbstractPlugin@@2UQMetaObject@@B)

    ....\bin\plugins\pluginfoo.dll:-1: error: LNK1120: 7 unresolved externals


  • Moderators

    @Defohin Do you link your pluginfoo plugin against the lib containing AbstractPlugin and PluginInterface?



  • @jsulm What you mean? The whole code I have is here.


  • Lifetime Qt Champion

    Hi,

    The usual setup when building a plugin based application is to have the plugin interfaces definitions and related code in a library that you will link to both the plugin and the application.

    That's what is suggested here.



  • @SGaist I'm really struggling to understand, can you provide an example, please?


  • Lifetime Qt Champion

    Roughly :
    pluginfoo.pro

    QT          += core gui
    TARGET       = pluginfoo
    TEMPLATE     = lib
    CONFIG      += plugin
    DESTDIR      = $$OUTPWD/bin/plugins
    LIBS += -L$$OUTPWD/lib -lmycoollib
    DEFINES     += QT_DEPRECATED_WARNINGS
    INCLUDEPATH += ../mycoollib
    SOURCES     += pluginfoo.cpp
    HEADERS     += pluginfoo.hpp
    DISTFILES   += pluginfoo.json
    

    mycoollib.pro

    QT          += core gui
    TARGET       = mycoollib
    TEMPLATE  = lib
    DESTDIR      = $$OUTPWD/lib
    INCLUDEPATH += .
    SOURCES     += \
        abstractplugin.cpp
    HEADERS     += \
        plugininterface.hpp
        abstractplugin.hpp
    
    

    myproject.pro

    QT      += core gui widgets xml network
    TARGET   = myproject
    TEMPLATE = app
    DESTDIR  = $$OUTPWD/bin
    LIBS += -L$$OUTPWD/lib -lmycoollib
    INCLUDEPATH += ../mycoollib
    DEFINES += QT_DEPRECATED_WARNINGS
    SOURCES += src/main.cpp \
               gui/myprojectwindow.cpp
    
    HEADERS += gui/myprojectwindow.hpp
    

    Project structure

    mycoolproject \
    ---- mycoollib
    ---- pluginfoo
    ---- myproject
    

    mycoolproject being a SUBDIR project building first mycoollib and then pluginfoo and myproject.



  • @SGaist Do I have to link with -l every single plugin I have? Or I can just provide a path like -L and it's going to work?

    I will try to modify here based on your example.


  • Lifetime Qt Champion

    You don't link the plugins. You link your plugins against that common library.

    Yes, you have to use both -land -L.

    -L just tells the linker where to look at.



  • @SGaist It works, but I have another question now.

    I named the "commonlib" to "pluginmanager" this class will hold information about plugins, load, unload and so on using QPluginLoader, but... when I try to use it on the main application including the header pluginmanager.hpp it says that it cannot open the file, even if I have the INCLUDEPATH += ../pluginmanager. On Qt Creator it does work, I'm able to ctrl+click, etc, but when I try to compile I get this error.


  • Moderators

    @Defohin Did you run qmake again after changing the pro file? After qmake you should do a complete rebuild.



  • @jsulm It works, but now I'm getting unresolved external symbol on the main project when trying to use PluginManager manager on the MyProjectWindow, and I have LIBS += -L../libs -lpluginmanager on the pro file.

    myprojectwindow.obj:-1: error: LNK2019: unresolved external symbol "public: __cdecl PluginManager::PluginManager(class QObject *)" (??0PluginManager@@QEAA@PEAVQObject@@@Z) referenced in function "public: __cdecl MyProjectWindow::MyProjectWindow(class QWidget *)" (??0MyProjectWindow@@QEAA@PEAVQWidget@@@Z)


  • Moderators

    @Defohin ../libs is a relative path, are you sure it is correct?
    Can you post the compiler/linker call just before the error message?
    As suggested by @SGaist : LIBS += -L$$OUTPWD/lib -lmycoollib (pluginmanager instead of mycoollib)



  • @jsulm I'm using $$OUTPWD now but it still doesn't work.


  • Moderators

    @Defohin Is the resulting path correct and is the lib there?



  • It's working now, I forgot to put MYPROJECT_EXPORT on PluginManager class.

    I love you guys ♥


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.