在Linux上的命令行上编译QObject派生的类 [英] Compiling QObject-derived class on the command line on Linux

查看:266
本文介绍了在Linux上的命令行上编译QObject派生的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Qt的新手.我正在尝试编译如下所示的小代码段:

I am new to Qt. I am trying to compile a small code snippet shown below:

#include<QtCore/QtCore>
#include<QtCore/QObject>

class Test:public QObject
{
  Q_OBJECT
  public:
  Test(){qDebug()<<"CTOR";}
};

int main()
{
Test t;
return 0;
}

我正在尝试使用以下命令通过命令行运行它:

I am trying to run it through command line using the following command:

g++ -o signalTest.exe -l QtCore signalTest.cpp

但是我遇到以下错误:

undefined reference to vtable for Test

我认为我需要包括QObject的库,但是我不确定.有什么想法吗?

I think I need to include the library for QObject, but I am not really sure. Any ideas?

推荐答案

您没有使用元对象编译器.真好.

You are not using the meta object compiler, aka. moc, properly.

您在源代码中有一个QObject,而不是标头,因此,除了将标头包含在qmake的HEADERS变量中之外,您还需要在源代码中包含生成的moc文件,如下所示.

You have a QObject in the source as opposed to the header, so instead of including the header into the HEADERS variable for qmake, you will need to include the generated moc file in your source code as demonstrated below.

请注意,由于属性,信号和可用的插槽,通常应将Q_OBJECT宏添加到Q_OBJECT中.严格来说,这不是解决此问题的必要方法,但是如果您知道这一点,则更好.

Please note that you should add the Q_OBJECT macro to your Q_OBJECT in general due to the propeties, signals, and slots that it makes available. This is not strictly necessary to fix this issue, but it is better if you are aware of this.

#include<QtCore/QtCore>
#include<QtCore/QObject>

class Test:public QObject
{
  Q_OBJECT
  public:
  Test(){qDebug()<<"CTOR";}
};

#include "main.moc" // <----- This will make it work

int main()
{
Test t;
return 0;
}

main.pro

TEMPLATE = app
TARGET = main
QT = core
SOURCES += main.cpp

构建并运行

qmake && make

这篇关于在Linux上的命令行上编译QObject派生的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆