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

img
img

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以添加戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

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

D_EXTERN_C QString D_CALLTYPE printfCurrentPath();

#endif // TESTAPI_H


(2)源文件


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



#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);


![](https://img-blog.csdnimg.cn/98a818a2a7e8482883eef2f9fa03a591.png)


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


![](https://img-blog.csdnimg.cn/0060c915815145418dc28c2fa6c1c2cd.png)


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


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



> 
> include(General/general.pri)
> 
> 
> INCLUDEPATH += -I $$PWD/General
> 
> 
> 


![](https://img-blog.csdnimg.cn/482d7c092579440d800548c5f5c6de60.png)


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



> 
> HEADERS += \  
>      $$PWD/TimerManager.h \  
>      $$PWD/Singleton.h
> 
> 
> SOURCES += \  
>      $$PWD/TimerManager.cpp
> 
> 
> 


![](https://img-blog.csdnimg.cn/473f54fc21ce414bb5bfb87378c197fe.png)



##### **5、如何调用动态库.so**


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



//动态库接口声明
typedef QString (*TYPE_printfAppPath)();
typedef QString (*TYPE_printfCurrentPath)();

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

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


(3)源文件:


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



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;

}





![img](https://img-blog.csdnimg.cn/img_convert/0ec9e974d15da83d28e3c707ca80c862.png)
![img](https://img-blog.csdnimg.cn/img_convert/86746a923dd4364d349a17db8f14bef7.png)

**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**

**[需要这份系统化的资料的朋友,可以添加戳这里获取](https://bbs.csdn.net/topics/618668825)**


**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**

入研究,那么很难做到真正的技术提升。**

**[需要这份系统化的资料的朋友,可以添加戳这里获取](https://bbs.csdn.net/topics/618668825)**


**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值