C++ QT单元测试moc“;未解析的外部符号“;对于QMetaObject

C++ QT单元测试moc“;未解析的外部符号“;对于QMetaObject,c++,qt,unit-testing,C++,Qt,Unit Testing,我第一次尝试将单元测试添加到我的项目中 我可以正常运行模拟测试(无需使用项目的类),也可以正常运行应用程序。但是如果我从项目中实例化对象,我会得到QMetaObject的一个未解析的外部符号。如果我没记错的话,这意味着项目中没有包含对象的moc 我该如何解决这个问题?我使用谷歌测试也有同样的问题。这本指南在这方面也没有帮助。我试过安装qt单元测试插件,结果也是一样 我上传了一个模拟项目,其结构与我在上述项目中使用的结构相同,请在此处获取: 我在windows上使用的是qt的静态构建,但我想这是不

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

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

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

我上传了一个模拟项目,其结构与我在上述项目中使用的结构相同,请在此处获取:

我在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
#-------------------------------------------------
#
# 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
包括/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
#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
这里我展示了测试项目

HelloWorldTest.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
#-------------------------------------------------
#
# 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_helloworldtest.cpp

#include "helloworldlib.h"
#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"

以下链接中是完整的项目:

我已经查看了您的项目,我知道您要测试的项目是HelloWorld,在该项目中有两个类:HelloWorld和MainWindow。我不明白您为什么要链接尚未创建的库。除此之外,HelloWorld类从未被使用过。您到底想测试什么(HelloWorld类还是MainWindow类)?在这个模拟示例中,
HelloWorld
。我正在测试函数
returnsTrue
,它确实返回
true
。构建该项目会创建HelloWorld.lib,尽管我不确定单元测试是否需要它,因为源代码仍然存在。如果有必要构建库,因为在.pro中是LIBS+=-lHelloWorld编写的。我已经建立了测试在一瞬间,我将上传代码。在链接是解决方案:感谢完整的解决方案!比我想象的要多。我还有一个未定义的符号,好像它忽略了lib。输出错误:我必须添加一个win开关,使库在lib/release(参见gist末尾)处着陆,否则它甚至找不到它。此外,我还收到一条警告信息
helloworldlib.obj:warning LNK4221:此对象文件未定义任何以前未定义的公共符号,因此当我仅生成库时,任何使用此库的链接操作都不会使用它。@quimnus更新我的解决方案。将动态库更改为静态库:非常好!这很有效。我将在周一学习它,并使它适应我的项目。回答得很好!