LNK2019 With Qt and 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: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,但您可以类似地使用它。
相关文章:
- C++核心准则 C35 对于接口类"A base class destructor should be either public and virtual, or protected and nonv
- 为什么C++逐位AND运算符在不同大小的操作数中表现为这样
- 为什么 Clang 不允许"and"作为函数名称?
- 位阵列上的快速AND运算
- 是否可以在 C++03 中定义'move-and-swap idiom'等效项
- BoostPython and CMake
- OpenSSL BIO and SSL_read
- Gurobi GRBModel and GRBmodel in C++
- 使用 gmock c++ 在真实对象上调用方法
- std::visit and std::variant usage
- SHBrowseForFolder with BIF_BROWSEFORCOMPUTER and SHGetPathFr
- Directx12 and keystrokes
- different between int **arr =new int [ n]; and int a[i][j]?
- C++ getenv and setenv
- gmock / google-mock发出警告,并且通过模拟异常未通过测试
- Inference pytorch C++ with alexnet and cv::imread image
- Visual Studio 2019 C++ and std::filesystem
- 保证逻辑 AND 表达式中的函数调用
- Installing GTest and GMock
- LNK2019 With Qt and Gmock