【发布时间】: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:
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