我正在尝试为我的应用程序编写单元测试。第一次尝试时,由于未定义的符号,我的单元测试构建无法链接。ld.lld: 错误:未定义符号:AnalysisEngine::
我正在尝试为我的应用程序编写单元测试。第一次尝试时,由于未定义的符号,我的单元测试构建无法链接。
ld.lld: error: undefined symbol: AnalysisEngine::diagnosisDetected(Diagnosis)
>>> referenced by ../code/core/src/analysisengine.cpp:18
>>> ../code/core/src/analysisengine.cpp.obj:(AnalysisEngine::parseBuffer())
>>> referenced by ../code/core/src/analysisengine.cpp:22
>>> ../code/core/src/analysisengine.cpp.obj:(AnalysisEngine::parseBuffer())
>>> referenced by ../code/core/src/analysisengine.cpp:26
>>> ../code/core/src/analysisengine.cpp.obj:(AnalysisEngine::parseBuffer())
ld.lld: error: undefined symbol: AnalysisEngine::staticMetaObject
>>> referenced by ../code/core/src/analysisengine.cpp.obj:(.refptr._ZN14AnalysisEngine16staticMetaObjectE)
ld.lld: error: undefined symbol: vtable for AnalysisEngine
>>> referenced by ../code/core/src/analysisengine.cpp.obj:(.refptr._ZTV14AnalysisEngine)
我的单元测试依赖于 AnalysisEngine() 类并对其进行了实例化。在我的普通应用程序中,它可以很好地链接和构建,但由于某种原因,信号出现了问题 diagnosisDetected
。链接器为什么找不到它?(所有相关代码如下)
.h
#ifndef ANALYSISENGINE_H
#define ANALYSISENGINE_H
#include <QObject>
#include <QTimer>
#include "framebuffer.h"
#include "diagnosis.h"
class AnalysisEngine : public QObject {
Q_OBJECT
public:
explicit AnalysisEngine(QObject *parent = nullptr);
bool flatliningCheck();
bool tachycardiaCheck();
bool arrhythmiaCheck();
signals:
void diagnosisDetected(Diagnosis diagnosis);
private:
QTimer analysisTimer;
FrameBuffer* buffer;
void parseBuffer();
};
#endif // ANALYSISENGINE_H
.cpp的相关部分
#include "analysisengine.h"
#include <QDebug>
#include <cmath>
AnalysisEngine::AnalysisEngine(QObject *parent)
: QObject(parent)
{
connect(&analysisTimer, &QTimer::timeout, this, &AnalysisEngine::parseBuffer);
analysisTimer.start(3000); // Run analysis every two seconds
}
void AnalysisEngine::parseBuffer() {
QMutexLocker locker(buffer->lock());
if (flatliningCheck()) {
qDebug() << "Diagnosis Detected: FLATLINE";
emit diagnosisDetected(Diagnosis::FLATLINING);
}
if (tachycardiaCheck()) {
qDebug() << "Diagnosis Detected: TACHYCARDIA";
emit diagnosisDetected(Diagnosis::TACHYCARDIA);
}
if (arrhythmiaCheck()) {
qDebug() << "Diagnosis Detected: ARRHYTHMIA";
emit diagnosisDetected(Diagnosis::ARRHYTHMIA);
}
}
diagnostic.h 包含信号中使用的枚举,并包含在 analysisengine.h 中
#ifndef DIAGNOSIS_H
#define DIAGNOSIS_H
enum class Diagnosis {
FLATLINING,
TACHYCARDIA,
ARRHYTHMIA
};
#endif // DIAGNOSIS_H
我的单元测试(Qtest)cmakelists.txt:
cmake_minimum_required(VERSION 3.5)
project(CoreUnitTests LANGUAGES CXX)
enable_testing()
find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Gui Test)
find_package(Qt6 REQUIRED COMPONENTS Gui Test)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
add_executable(CoreUnitTests tst_TestAnalysisEngine.cpp
test_analysisengine.h
../../code/core/src/analysisengine.cpp
../../code/core/src/framebuffer.cpp
../../code/core/inc/diagnosis.h)
add_test(NAME TestAnalysisEngine COMMAND TestAnalysisEngine)
target_include_directories(CoreUnitTests PRIVATE
../../code/core/inc # Add the core headers to the include path
./
)
target_link_libraries(CoreUnitTests PRIVATE Qt6::Gui Qt6::Test)