为 KRunner 定义 plug-ins 时未定义 vtable

Undefined vtable when defining plug-ins for KRunner

我正在尝试使用 QtCreator 为 KRunner (for Plasma 5, documentation here) 编写 plug-in,但我遇到了可怕的 "undefined reference to vtable" 错误。

我能够在没有任何警告的情况下将 plug-in 编译为独立库。 然而,当它与一个非常简单的应用程序一起编译时,我得到了这个 vtable 错误 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.

请注意,错误是关于一个名为 factory 的 class,它是通过宏定义的(由 KDE 库提供)。

在我写代码之前我必须说:

我的 header 是 (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

实施:

#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)

如您所见,我简单地实现了所有具有空定义的方法,唯一的另一件事是调用 K_EXPORT_PLASMA_RUNNER 宏,这是我得到错误的地方。

我的 .pro 文件是:

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

如果我将 TEMPLATE 更改为 lib 并删除到目前为止未提及的文件,一切都可以正常编译并生成共享库。

main.cppunicodesymbolssearchwindow.cpp/h/ui 是使用 Qt Creator 创建示例 Qt 应用程序的基本文件。

main.cpp的内容是:

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

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

    return a.exec();
}

然后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;
}

最后 UI 是空的 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>

最后:

K_EXPORT_PLASMA_RUNNER的定义应该是:

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

取自 here (at the very end of the header file). I find odd that libname is not used. This 其他来源显示的定义略有不同:

#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)

K_PLUGIN_FACTORY定义为here。如果我尝试使用 -E 编译 unicodesymbolsrunner.cpp,似乎 K_EXPORT_PLASMA_RUNNER 宏会扩展为:

# 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));

尝试用 -E 编译,moc_unicodesymbolsrunner.cpp 生成的文件没有任何提及这样的 factory class 声明。

任何人都可以帮助我了解发生了什么以及如何解决这个问题吗? 据我所知,我完全遵循某人编写 KRunner 插件的意图...

这里的问题是我试图在单个 Qt Creator 项目中生成独立应用程序和插件库。因此,Qt Creator 正在编译包括应用程序对象文件的插件库,这在 link 时产生了 vtable 的双重声明。

因此,我发现基于某些核心库生成独立应用程序和共享库的最简单方法是使用 subdirs 模板作为 QtCreator 项目,分别开发三样东西(核心库、插件库和独立应用程序)并根据需要使用 depends 配置选项。

可能有一种方法可以在不创建多个项目的情况下完成我想做的事情,但我找不到它。如果其他人知道,请继续并添加您自己的答案。