在 dll 中为 qLibrary 编译 qobject 时出错

Error while compiling qobject in dll for qlibrary

本文关键字:qobject 出错 编译 qLibrary dll 中为      更新时间:2023-10-16

我写了一些核心来动态加载*.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