Plugin compilation in debug mode



  • I'm using QT version 5.5
    I'm trying to build a plugin based on the Echo plugin example

    However when I try and build my code in Debug configuration I get an error
    Undefined reference to "Vtable for HardwareInterface"

    But when I build it in Release configuration I don't get any errors and the plugin works with my main application which uses QPluginLoader to load it.

    Here is my code
    the interface header file

    #include <QString>
    
    class HardwareInterface
    {
    
    public:
        virtual ~HardwareInterface() {}
    
        virtual int StartInterfacePlugin();
        virtual bool SendData(unsigned char*);
        virtual QString GetLastError();
        virtual void ClosePlugin();
    };
    
    QT_BEGIN_NAMESPACE
    
    #define HardwarePlugin_iid "Lighting.Hardware.HardwareInterface"
    
    Q_DECLARE_INTERFACE(HardwareInterface, HardwarePlugin_iid)
    QT_END_NAMESPACE
    

    The plugin header file

    #include <QtPlugin>
    #include <QString>
    
    #include "hardwareinterface.h"
    
    
    class testplugin : public QObject, HardwareInterface
    {
        Q_OBJECT
        Q_PLUGIN_METADATA(IID HardwarePlugin_iid FILE "testplugin.json")
        Q_INTERFACES(HardwareInterface)
    
    public:
        int StartInterfacePlugin();
        bool SendData(unsigned char*);
        virtual QString GetLastError();
        virtual void ClosePlugin();
    
    private:
        QString ErrorString;
    
    
    };
    

    and the source for the plugin

    #include "testplugin.h"
    
    int testplugin::StartInterfacePlugin()
    {
        return 3;
    }
    
    bool testplugin::SendData(unsigned char *data)
    {
    
        // process data and do what we need with it
        ErrorString="None";
    
        return true;
    }
    
    QString testplugin::GetLastError()
    {
        return ErrorString;
    }
    
    void testplugin::ClosePlugin()
    {
        // do stuff needed to close the plugin
    }
    

    and this is the .pro file

    QT       += core gui
    
    TARGET = $$qtLibraryTarget(testplugin)
    TEMPLATE = lib
    CONFIG += plugin
    
    DESTDIR = plugins
    
    SOURCES += testplugin.cpp
    
    HEADERS += testplugin.h
    DISTFILES += testplugin.json
    
    

    I need to debug my application and QPluginLoader won't allow me to load plugins that are compiled in Release mode.

    I've tried building the EchoPlugin in both Debug and Release configurations and it compiles correctly in boih configurations. I can't see what the difference is.

    Any help would be appreciated.


  • Qt Champions 2016

    @Kev-The-Soundman
    Is this g++?
    If so, try to do both of the following things for your interface:

    1. un-inline the virtual destructor
    2. put a trivial non-inline constructor for your interface.

    PS.
    If your interface doesn't implement the declared virtual methods, then by all means make them pure virtual.


  • Lifetime Qt Champion

    Hi,

    In addition to @kshegunov . Did you add the Q_OBJECT macro after building your plugin once in debug mode ? If so, re-run qmake and try to rebuild



  • Hi Kshegunov

    I'm using the mingw32 compiler that comes bundles with the QT installation

    Hi SGaist

    The project was built completely from scratch with the Q_OBJECT macro already included.

    It's the fact that it compiles correctly under Release but not under Debug that confuses me.

    I only want to have debug versions so I can debug my main application which uses the plugin.


  • Qt Champions 2016

    @Kev-The-Soundman

    It's the fact that it compiles correctly under Release but not under Debug that confuses me.

    Debug is and should be more strict. You can live without a vtable, but it's a better idea not to have dangling vtable pointers ... so the debug build is properly complaining. Release doesn't care much about errors - there you go for optimizations.

    I'm using the mingw32 compiler that comes bundles with the QT installation

    Basically g++ on tranquilizers. Okay that's actually good. Then see what I wrote and do that:
    First and foremost don't allow to have virtual functions declared without implementing them (like I suspect is the case with your interface).
    And if that doesn't work, make sure you have at least one non-inline function (usually the constructor) before your virtual functions /for further reading on why, you can look here/.



  • kshegunov

    Thanks.

    I'll look at that tonight when I get home.



  • OK i figured it out.

    There was one subtle difference between my hardwareinterface class and the echointerface class from the echoplugin example.

    in echointerface.h the virtual echo function is declared like this

    virtual QString echo(const Qstring message) =- 0;
    

    but the virtual functions in my hardwareinterface.h were declared like this

    virtual int StartInterfacePlugin();
        virtual bool SendData(unsigned char*);
        virtual QString GetLastError();
        virtual void ClosePlugin();
    

    Adding the =0 to the end of each virtual function declaration fixes the problem.
    i can now compile my plugins in both release and debug mode.

    If someone can explain to me what that does i'd appreciate it.


  • Qt Champions 2016

    @Kev-The-Soundman
    Well, this is what I said in the first place.

    PS.
    If your interface doesn't implement the declared virtual methods, then by all means make them pure virtual.

    and:

    First and foremost don't allow to have virtual functions declared without implementing them (like I suspect is the case with your interface).

    = 0 marks that the virtual function is pure-virtual, which means it shall have no implementation (body).



  • Hi kshegunov

    It's my first time using virtual functions so the =0 thing to mark them as pure virtual had passed me by.

    I've now got release and debug versions of my plugins and my program. I'm happy

    Thanks for your help


  • Qt Champions 2016

    @Kev-The-Soundman said:

    It's my first time using virtual functions so the =0 thing to mark them as pure virtual had passed me by.

    Oh, I see. That explains it. :)

    I've now got release and debug versions of my plugins and my program. I'm happy
    Thanks for your help

    I'm glad it worked out. Happy coding!


Log in to reply
 

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