c++ - 为 KRunner 定义插件时未定义的 vtable

标签 c++ qt macros kde-plasma vtable

我正在尝试为 KRunner 编写一个插件(对于 Plasma 5,文档 here)使用 QtCreator,但我遇到了可怕的“对 vtable 的 undefined reference ”错误。

我能够在没有任何警告的情况下将插件编译为独立库。 然而,当将它与一个极其简单的应用程序一起编译时,我得到了这个 vtable 错误与我没有(显式)编写的类相关:

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

实现:

#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 (在头文件的最后)。我觉得很奇怪 libname 没有被使用。 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.cppK_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 类声明。

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

最佳答案

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

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

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

关于c++ - 为 KRunner 定义插件时未定义的 vtable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36925763/

相关文章:

c++ - 为什么我的 QDeclarativeItem 没有获取任何鼠标/键盘事件?

c++ - 解析终端参数 Qt Mac OS X

java - Scala 宏找不到 java.util.List、java.lang.Object

c++ - 使用宏来压缩代码

c++ - 如何标记粘贴号码?

c++ - 为什么我的 C++ 并行程序在 MPI_Gather 中出现 MPI fatal error ?

c++ - 没有帧率限制的移动 C++ SFML

python - 布局内容更改后将 QMainWindow 调整为最小尺寸

c++ - OpenMesh Decimater 不会减少顶点数

c++ - 将 C++ 成员函数映射到 C 回调