LNK2019 With Qt and Gmock

LNK2019 With Qt and Gmock

本文关键字:Gmock and Qt With LNK2019      更新时间:2023-10-16

这几天我一直在用头撞墙。我正在尝试将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:googletestgooglemockinclude
INCLUDEPATH += D:googletestgoogletestinclude

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

Error   C2059   syntax error: '.'   AutoTests   ProjectDirtst_reptileprofile.cpp   63  

谢谢

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

TEMPLATE = lib

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

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

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

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

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

本教程不适用于googletest,但您可以类似地使用它。