qt 如何制作动态库插件
创始人
2024-11-18 08:07:39
0

首先

首先第一点要确定我们的接口是固定的,也就是要确定

#ifndef RTSPPLUGIN_H #define RTSPPLUGIN_H #include "rtspplugin_global.h" typedef void (*func_callback)(uint8_t* data,                               int len,                               uint32_t ssrc,                               uint32_t ts,                               const char* ipfrom,                               uint16_t fport); extern "C" { RTSPPLUGIN_EXPORT const char* func_name(); RTSPPLUGIN_EXPORT int func_no(); //类型,接入类型? RTSPPLUGIN_EXPORT int func_access(); RTSPPLUGIN_EXPORT void* create(const char* ip, uint16_t port, func_callback callback); RTSPPLUGIN_EXPORT int  start(void *server); RTSPPLUGIN_EXPORT void stop(void* server); } #endif // RTSPPLUGIN_H 

使用extern “C” 来保持接口能被正确的找到函数名

rtspplugin_global.h 文件定义宏

#include   #if defined(RTSPPLUGIN_LIBRARY) #  define RTSPPLUGIN_EXPORT Q_DECL_EXPORT #else #  define RTSPPLUGIN_EXPORT Q_DECL_IMPORT #endif 

文件实现如下,需要定义一个结构体,将需要保存的数据结构都放在里面

#include "rtspplugin.h" #include  extern "C" { #include  #include  #include  #include  #include  #include  #include  #include   }  typedef struct rtsplugin {     struct SwsContext* img_convert_ctx = NULL;     AVFormatContext* input_ctx = NULL;     int video_stream, ret;     AVStream* video = NULL;     //AVCodecContext* decoder_ctx = NULL;     //AVCodec* decoder = NULL;     AVPacket packet;     enum AVHWDeviceType type;     std::string url; }rtsplugin; const char* func_name() {     return "rtspclient"; } int func_no() {     return 10; } void* create(const char* ip, uint16_t port, func_callback callback) {     rtsplugin* pi = new rtsplugin();     return pi; } int  start(void *client) {     return 0; } void stop(void* client) { } 

qt的pro文件如下

QT -= gui #QT += core gui widgets TEMPLATE = lib DEFINES += RTSPPLUGIN_LIBRARY  CONFIG += c++11  # The following define makes your compiler emit warnings if you use # any Qt feature that has been marked 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 INCLUDEPATH += ../VT_qt/win64/ DESTDIR = ../ # You can also make your code fail to compile if it uses 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  SOURCES += \     rtspplugin.cpp  HEADERS += \     rtspplugin_global.h \     rtspplugin.h   win32{     DEFINES += WIN32_LEAN_AND_MEAN     LIBS += -L../VT_qt/win64/     DESTDIR = ../VT_qt/bin64/       ## FFmpeg     LIBS += -lavformat      \             -lavdevice      \             -lavcodec       \             -lswresample    \             -lswscale       \             -lavutil        \             -lopencv_world450d    }  # Default rules for deployment. unix {     target.path = /usr/lib } !isEmpty(target.path): INSTALLS += target 

编译完成,正确生成 动态库
在这里插入图片描述

调用

生成exe文件
pro文件如下

TEMPLATE = app CONFIG += console c++11 CONFIG -= app_bundle    win32{     DEFINES += WIN32_LEAN_AND_MEAN     DESTDIR = ../VT_qt/bin64/ }  #CONFIG -= qt QT       += core  SOURCES += \         main.cpp 
#include  #include  #include   using namespace std; typedef void(*func_callback)(uint8_t* data,     int len,     uint32_t ssrc,     uint32_t ts,     const char* ipfrom,     uint16_t fport);  typedef  const char* (*func_name)(); typedef int (*func_no)(); typedef int (*func_access)(); //接入型插件,必须create, start stop typedef void* (*create)(const char* ip, uint16_t port, func_callback callback); typedef int (*start)(void *server); typedef void (*stop)(void* server); int main(int argc, char *argv[]) {     QCoreApplication a(argc, argv);      QString library_path = QCoreApplication::applicationDirPath() + "/rtspplugin.dll";     QLibrary* L = new QLibrary(library_path);      if( nullptr == L) return -1;      if(! L->load()){         qDebug() << "[error:]" << L->errorString();         delete L;         L = NULL;     }      func_name funcname = (func_name)L->resolve("func_name");     const char* name = funcname();     qDebug()<resolve("func_no");     int no = funcno();     qDebug()<unload();      return a.exec();  } 

只是个示例,不要忘了释放,释放要在动态库里面释放,

主要的申请和释放改一下

void* create(const char* url, func_callback callback) {     PluginContext* con = (PluginContext*) malloc(sizeof(PluginContext));     con->url = url;     return con; } void Release(void* context) {     free(context); } 

ok, 成功执行,没有问题
在这里插入图片描述

相关内容

热门资讯

长期以来!反杀新火神辅助!竟然... 长期以来!反杀新火神辅助!竟然真的是有开挂辅助方法(发现有挂)-哔哩哔哩1、玩家可以在反杀新火神辅助...
窍门辅助!打哈儿床将作必弊(辅... 窍门辅助!打哈儿床将作必弊(辅助)原来有辅助攻略(哔哩哔哩)1、上手简单,内置详细流程视频教学,新手...
规律辅助挂!四川家园茶馆辅助!... 规律辅助挂!四川家园茶馆辅助!一直确实有开挂辅助攻略(有挂神器)-哔哩哔哩1、打开软件启动之后找到中...
阶段辅助!川娱竞技血战辅助器(... 阶段辅助!川娱竞技血战辅助器(辅助)都是是有辅助插件(哔哩哔哩)川娱竞技血战辅助器脚本下载中分为三种...
据悉!欢聚水鱼怎么破解!总是确... 据悉!欢聚水鱼怎么破解!总是确实有开挂辅助教程(有挂技术)-哔哩哔哩1、该软件可以轻松地帮助玩家将欢...
绝活辅助!小南四川长牌辅助(辅... 绝活辅助!小南四川长牌辅助(辅助)其实真的是有辅助攻略(哔哩哔哩)小南四川长牌辅助透视方法中分为三种...
第三方插件!蜜瓜大厅辅助下载!... 第三方插件!蜜瓜大厅辅助下载!一贯是有开挂辅助教程(有挂总结)-哔哩哔哩蜜瓜大厅辅助下载破解侠是真的...
要领辅助!心悦透明器看手机纸牌... 要领辅助!心悦透明器看手机纸牌(辅助)好像有辅助软件(哔哩哔哩)心悦透明器看手机纸牌辅助器是一种具有...
最新消息!新上游透视辅助!竟然... 最新消息!新上游透视辅助!竟然存在有开挂辅助神器(有挂技术)-哔哩哔哩1、用户打开应用后不用登录就可...
技法辅助!奇迹脚本辅助器免费(... 技法辅助!奇迹脚本辅助器免费(辅助)一直有辅助app(哔哩哔哩)1、任何奇迹脚本辅助器免费透视是真的...