【问题标题】:Qt Quick + CMake + custom QObject results in undefined reference to `vtable'Qt Quick + CMake + 自定义 QObject 导致对“vtable”的未定义引用
【发布时间】:2019-08-03 11:57:07
【问题描述】:

我使用 Qt Quick 制作小型应用程序来处理文件。 一切正常,直到我在build/ 文件夹中执行的那一刻

rm -rf * 
cmake ..
make

然后因为这个错误而停止(列表很大,我抑制了不重要的部分):

[100%] Linking CXX executable uint32_sort_gui

In function `LibController::~LibController()':
lib_controller.cpp:(.text+0x10): undefined reference to `vtable for LibController'
main.cpp.o: In function `int qmlRegisterType<LibController>(char const*, int, int, char const*)':

...

这是我的.hpp.cpp 课程文件:

lib_controller.hpp

#include <QObject>
#include <QString>

class LibController : public QObject{
    Q_OBJECT
    Q_PROPERTY(decltype(getProgress) progress READ getProgress NOTIFY ProgressChanged)
    public:
        explicit LibController(QObject *parent = 0);
        ~LibController(); 

        double getProgress();

        Q_INVOKABLE
        void addFile(QString from_name, QString to_name);
        Q_INVOKABLE
        void sortFile();
    signals:
        void ProgressChanged();

    private:
        double current_progress;
        FileManager* file_manager;
};

lib_controller.cpp

#include "lib_controller.hpp"

LibController::~LibController(){
    delete file_manager;
}

double LibController::getProgress(){...}

void LibController::addFile(QString from_name, QString to_name){...}

void LibController::sortFile(){...}

main.cpp

#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlComponent>

#include "lib_controller.hpp"

int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);

    // Registration of custom type
    qmlRegisterType<LibController>("com.sort.controller", 0, 1, "LibController");

    ...

    return app.exec();
}

还有我的CMakeLists.txt 配置。

我阅读了有关此问题的另一个问题,但清理和重建并没有帮助(我什至不小心删除了整个项目文件夹)。

问题仍然存在,我不明白,如何解决它...

UPD:

这里是full error message

UPD2:

从项目中排除LibController 并重新编译后,错误消失了,但没有向我显示任何窗口。我可以从终端看到它正在运行,但没有弹出 GUI。

我猜问题不在LibController,而是在其他地方。

【问题讨论】:

  • 你的 CMake 文件中有 set(CMAKE_AUTOMOC ON) 吗?此链接器错误通常是由于 moc 未处理您的头文件造成的。
  • @Mike 是的。我添加了 CMakeLists.txt 来发布
  • 我仍然怀疑由于某种原因没有为您的头文件调用 moc。你能通过运行make VERBOSE=1 并检查执行的命令来验证吗?
  • 此外,链接器错误消息的其余部分可能会提供有关缺少定义的函数的线索(例如,它是LibController::staticMetaObject?)。你能把它附加到你的问题中吗?
  • @Mike 好吧,有[ 50%] Automatic MOC and UIC for target uint32_sort_gui 行。我将完整的输出上传到pastebin

标签: c++ qt cmake qt5 qtquick2


【解决方案1】:

您需要将标头添加到源文件列表中,以便 cmake 能够在它们上运行 AUTOMOC。

这个问题已经在这里Changing location of header file causes missing vtable error when compiling with CMake提出并回答了

【讨论】:

    【解决方案2】:

    由于您在标头中调用了构造函数,请尝试将其添加到 cpp 文件中:

    LibController::LibController(QObject *parent) : QObject(parent) {
    }
    

    另外,我发现 Rebuild All 不会一直重新运行 qmake。如果您仍然看到问题运行 Build,运行 qmake 以确保标头得到处理。

    【讨论】:

    • 对不起,但这没有帮助,消息是一样的。我也不使用qmake,而是使用CMake。我每次删除build/文件夹中的所有内容,然后运行cmake .. ; make
    【解决方案3】:

    根据this问题中的cmets,你应该

    1. 将您的自定义对象头文件重命名为moc_*your header name*.hpp
    2. 将此标头的路径添加到add_executable()函数
    3. 清除build/目录并重新运行CMake
    4. 在一天结束时有点开心

    希望这会帮助像我这样试图找到最后手段的人。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-14
      相关资源
      最近更新 更多