[英]undefined reference to staticMetaObject
It looks like Qt is not handling the Q_GADGET macro properly, because i am getting the error below. 看起来Qt没有正确处理Q_GADGET宏,因为我收到下面的错误。 Anyone know why? 谁知道为什么?
ERROR: undefined reference to `Exception::staticMetaObject' 错误: 对'Exception :: staticMetaObject'的未定义引用
EDIT: Later i found that the MOC doesn't generate moc_exception.cpp. 编辑:后来我发现MOC没有生成moc_exception.cpp。 Sometimes when i switch the compiler kit it compiles fine but than if i add another class that inherits Exception, it again doesn't work. 有时,当我切换编译器工具包时,它编译得很好但是如果我添加另一个继承Exception的类,它再次不起作用。 The problem needs to be somewhere in MOC which ignores the Q_GADGET macro in some conditions? 问题需要在MOC的哪个地方忽略某些条件下的Q_GADGET宏?
I have Qt 5.5.0. 我有Qt 5.5.0。
project.pro project.pro
CONFIG += c++11
QT += core
QT -= gui
SOURCES += \
main.cpp
HEADERS += \
nobject.h \
exception.h
nobject.h nobject.h
#ifndef NOBJECT_H
#define NOBJECT_H
#include <QObject>
class NObject
{
Q_GADGET
public:
NObject() {}
virtual ~NObject() {}
};
#endif // NOBJECT_H
exception.h exception.h
#ifndef EXCEPTION_H
#define EXCEPTION_H
#include "nobject.h"
class Exception : public NObject
{
Q_GADGET
public:
Exception() {}
virtual ~Exception() {}
};
#endif // EXCEPTION_H
main.cpp main.cpp中
#include <iostream>
#include "exception.h"
using namespace std;
int main(int argc, char* argv[]) {
Exception nobject;
std::cout << Exception::staticMetaObject.className() << std::endl;
return 0;
}
I suspect running qmake might solve your problem. 我怀疑运行qmake可能会解决您的问题。 Remember to re-run qmake every time you add or remove Q_GADGET or Q_OBJECT macro to get moc tool to re-generate code for meta data. 每次添加或删除Q_GADGET或Q_OBJECT宏时,请记住重新运行qmake,以使moc工具重新生成元数据的代码。
I didn't find any problem in your code by looking it so I copy-pasted the code to my machine and built and ran it. 通过查看我在代码中没有发现任何问题,因此我将代码复制粘贴到我的机器上并构建并运行它。 It built ok after I added implementation for NObject constructor. 在我为NObject构造函数添加实现后,它构建好了。 I built and ran it successfully on Qt5 and Qt4, and the program printed Exception on the console. 我在Qt5和Qt4上构建并成功运行它,程序在控制台上打印出Exception 。
Qt 5: Qt 5:
> ~/temp/qgadgettest$ qmake --version
QMake version 3.0
Using Qt version 5.2.1 in /usr/lib/i386-linux-gnu
> /temp/qgadgettest$ ./qgadgettest
Exception
Qt 4: Qt 4:
> ~/temp/qgadgettest$ qmake-qt4 --version
QMake version 2.01a
Using Qt version 4.8.6 in /usr/lib/i386-linux-gnu
> ~/temp/qgadgettest$ ./qgadgettest
Exception
In my environment, this code is correct. 在我的环境中,此代码是正确的。 But I have added to the body of the constructor NObject. 但是我已经添加了构造函数NObject的主体。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.