Ubuntu下使用linuxdeployqt打包Qt程序指南
开发Qt界面程序后,常面临将其移植至未安装Qt环境的计算机上运行的难题。经查阅资料得知,Windows平台有windowsdeployqt工具,而Linux平台则可使用linuxdeployqt来简化打包流程。
1. 配置Qt运行环境
首先需正确配置Qt环境,在~/.bashrc文件中添加相应的路径设置:
export PATH=/home/xl/Qt5.9.2/5.9.2/gcc_64/bin:$PATH
export LD_LIBRARY_PATH=/home/xl/Qt5.9.2/5.9.2/gcc_64/lib:$LD_LIBRARY_PATH
export QT_PLUGIN_PATH=/home/xl/Qt5.9.2/5.9.2/gcc_64/plugins:$QT_PLUGIN_PATH
export QML2_IMPORT_PATH=/home/xl/Qt5.9.2/5.9.2/gcc_64/qml:$QML2_IMPORT_PATH
其中/home/xl/Qt5.9.2/5.9.2/目录需根据实际安装的Qt路径进行调整。
随后执行sourec ~/.bashrc命令使上述配置立即生效。
2. 编译linuxdeployqt源码
该项目托管于 https://github.com/probonopd/linuxdeployqt.git 。
尽管官方提供了预编译包,但因笔者使用的是Ubuntu18系统,版本较高可能导致兼容性问题,故选择自行编译源码。
为防止预编译包因检测到系统版本过高而拒绝运行,需在编译前注释掉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;
}*/
接着使用cmake和make进行编译,生成的可执行程序位于tools/linuxdeployqt/linuxdeployqt。
为方便全局调用,可将该可执行程序复制到系统的/usr/local/bin/目录下。
3. 执行打包操作
将Qt编译生成的应用程序及相关文件拷贝至一个独立的文件夹中。
在该目录下执行命令:linuxdeployqt appname.
通常情况下打包会顺利完成,当前目录下会生成AppRun文件,直接运行即可启动应用。
但有时会遇到阻碍,通常是因为系统缺少必要的依赖库。例如遇到的错误如下:
ERROR: Could not start patchelf.
ERROR: Make sure it is installed on your $PATH.
ERROR: Error reading rpath with patchelf "libQt5Widgets.so" : ""
ERROR: Error reading rpath with patchelf "libQt5Widgets.so" : ""
此错误提示缺少patchelf工具,通过安装该工具即可解决:
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.
这表明系统缺少libqjp2.so所需的libjasper库。虽然本地能正常运行,但打包时仍报缺库。解决方法很直接,缺什么装什么:
sudo add-apt-repository "deb http://security.ubuntu.com/ubuntu xenial-security main"
sudo apt update
sudo apt install libjasper1 libjasper-dev
完成安装后,打包过程即可顺利结束。
总结
以上介绍了在Ubuntu下使用linuxdeployqt打包Qt程序时可能遇到的问题及解决方案,希望能帮助到大家。如有任何疑问欢迎留言,小编会及时回复。感谢支持!若觉得本文有帮助,欢迎转载并注明出处。