c++ - 为 qlibrary 在 dll 中编译 qobject 时出错
问题描述
我编写了一些用于动态加载 *.dll 文件的核心。作为主要工具,我使用 Qt 5.12 企业版。该 dll 必须通过 QLibrary 下载其他 qobject 继承类,以便在核心程序中连接和使用它,通过信号/插槽连接它。但是在编译 dll 时,应该通过 qlibrary 下载,我收到一条错误消息“vtable 的未定义引用,驱动程序接口”,我在堆栈溢出上看到了相同的问题,但其中的解决方案是尝试清理和重建项目,不不适合我。也许我应该包括其他 moc 文件,但是哪个?我使用 qmake 作为构建系统。所有纯虚方法都在继承类中重新定义。下面列出。
#ifndef DRIVER101104_H
#define DRIVER101104_H
#include "driver101-104_global.h"
#include "driverinterface.h"
#include "protocol101104wrapper.h"
#include <QObject>
#include <functional>
#include <QHash>
class Driver101104 : public DriverInterface
{
Q_OBJECT
public:
Driver101104();
virtual ~Driver101104();
// DriverInterface interface
public:
void setMessageAsString(const QString &_message);
QString getMessageAsString();
void initializeChannel(const QJsonObject& _parameters);
bool downloadTagsModel(const QHash<QString, std::shared_ptr<TagModel> > &_tagModels);
void setCallBackFunction(const QString &_nameOfCallBack, std::function<void (const QString &)> _callback);
private:
protocol101104Wrapper wrapper;
};
extern "C"{
DRIVER101104SHARED_EXPORT DriverInterface* createDriverClass(){
return new Driver101104();
}
}
#endif // DRIVER101104_H
#ifndef DRIVERINTERFACE_H
#define DRIVERINTERFACE_H
#include <QHash>
#include <memory>
#include <functional>
#include <QJsonObject>
#include <QObject>
class TagModel;
class DriverInterface : public QObject
{
Q_OBJECT
public:
DriverInterface() : QObject(nullptr){
}
virtual ~DriverInterface() {
}
/*This methods are for external type of driver connection!
* All other logick have to be In usual cases.*/
virtual void setMessageAsString(const QString& _message) = 0;
virtual QString getMessageAsString() = 0;
/*This method for type of external LinkAgent and external driver!*/
virtual void initializeChannel(const QJsonObject& _parameters) = 0;
virtual void setCallBackFunction(const QString& _nameOfCallBack,
std::function<void (const QString& _message)> _callBack) = 0;
/*Usual type of working, Device download tags model into it's own storage
* and serialize or deserialize it.*/
virtual bool downloadTagsModel(const QHash<QString, std::shared_ptr<TagModel>>& _tagModels) = 0;
};
#endif // DRIVERINTERFACE_H
#-------------------------------------------------
#
# Project created by QtCreator 2019-10-14T15:08:10
#
#-------------------------------------------------
QT -= gui
TARGET = Driver101-104
TEMPLATE = lib
DEFINES += DRIVER101104_LIBRARY
# The following define makes your compiler emit warnings if you use
# any feature of Qt which has 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
include(LibHeaders.pri)
include(LibSources.pri)
CONFIG += c++11
SOURCES += \
callbacknames.cpp \
driver101104.cpp \
protocol101104wrapper.cpp \
servicenames.cpp
HEADERS += \
callbacknames.h \
driver101104.h \
driver101-104_global.h \
driverinterface.h \
enums.h \
protocol101104wrapper.h \
servicenames.h
unix {
target.path = /usr/lib
INSTALLS += target
}
#include "driver101104.h"
Driver101104::Driver101104():DriverInterface()
{
}
Driver101104::~Driver101104()
{
}
void Driver101104::setMessageAsString(const QString &_message)
{
}
QString Driver101104::getMessageAsString()
{
}
/**
* @brief Driver101104::initializeChannel
* @param _parameters
*/
void Driver101104::initializeChannel(const QJsonObject& _parameters)
{
wrapper.channelInitializer(_parameters);
}
/**
* @brief Driver101104::downloadTagsModel
* Here should be something that return code
* of error for this program
* @param _tagModels
* @return
*/
bool Driver101104::downloadTagsModel(const QHash<QString, std::shared_ptr<TagModel> >& _tagModels)
{
return true;
}
void Driver101104::setCallBackFunction(const QString &_nameOfFunction, std::function<void (const QString&)> _callback)
{
wrapper.setCallBackFunction(_nameOfFunction,_callback);
}
解决方案
我找到了这个问题的答案,我重写了driverInterface和driver01104的类,之后,一切都正确了。库已加载并正常工作。仅与信号和插槽的连接不起作用。
#ifndef DRIVER101104_H
#define DRIVER101104_H
#include <QObject>
#include <functional>
#include <QHash>
#include "driver101-104_global.h"
#include "protocol101104wrapper.h"
#include "driverinterface.h"
class DRIVER101104SHARED_EXPORT Driver101104 : public DriverInterface
{
Q_OBJECT
public:
Driver101104(QObject* parent = nullptr);
virtual ~Driver101104();
public:
void initializeChannel(const QJsonObject &_parameters);
void setCallBackFunction(const QString &_nameOfCallBack,
std::function<void (const QString &)> _callback);
bool downloadTagsModel(const QHash<QString,
std::shared_ptr<TagModel> > &_tagModels);
void setMessageAsString(const QString &_message);
QString getMessageAsString();
private:
protocol101104Wrapper* wrapper;
};
extern "C"{
DRIVER101104SHARED_EXPORT DriverInterface* createDriverClass();
}
#endif // DRIVER101104_H
#ifndef DRIVERINTERFACE_H
#define DRIVERINTERFACE_H
#include <QObject>
#include <functional>
#include <QString>
#include <memory>
#include <QHash>
#include <QJsonObject>
class TagModel;
class DriverInterface : public QObject
{
Q_OBJECT
public:
DriverInterface(QObject* parent = nullptr): QObject(parent){
}
virtual ~DriverInterface(){
}
virtual void setMessageAsString(const QString& _message) = 0;
virtual void initializeChannel(const QJsonObject& _parameters) = 0;
virtual void setCallBackFunction(const QString &_nameOfCallBack,
std::function<void (const QString &)> _callback) = 0;
virtual bool downloadTagsModel(const QHash<QString,
std::shared_ptr<TagModel> >& _tagModels) = 0;
virtual QString getMessageAsString() = 0;
signals:
void callBackMessage(QJsonObject _message);
};
#endif // DRIVERINTERFACE_H
推荐阅读
- java - 如何在scrollView.setOnScrollChangeListener中获取scrollY的最大值
- python - 从 HTTP 响应到 csv
- c# - 如何将 Matrix 转换为 Mat 类型?
- python - 向独立进程发送信号/事件
- snakemake - 使用 snakemake 打印简化的 DAG 图
- python - 从python中的多个文件夹加载多个图像
- python-3.x - Python 3 上下文管理器模拟单元测试
- rxjs - RxJS 6 /点击运算符何时发出值
- node.js - 使用打字稿配置本机反应时出错
- excel - VBA循环结合Lastrow并查找空白值