目录
引言
软件发布需要拷贝对应的依赖库,windows可以使用qt自带的windeployqt,linux也有对应的开源工具linuxdeployqt
大致原理是通过命令行诸如ldd,找到可执行文件的对应依赖库的路径,拷贝至对应路径下的lib文件夹中。
下载并编译
由于使用的是高版本的Ubuntu, 已编译好的包不能正常使用(诸如执行linuxdeployqt后原文件被修改的情况),最后选择编译源代码解决,还可以修改源码满足特定需求
1.下载linuxdeployqt
下载地址:https://github.com/probonopd/linuxdeployqt/releases
帮助文档:https://github.com/probonopd/linuxdeployqt/blob/master/README.md
2.编译linuxdeployqt
解压后打开linuxdeployqt.pro点击编译,在当前目录下的bin文件夹中就生成对应的linuxdeploy,为了能够正常使用需要做如下修改。
2.1去除版本限制
找到tools/linuxdeployqt/main.cpp, 注释如下代码:
// openSUSE Leap 15.0 uses glibc 2.26 and is used on OBS
if (strverscmp (glcv, "2.27") >= 0) {
qInfo() << "ERROR: The host system is too new.";
qInfo() << "Please run on a system with a glibc version no newer than what comes with the oldest";
qInfo() << "currently still-supported mainstream distribution (xenial), which is glibc 2.23.";
qInfo() << "This is so that the resulting bundle will work on most still-supported Linux distributions.";
qInfo() << "For more information, please see";
qInfo() << "https://github.com/probonopd/linuxdeployqt/issues/340";
return 1;
}
2.2去除找不到库就不继续执行限制
原代码中如果遇到找不到库文件就会警告并停止,本人实际使用时是希望将能找到的拷贝完成,所以修改了对应代码,这个看个人需求进行修改。
找到tools/linuxdeployqt/shared.cpp进行如下修改:
if ((outputLine.contains("not found")) && (qtDetectionComplete == 1)){
LogWarning() << "ldd outputLine:" << outputLine.replace("\t", "");
//LogError() << "for binary:" << binaryPath;
//LogError() << "Please ensure that all libraries can be found by ldd. Aborting.";
//exit(1);
outputLines.removeAll(outputLine);
}
提示哪些库未找到,不再直接退出,而从outputlines中移除并继续执行。
使用
1.设置环境变量
修改~/.bashrc:
vim ~/.bashrc
将qt的对应路径加入在末尾:
export PATH=/opt/Qt5.5.1/5.5/gcc_64/bin:$PATH
export LD_LIBRARY_PATH=/opt/Qt5.5.1/5.5/gcc_64/lib:$LD_LIBRARY_PATH
export QT_PLUGIN_PATH=/opt/Qt5.5.1/5.5/gcc_64/plugins:$QT_PLUGIN_PATH
export QML2_IMPORT_PATH=/opt/Qt5.5.1/5.5/gcc_64/qml:$QML2_IMPORT_PATH
2.安装pathchelf工具
sudo apt install patchelf
如果未安装直接执行拷贝会有如下提示:
ERROR: ldd outputLine: "libjasper.so.1 => not found"
ERROR: for binary: "/home/xl/Qt5.9.2/5.9.2/gcc_64/plugins/imageformats/libqjp2.so"
ERROR: Please ensure that all libraries can be found by ldd. Aborting.
3.执行打包
将编译好的linuxdeployqt放在 /usr/local/bin/ 目录底下,这样可以在命令行中直接调用
放置完成后,在可执行文件目录下,执行如下代码:
linuxdeployqt 可执行文件名 -appimage
appimage是执行参数,见说明文档: