目录
1、QDir::currentPath()
2、QAppllication::appllicationDirPath()
3、获取.so自身的路径(利用dladdr)
Qt中,也有相关的接口获取程序的相关路径的。
先了解下相关的接口:
1、QDir::currentPath()
(1)在Linux系统下,在Qt Creator的编译器里面直接点击运行时候,这个路径是这个编译器所在的位置:
/opt/Qt5.7.1/Tools/QtCreator/bin
(2)要是在终端直接运行这个程序的话,这个路径是当前程序所在的位置:
2、QAppllication::appllicationDirPath()
而无论你通过何种途径去运行exe文件,QAppllication::appllicationDirPath()的路径始终都是exe文件所在的绝对路径。 因此,如果想要获取固定的exe路径信息,还是建议使用QAppllication::appllicationDirPath()
3、获取.so自身的路径(利用dladdr)
上面2种方法都不能实现获取.so自身的路径,故另辟蹊径。
#include <QDebug>
#include <dlfcn.h>void getCurrentDllPath()
{return;
}void Func()
{Dl_info info;//dladdr获取某个地址的符号信息int rc = dladdr((void*)getCurrentDllPath, &info);if (!rc){QString strError = QString("Problem retrieving program information for %1").arg(dlerror());qDebug() << __FUNCTION__ << strError;return;}QString strPath = "";//info.dli_fname是当前动态库的路径:还带有当前动态库的名字strPath = info.dli_fname;// 主要是把当前的动态库名字部分去掉,只需要当前目录的路径strPath = strPath.mid(0, strPath.lastIndexOf("/"));qDebug() << "Current path : " << strPath;
}