Search code examples
c++qtmacroskde-plasmavtable

Undefined vtable when defining plug-ins for KRunner


I'm trying to write a plug-in for KRunner (for Plasma 5, documentation here) using QtCreator but I'm getting the dreaded "undefined reference to vtable" error.

I'm able to compile the plug-in as a standalone library without any warning. Yet when compiling it together with an extremely simple application I get this vtable error related to a class I didn't (explicitly) write:

g++ -m64 -o UnicodeSymbolsRunner main.o unicodesymbolssearchwindow.o unicodesymbolsrunner.o moc_unicodesymbolssearchwindow.o moc_unicodesymbolsrunner.o   -L/usr/X11R6/lib64 -lQt5Widgets -lQt5Gui -lKF5Runner -lKF5Service -lKF5ConfigCore -lKF5CoreAddons -lQt5Core -lGL -lpthread 
unicodesymbolsrunner.o: In function `factory::factory()':
    /home/giacomo/Progetti/build-UnicodeSymbolsRunner-Desktop-Debug/../UnicodeSymbolsRunner/unicodesymbolsrunner.cpp:23:
        undefined reference to `vtable for factory'
unicodesymbolsrunner.o: In function `factory::~factory()':
Makefile:222: recipe for target 'UnicodeSymbolsRunner' failed
/home/giacomo/Progetti/build-UnicodeSymbolsRunner-Desktop-Debug/../UnicodeSymbolsRunner/unicodesymbolsrunner.cpp:23: undefined reference to `vtable for factory'
collect2: error: ld returned 1 exit status
make: *** [UnicodeSymbolsRunner] Error 1
23:02:54: The process "/usr/bin/make" exited with code 2.

Note that the error is about a class called factory which is defined through a macro (provided by KDE libraries).

Before I write the code I must say:

  • Yes I know there are plenty of questions mentioning this error in particular with Qt. I've read some of them but couldn't find anything useful for my situation
  • Classes do have the Q_OBJECT macro and the declaration is in the header file. I did re-run qmake and I can confirm that moc is being called since when compiling it generates the various moc_<filename>.cpp files.
  • My class doesn't define any virtual member/method and it defines all members/methods it declares.

My header is (unicodesymbolsrunner.h):

#ifndef UNICODESYMBOLSRUNNER_H
#define UNICODESYMBOLSRUNNER_H

#include <KRunner/AbstractRunner>

class UnicodeSymbolsRunner : public Plasma::AbstractRunner
{
    Q_OBJECT

public:
    UnicodeSymbolsRunner(QObject *parent, const QVariantList &args);
    ~UnicodeSymbolsRunner();

    void match(Plasma::RunnerContext &context);
    void run(const Plasma::RunnerContext &context, const Plasma::QueryMatch &match);
    void reloadConfiguration();
};

#endif // UNICODESYMBOLSRUNNER_H

The implementation:

#include "unicodesymbolsrunner.h"

UnicodeSymbolsRunner::UnicodeSymbolsRunner(QObject *parent, const QVariantList &args)
    : Plasma::AbstractRunner(parent, args)
{
}

UnicodeSymbolsRunner::~UnicodeSymbolsRunner() {}

void UnicodeSymbolsRunner::match(Plasma::RunnerContext &context)
{
    Q_UNUSED(context)
}
void UnicodeSymbolsRunner::run(const Plasma::RunnerContext &context, const Plasma::QueryMatch &match)
{
    Q_UNUSED(context)
    Q_UNUSED(match)
}

void UnicodeSymbolsRunner::reloadConfiguration() {}


K_EXPORT_PLASMA_RUNNER(unicodesymbolsrunner, UnicodeSymbolsRunner)

As you can see I simply implement all methods with empty definitions and the only other thing is the call to the K_EXPORT_PLASMA_RUNNER macro, which is the point where I get the error.

My .pro file is:

CONFIG   += c++11
QT       += core gui KRunner KConfigCore KCoreAddons KService
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
INCLUDEPATH += /usr/include/KF5

TARGET = UnicodeSymbolsRunner
TEMPLATE = app

SOURCES += main.cpp\
        unicodesymbolssearchwindow.cpp \
    unicodesymbolsrunner.cpp

HEADERS  += unicodesymbolssearchwindow.h \
    unicodesymbolsrunner.h

FORMS    += unicodesymbolssearchwindow.ui

QMAKE_CXXFLAGS += -Wextra

If I change the TEMPLATE to lib and remove the files not mentioned until now everything compiles fine and produces a shared library.

The main.cpp and unicodesymbolssearchwindow.cpp/h/ui are the basic files to create a sample Qt application with Qt Creator.

The contents of main.cpp are:

#include "unicodesymbolssearchwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    UnicodeSymbolsSearchWindow w;
    w.show();

    return a.exec();
}

Then unicodesymbolssearchwindow.cpp:

#include "unicodesymbolssearchwindow.h"
#include "ui_unicodesymbolssearchwindow.h"

UnicodeSymbolsSearchWindow::UnicodeSymbolsSearchWindow(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::UnicodeSymbolsSearchWindow)
{
    ui->setupUi(this);
}

UnicodeSymbolsSearchWindow::~UnicodeSymbolsSearchWindow()
{
    delete ui;
}

Finally the UI is an empty QWidget:

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>UnicodeSymbolsSearchWindow</class>
 <widget class="QWidget" name="UnicodeSymbolsSearchWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>400</width>
    <height>300</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>UnicodeSymbolsSearchWindow</string>
  </property>
 </widget>
 <layoutdefault spacing="6" margin="11"/>
 <resources/>
 <connections/>
</ui>

Finally:

The definition of K_EXPORT_PLASMA_RUNNER should be:

#define K_EXPORT_PLASMA_RUNNER( libname, classname )     \
    K_PLUGIN_FACTORY(factory, registerPlugin<classname>();) \
    K_EXPORT_PLUGIN_VERSION(PLASMA_VERSION)

taken from here (at the very end of the header file). I find odd that libname is not used. This other source shows a slightly different definition:

#define K_EXPORT_PLASMA_RUNNER( libname, classname ) \
    K_PLUGIN_FACTORY(factory, registerPlugin<classname>();) \
    K_EXPORT_PLUGIN(factory("plasma_runner_" #libname)) \
    K_EXPORT_PLUGIN_VERSION(PLASMA_VERSION)

The K_PLUGIN_FACTORY is defined here. If I try to compile unicodesymbolsrunner.cpp using -E it seems that the K_EXPORT_PLASMA_RUNNER macro gets expanded to:

# 23 "../UnicodeSymbolsRunner/unicodesymbolsrunner.cpp" 3 4
class factory : public KPluginFactory
{
public:
    template <typename ThisObject>
    inline void qt_check_for_QOBJECT_macro(const ThisObject &_q_argument) const { int i = qYouForgotTheQ_OBJECT_Macro(this, &_q_argument); i = i + 1; }
# 23 "../UnicodeSymbolsRunner/unicodesymbolsrunner.cpp"
#pragma GCC diagnostic push
# 23 "../UnicodeSymbolsRunner/unicodesymbolsrunner.cpp"
    static const QMetaObject staticMetaObject;
    virtual const QMetaObject *metaObject() const;
    virtual void *qt_metacast(const char *);
    virtual int qt_metacall(QMetaObject::Call, int, void **);
# 23 "../UnicodeSymbolsRunner/unicodesymbolsrunner.cpp"
#pragma GCC diagnostic pop
# 23 "../UnicodeSymbolsRunner/unicodesymbolsrunner.cpp"
    static inline QString tr(const char *s, const char *c = nullptr, int n = -1) { return staticMetaObject.tr(s, c, n); }
    static inline QString trUtf8(const char *s, const char *c = nullptr, int n = -1) { return staticMetaObject.tr(s, c, n); }
private:
    __attribute__((visibility("hidden")))
    static void qt_static_metacall(QObject *, QMetaObject::Call, int, void **);
    struct QPrivateSignal {};
public:
    explicit factory();
    ~factory();
private:
    void init();
};
factory::factory() { registerPlugin<
# 23 "../UnicodeSymbolsRunner/unicodesymbolsrunner.cpp"
UnicodeSymbolsRunner
# 23 "../UnicodeSymbolsRunner/unicodesymbolsrunner.cpp" 3 4
>(); }
factory::~factory() {}
extern "C" __attribute__((visibility("default"))) const quint32 kde_plugin_version = ((5<<16)|(18<<8)|(0));

Trying to compile with -E the moc_unicodesymbolsrunner.cpp produces a file without any mention of such a factory class declaration.

Can anyone help me understand what's happening and how to fix this? As far as I know I followed exactly what someone is meant to do to write a KRunner plugin...


Solution

  • The problem here is that I was trying to both produce a standalone application and the plug-in library in a single Qt Creator project. As such Qt Creator was compiling the plug-in library including the application's objects files, and this produced the double declaration of the vtable at link time.

    So, the easiest way I found to produce both a standalone application and a shared library based on some core libraries is to use the subdirs template as QtCreator project, develop the three things separately (core libraries, the plug-in library and the standalone application) and use the depends configuration options as necessary.

    There may be a way to do what I wanted without creating multiple projects, but I couldn't find it. If someone else knows it go ahead and add your own answer.