LNK2019 与 Qt 和 Gmock

LNK2019 With Qt and Gmock

在过去的几天里,我一直在用头撞墙。我正在尝试将 googletest 的 gmock 库合并到我的 Qt Autotest 子目录项目中,但一直收到以下链接器错误,我不确定如何解决此问题。主应用程序编译并运行得很好。

tst_reptileprofile.obj : error LNK2019: unresolved external symbol "public: __thiscall DashboardWidget::DashboardWidget(void)" (??0DashboardWidget@@QAE@XZ) referenced in function "private: void __thiscall TestReptileProfile::init(void)" (?init@TestReptileProfile@@AAEXXZ)

这里是测试代码:

#include <QtTest/QtTest>
#include <QCoreApplication>
#include <QObject>
#include "gmock/gmock.h"

#include "../Application/dashboardwidget.h"

class TestReptileProfile : public QObject
{
    Q_OBJECT

public:
    TestReptileProfile() {}
    ~TestReptileProfile() {}

private slots:
    void initTestCase()
    {
    }
    void cleanupTestCase()
    {
    }

    void init()
    {
        dashboard_ = new DashboardWidget();
    }

    void cleanup()
    {
        delete dashboard_;
    }

private:
    DashboardWidget* dashboard_;

};

#include "tst_reptileprofile.moc"
QTEST_MAIN(TestReptileProfile)

DashboardWidget.h/.cpp

#pragma once

#include <QtWidgets/QWidget>
#include <QtWidgets/QAbstractButton>

namespace Ui {
class DashboardWidget;
}

class DashboardWidget : public QWidget
{
    Q_OBJECT

public:
    explicit DashboardWidget();
    ~DashboardWidget();
    QAbstractButton* openProfileButton();

private:
    Ui::DashboardWidget *ui;
    QAbstractButton* openProfileButton_;
};

#include "dashboardwidget.h"
#include "ui_dashboardwidget.h"

DashboardWidget::DashboardWidget() :
    ui(new Ui::DashboardWidget)
{
    ui->setupUi(this);
    openProfileButton_ = ui->openReptileProfilePageButton;
}

DashboardWidget::~DashboardWidget()
{
    delete ui;
}

QAbstractButton* DashboardWidget::openProfileButton()
{
    return openProfileButton_;
}

子目录项目.pro

TEMPLATE = subdirs

CONFIG += ordered

SUBDIRS += \
    Application \
    AutoTests

AutoTests.depends = Application

Application.pro

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = Application
TEMPLATE = app

DEFINES += QT_DEPRECATED_WARNINGS

SOURCES += \
        main.cpp \
        mainview.cpp \
        . . .
    mainpresenter.cpp \
    dashboardwidget.cpp \


FORMS += \
        mainview.ui \
    i_mainpresenter.h \
    dashboardwidget.ui \

HEADERS += \
        mainview.h \
    dashboardwidget.h \

AutoTests.pro

QT += testlib
QT += gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

CONFIG += qt warn_on depend_includepath testcase

TEMPLATE = app

SOURCES += \ 
    tst_reptileprofile.cpp

unix|win32: LIBS += -L$$PWD/../../../googletest/googlemock/msvc/2015/Win32-Release/ -lgmock

INCLUDEPATH += $$PWD/../../../googletest/googlemock/msvc/2015/Win32-Release
DEPENDPATH += $$PWD/../../../googletest/googlemock/msvc/2015/Win32-Release

INCLUDEPATH += D:\googletest\googlemock\include\
INCLUDEPATH += D:\googletest\googletest\include\

我还尝试将项目转换为 Visual Studio 项目,但导致编译错误。

Error   C2059   syntax error: '.'   AutoTests   ProjectDir\tst_reptileprofile.cpp   63  

谢谢

有两点。第一件事:您正在创建两个可执行文件(应用程序和自动测试)。要测试您的生产代码,您必须将其构建到一个库中。

TEMPLATE = lib

这意味着,应该创建两个可执行文件,用于测试和应用程序。可执行文件需要针对新库进行 linked。 例如:

LIBS += -L../lib/debug -llib

最后你有三个子目录和.pro-files。

第二件事:如果在 linking 库时创建和导入库,则必须导出库中的符号。这可以通过预处理器定义来完成:TEST_COMMON_DLLSPEC.

您可以在以下 link 中找到完整的操作方法: https://wiki.qt.io/How_to_create_a_library_with_Qt_and_use_it_in_an_application

本教程未与 googletest 一起使用,但您可以类似地使用它。