QT单元测试moc“未解析的外部符号”用于QMetaObject

时间:2017-01-06 16:53:16

标签: c++ qt unit-testing

我第一次尝试将单元测试添加到我的项目中。

我可以运行模拟测试(不使用我的项目的类)并运行应用程序。但是如果我从项目中实例化对象,我会得到一个未解析的QMetaObject外部符号。如果我没记错的话,这意味着对象的moc没有包含在项目中。

我该如何解决这个问题?我使用googletests也有同样的问题。该指南也没有帮助。我已经尝试安装qt单元测试插件,结果相同。

我上传的模拟项目遵循我在上述项目中使用的相同结构,在此处获取:https://github.com/quimnuss/QtUnitTestingTest

我在Windows上使用静态构建的qt,但我猜这是不相关的。使用QtCreator作为IDE和NMAke构建。

我也尝试过添加HelloWorld.lib,但是看看Makefile.release它没有被使用。

有人知道我做错了什么?

这是单元测试.pro:

QT       += widgets network testlib

TARGET = tst_someunittesttest
CONFIG   += console
CONFIG   -= app_bundle

TEMPLATE = app

INCLUDEPATH += $$PWD/../HelloWorld

include($$PWD/../HelloWorld/helloworldCommon.pri)

LIBS += -L"$$OUT_PWD/../HelloWorld/release"
LIBS += -lHelloWorld

message("Searching libs here $$LIBS")

SOURCES += tst_someunittesttest.cpp
DEFINES += SRCDIR=\\\"$$PWD/\\\"

第一个错误的完整消息:

tst_someunittesttest.obj : error LNK2001: unresolved external symbol "public: virtual struct QMetaObject const * __cdecl HelloWorld::metaObject(void)const " (?metaObject@HelloWorld@@UEBAPEBUQMetaObject@@XZ)

1 个答案:

答案 0 :(得分:1)

使用以下标志时:

LIBS += -L"$$OUT_PWD/../HelloWorld/release"
LIBS += -lHelloWorld

您必须拥有已编译的动态或静态库。因此,您必须创建一个生成库的项目。在下一部分中,我将向您展示如何创建动态库。

<强> HelloWorldLib.pro

#-------------------------------------------------
#
# Project created by QtCreator 2017-01-06T12:37:49
#
#-------------------------------------------------

QT       -= gui

TARGET = HelloWorldLib
TEMPLATE = lib

DEFINES += HELLOWORLDLIB_LIBRARY

# The following define makes your compiler emit warnings if you use
# any feature of Qt which as been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

INCLUDEPATH += $$PWD/include

SOURCES += src/helloworldlib.cpp

HEADERS += include/helloworldlib.h\
        include/helloworldlib_global.h

unix {
    target.path = /usr/lib
    INSTALLS += target
}

DESTDIR = $$PWD/lib

包含/ helloworldlib.h

#ifndef HELLOWORLDLIB_H
#define HELLOWORLDLIB_H

#include "helloworldlib_global.h"

#include <QDebug>

class HELLOWORLDLIBSHARED_EXPORT HelloWorldLib: public QObject
{
    Q_OBJECT

public:
    HelloWorldLib(){

    }
    static bool returnTrue()
    {
        return true;
    }

public slots:
    void someSlot()
    {
        qDebug() << "test";
    }
};

#endif // HELLOWORLDLIB_H

包含/ helloworldlib_global.h

#ifndef HELLOWORLDLIB_GLOBAL_H
#define HELLOWORLDLIB_GLOBAL_H

#include <QtCore/qglobal.h>

#if defined(HELLOWORLDLIB_LIBRARY)
#  define HELLOWORLDLIBSHARED_EXPORT Q_DECL_EXPORT
#else
#  define HELLOWORLDLIBSHARED_EXPORT Q_DECL_IMPORT
#endif

#endif // HELLOWORLDLIB_GLOBAL_H

<强>的src / helloworldlib.cpp

#include "helloworldlib.h"

这里我展示了测试项目。

<强> HelloWorldTest.pro

#-------------------------------------------------
#
# Project created by QtCreator 2017-01-06T12:42:42
#
#-------------------------------------------------

QT += testlib
QT -= gui

# greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = tst_helloworldtesttest
CONFIG   += console
CONFIG   -= app_bundle

TEMPLATE = app

# The following define makes your compiler emit warnings if you use
# any feature of Qt which as been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0


SOURCES += tst_helloworldtesttest.cpp
DEFINES += SRCDIR=\\\"$$PWD/\\\"

win32:CONFIG(release, debug|release): LIBS += -L$$PWD/../HelloWorldLib/lib/release/ -lHelloWorldLib
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/../HelloWorldLib/lib/debug/ -lHelloWorldLib
else:unix: LIBS += -L$$PWD/../HelloWorldLib/lib/ -lHelloWorldLib

INCLUDEPATH += $$PWD/../HelloWorldLib/include
DEPENDPATH += $$PWD/../HelloWorldLib/include

<强> tst_helloworldtesttest.cpp

#include <QString>
#include <QtTest>

#include <helloworldlib.h>

#include <QDebug>

class HelloWorldTestTest : public QObject
{
    Q_OBJECT

public:
    HelloWorldTestTest();

private Q_SLOTS:
    void testCase1_data();
    void testCase1();
};

HelloWorldTestTest::HelloWorldTestTest()
{
}

void HelloWorldTestTest::testCase1_data()
{
    QTest::addColumn<QString>("data");
    QTest::newRow("0") << QString();
}

void HelloWorldTestTest::testCase1()
{
    QFETCH(QString, data);
    QVERIFY2(true, "Failure");

    HelloWorldLib hw;
    QVERIFY(hw.returnTrue());

}

QTEST_APPLESS_MAIN(HelloWorldTestTest)

#include "tst_helloworldtesttest.moc"

输出:

********* Start testing of HelloWorldTestTest *********
Config: Using QtTest library 5.7.1, Qt 5.7.1 (x86_64-little_endian-lp64 shared (dynamic) release build; by GCC 6.2.1 20160830)
PASS   : HelloWorldTestTest::initTestCase()
PASS   : HelloWorldTestTest::testCase1(0)
PASS   : HelloWorldTestTest::cleanupTestCase()
Totals: 3 passed, 0 failed, 0 skipped, 0 blacklisted, 0ms
********* Finished testing of HelloWorldTestTest *********

以下链接是完整的项目:https://github.com/eyllanesc/stackoverflow/tree/master/QtUnitTestingTest