Linux+qt:创建动态库so,以及如何使用(详细步骤)

目录

[1、根据安装Qt Creator的向导进行创建](#1、根据安装Qt Creator的向导进行创建)

2、开发动态库注意的一些细节

3、给动态库添加一个对外开放的接口文件

[4、了解下Qt的 .pri文件(非常实用)](#4、了解下Qt的 .pri文件(非常实用))

5、如何调用动态库.so


1、根据安装Qt Creator的向导进行创建

(1)选择"Library"->"C++ Library"

(2)编写上动态库名字; (3)选择需要的编译方式:(本文选择的是:gcc编译的方式)

(4)选择动态库所需要的模块:

注意:其实这里不选,后面也可以在.pro文件里添加的。

(5)填写动态库的名字

(6)点击完成即可:

2、开发动态库注意的一些细节

QT -= gui // 添加需要的模块

TARGET = testDll // 生成动态库的名字

CONFIG += plugin // 如果不加,可能生成的动态库为:libtestDll.so.1.0.0

DESTDIR = ../bin/dll

LIBS += -L../bin/dll -lrt

3、给动态库添加一个对外开放的接口文件

例如:添加一个testApi的文件,想向外面暴露哪些接口,可以在这里表示

(1)头文件

cpp 复制代码
#ifndef TESTAPI_H
#define TESTAPI_H

#include <QString>
using namespace std;

//定义宏
#if defined(__cplusplus)
#define D_EXTERN_C extern "C"
#else
#define D_EXTERN_C
#endif

#define D_CALLTYPE
#define D_DECL_EXPORT   __attribute__((visibility("default")))
#define D_DECL_IMPORT   __attribute__((visibility("default")))


//向外暴露2个接口
D_EXTERN_C QString D_CALLTYPE printfAppPath();

D_EXTERN_C QString D_CALLTYPE printfCurrentPath();

#endif // TESTAPI_H

(2)源文件

具体实现,可以在TestDll中进行功能的是实现。

cpp 复制代码
#include "testapi.h"
#include "testdll.h"

QString printfAppPath()
{
    return TestDll::getInstance()->printfAppPath();
}

QString printfCurrentPath()
{
    return TestDll::getInstance()->printfCurrentPath();
}
4、了解下Qt的 .pri文件(非常实用)

(1).pri文件功能:把额外用到的一些自定义组件放在一个文件中,方便调用,使用的时候采用include引用即可。

(2)具体创建流程:

**1)**打开项目文件夹,在这里面新建一个文件夹(名为demo);

**2)**在demo文件夹下新建一个pri文件(名为demo)。(怎么新建?可以新建一个文本文件,然后重命名为demo.pri);

**3)**打开Qt Creator ,在你的项目的Pro文件中加入相关的 .pri文件

写完保存后,自动刷新,.pri文件会自动导入到你的工程下。

include(General/general.pri)

INCLUDEPATH += -I $$PWD/General

**4)**然后可以在demo文件中添加Headers,Sources,Resources等文件~,如此一来下次使用就直接引用这个demo文件的内容即可。

HEADERS += \

$$PWD/TimerManager.h \

$$PWD/Singleton.h

SOURCES += \

$$PWD/TimerManager.cpp

5、如何调用动态库.so

(1)在调用的地方进行,动态库接口声明:

cpp 复制代码
//动态库接口声明
typedef QString (*TYPE_printfAppPath)();
typedef QString (*TYPE_printfCurrentPath)();


// 声明一个变量
TYPE_printfAppPath          printfAppPath;
TYPE_printfCurrentPath      printfCurrentPath;


//加载动态库
bool loadLibrary(const QString& appPath);

(3)源文件:

实现加载动态库,以及如何使用这个接口。

cpp 复制代码
bool loadLibrary(const QString& appPath)
{
    QLibrary *m_pLibTest;
    
    //按照实际动态库所在的目录
    QString strLibFile = appPath + "/dll/libtestDll.so";

    if (QFile::exists(strLibFile))
        m_pLibTest = new QLibrary(strLibFile);
    else {
        slotAppendText(strLibFile + " don't exists");
        return false;
    }

    if(!m_pLibTest->load())
    {
        QString strErrMsg = m_pLibTest->errorString();
        slotAppendText(strLibFile + " load failed: " + m_pLibTest->errorString());
        return false;
    }
    else
    {
        slotAppendText(strLibFile + " load success");
    }


    printfAppPath    = reinterpret_cast<TYPE_printfAppPath>(m_pLibTest->resolve("printfAppPath"));
    printfCurrentPath    = reinterpret_cast<TYPE_printfCurrentPath>(m_pLibTest->resolve("printfCurrentPath"));


    if(!printfAppPath)
    {
        slotAppendText("printfAppPath not resolve");
        return false;
    }

    if(!printfCurrentPath)
    {
        slotAppendText("printfCurrentPath not resolve");
        return false;
    }

    QString strAppPath = printfAppPath();
    QString strCurrentPath = printfCurrentPath();

    slotAppendText("strAppPath : " + strAppPath);
    slotAppendText("strCurrentPath : " + strCurrentPath);

    return true;

}
相关推荐
隐积21 分钟前
Qt魔法之旅 · 全系列课程导航
qt
Byron Loong1 小时前
【C++】重定向是什么
开发语言·c++
techdashen1 小时前
Go设计取舍之六: sync.Mutex正常模式与饥饿模式
开发语言·后端·golang
TunerT_TQ2 小时前
【智能体安全治理|专栏第9期】从“一堆规则”到“数字宪法”:智能体治理的下一个阶段
java·开发语言·安全·开源治理·大模型安全·ai基础设施·智能体安全
Aurorar0rua2 小时前
CS50 x 2024 Notes Algorithms - 07
c语言·开发语言·学习方法
程序员-Benothing2 小时前
Java ForkJoinPool 详解:从分治思想到高性能并行计算
java·开发语言·后端·面试·职场和发展
脚踏实地皮皮晨2 小时前
003006001_WrapPanel控件
开发语言·c#·.net·wpf·visual studio
上海安当技术3 小时前
单点登录 SSO 怎么选协议?SAML 2.0 / OAuth 2.0 / OIDC / CAS 对比与 ERP、OA、CRM 接入实战
java·开发语言
不才不才不不才3 小时前
Spring 源码系列(14): JDK 动态代理 vs CGLIB,以及拦截器责任链
java·开发语言·spring
mifengxing3 小时前
计算机组成原理——存储器系统
开发语言·考研·计算机组成原理·复习笔记·计算机408