QT 单元测试 moc "unresolved external symbol" for QMetaObject



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

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

我该如何解决这个问题?我在使用谷歌测试时也遇到了同样的问题。指南对此也没有帮助。我试过安装qt单元测试插件,结果是一样的。

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

我在windows上使用qt的静态构建,但我想这是无关紧要的。使用QtCreator作为IDE和NMAke构建。

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

有人知道我做错了什么?

这是单元测试。专业:

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)

当您使用以下标志时:

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

相关内容

最新更新