gpt4 book ai didi

c++ - QMetaObject 的 QT 单元测试 moc "unresolved external symbol"

转载 作者:行者123 更新时间:2023-11-28 05:20:58 25 4
gpt4 key购买 nike

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

我可以正常运行模拟测试(不使用我项目的类)并正常运行应用程序。但是如果我从项目中实例化对象,我会得到一个未解析的 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)

最佳答案

当您使用以下标志时:

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

include/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

include/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

关于c++ - QMetaObject 的 QT 单元测试 moc "unresolved external symbol",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41510411/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com