QT 程序制作 rpm 包,centos +QT+ RPM +rpmbuild +demo下载

一、摘要

       如何在 centos 对开发的QT 程序制作rpm 安装包?

     分解知识:

            a.rpm 打包相关技术  通过查询知道 rpmbuild

                这一部分知识网上比较多,默认是configure 即make打包模式

            b.qt 打包时的特殊细节 针对qt creator 开发的pro 项目如何使用rpmbuild打包

               qt 打包时不使用configure cmake 而是使用 qmake,这还不算,而是使用指定版本,指定路径下的qmake

               c.过程问题:/var/tmp/rpm-tmp.R5ouSs: line 41: ./configure: No such file or directory

/var/tmp/rpm-tmp.R5ouSs: line 41: ./configure: No such file or directory
error: Bad exit status from /var/tmp/rpm-tmp.R5ouSs (%build)

            d.过程中问题:Project ERROR: Unknown module(s) in QT: webenginewidgets charts

Project ERROR: Unknown module(s) in QT: webenginewidgets charts
error: Bad exit status from /var/tmp/rpm-tmp.m8NnVt (%build)

二、rpm 打包的基础知识

        制作前环境中要有rpmbuild,rpmdevtools 可以通过下面命令安装

                yum install rpmbuild
                yum install rpmdevtools

  三、 制作rpm 包

       3.1 创建rpm 打包目录

          首先执行rpmdev-setuptree 生成rpmbuild目录文件,如果没有安装rpmdevtools,只能用mkdir 一个个创建,如:mkdir -p ~/rpmbuild/{BUILD,RPMS,SOURCES,SPECS,SRPMS}

[root@localhost~]# rpmdev-setuptree
[root@localhost~]# ls ~/rpmguild/
[root@localhost~]# BUILD RPMS SOURCES SPECS SRPMS
[root@localhost~]#

      

3.2 打包喂料(将源代码放入打包目录)

           

  比如:网上的一个demo 当然自己也可以创建一个空的 demo

    show-timer-1.0.0  网上的demo

    testrpm.1.0.0    我自己创建的一个demo

 下面主要的工作就是制作代码压缩包,放置到SOURCES 中 *.tar.gz

 比如:show-timer-1.0.0

[root@dev show-timer-1.0.0]# ls
main.cpp          showTime.pro.user                   widget.h
showTime.desktop  showTime.pro.user.2386a92.4.8-pre1  widget.ui
showTime.pro      widget.cpp
[root@dev show-timer-1.0.0]# 

                                 

手工操作可以带文件夹压缩

(纠集点:是在show-timer-1.0.0文件夹里面全选压缩,还是在文件夹外压缩? 答案是,在文件夹外压缩)

然后:

               第一步:将show-timer-1.0.0 文件夹复制到BUILD下

        第二步:将show-timer-1.0.0.tar.gz 文件复制到SOURCES下

第三步:进入SPECS 创建  xx.spec 如: show-timer.spec

  shwo-timer.spec 内容如下:

%global debug_package  %{nil}
#Package name
Name:           show-timer
#Version number
Version:       1.0.0 
#The release number of the package
Release:        1%{?dist}
#summary
Summary:	show-timer        
#The software authorization method is usually GPL (free software) or GPLv2,BSD
License:        LGPLv2+ and GPLv2+

#Source zip package name            
Source0:        show-timer-1.0.0.tar.gz
#Build and compile dependencies. If you write too much, it's useless, but you write it, you have to install it. If you don't install it, you can't compile it;
#If you write less, you can still compile in the current compilation environment. If you don't, you can compile. So it's better to write clearly.
BuildRequires: qt5-qtbase-devel,qt5-qtx11extras-devel,qt5-qttools-devel,qt5-qtsvg-devel 
#,lightdm-qt5-devel
#describe
%description
show-timer
#Perform before installation 
%prep
%autosetup
#The commands to be executed to compile the software package. This section is generally composed of multiple make commands.


%build
qmake-qt5
make

#Defines the command that will be executed when the package is installed, similar to the make install command. Used to define the configuration work to be performed after the software installation is completed.
%install
mkdir -p %{buildroot}/usr/bin/
install -m 755 showTime %{buildroot}/usr/bin/
mkdir -p %{buildroot}/etc/xdg/autostart/
install -m 755 showTime.desktop %{buildroot}/etc/xdg/autostart/
mkdir -p %{buildroot}/usr/share/applications/
install -m 755 showTime.desktop %{buildroot}/usr/share/applications/
#Define the files contained in the package
#%{u bindir} is the macro definition ,here is / usr/bin;% {_sysconfidir} is /etc;% {_datadir} is /usr/share
#All macros can be found in /usr/lib/rpm/macros
%files
%{_bindir}/showTime
%{_sysconfdir}/xdg/autostart/showTime.desktop
%{_datadir}/applications/showTime.desktop
#Each software update can be recorded here and saved in the released software  package for query.
%changelog
* Thu Dec 16 2022 kuangchunhua <[email protected]> - 1.0.0
- show-timer

创建的过程,大家可以手工创建也可以用vim 创建,只要搞定即可

补充一下(showTime.destop 快捷方式文件):

文件中都有一个showTime.destop 快捷方式文件,一般不包括在项目源代码,大家可以网上搜索创建一个放在里打包前的项目中即可,如果懒的搜索就复制下面

[Desktop Entry]
Name=showTimer
Name[zh_CN]=当前时间
Comment=显示当前时间
Comment[zh_CN]=显示当前时间
Exec=/usr/bin/showTime
Terminal=false
Type=Application
NoDisplay=false              

 保存成showTime.destop 即可,如果需要权限什么的,自行赋值 chmod 777 showTime.desktop

3.3 开始执行

      将命令行,定位到/root/rpmbuild 目录下

  执行 rpmbuild -ba SPECS/show-timer.spec 生成rpm 二进制包和src源码包

[root@localhost rpmbuild]# ls SRPMS
show-timer-1.0.0-1.src.rpm
[root@localhost rpmbuild]# ls RPMS/x86_64/
show-timer-1.0.0-1.x86_64.rpm
[root@localhost rpmbuild]# 

  自此,我们在RPMS/x86_64就可以找到 rpm包了,是不是很兴奋,我们成功了

 其实,这是demo ,真的很简单,并且这个demo 大家用rpm -ivh  show-timer-1.0.0-1.x86_64.rpm

能安装成功,然后在菜单中能找到,能运行

 

 那么在开心之余我们来准备把自己开发的代码打包呢

3.4 我们来分析一下show-timer.spec

%build
qmake-qt5
make

 发现 %build 下面的两行代码,才是我们打包qt程序真正要找的命令,

没错,是的,不过感悟的还不够,这里我想说的是,我们在这里要放置真正编译我们qt程序的命令

,我们再换个说法:我们程序已开发完了,并且在qtcreator中可以运行,怎么整合到这里面呢

我们程序已开发完了,并且在qtcreator中可以运行,怎么整合到这里面呢?

我们程序已开发完了,并且在qtcreator中可以运行,怎么整合到这里面呢?

我们程序已开发完了,并且在qtcreator中可以运行,怎么整合到这里面呢?

 用qtcreator 打开我们的项目,然后 左边选中搬手标志,右边选择build

详见3.5 

3.5 找到自己程序的编译命令

 大家在Build Steps 找到了

 qmake xxx.pro -spec linux-g++ CONFIG+=qtquickcompiler
 make -j16 in /home/admin/workspace/MLESI/xxx/build-xxx-Desktop_Qt_5_14_1_GCC_64bit-Release

这个只是开始,不过已经成功了一半,点击details 就会民开抽屉如下:

/opt/Qt5.14.1/5.14.1/gcc_64/bin/qmake /home/admin/workspace/xxx.pro -spec linux-g++ CONFIG+=qtquickcompiler && /bin/make qmake_all

 最重要就是这个,命令,这是你的qtcreator能够正确编译你的代码关键:

/opt/Qt5.14.1/5.14.1/gcc_64/bin/qmake /home/admin/workspace/xxx.pro -spec linux-g++ CONFIG+=qtquickcompiler && /bin/make qmake_all

提取要点:/opt/Qt5.14.1/5.14.1/gcc_64/bin/qmake xxx.pro -spec linux-g++ CONFIG+=qtquickcompiler && /bin/make qmake_all

 说明:/opt/Qt5.14.1/5.14.1/gcc_64/bin/qmake 中的 “/opt/Qt5.14.1/5.14.1/gcc_64/bin/” 就是你本机最宝贵的环境路径,

本机可能安装有多个qmake ,如qmake,qmake-qt5,cmake等那些都没用,只有你的这个路径下的qmake才是真正和你的程序匹配的qmake

还有下一步:

 这一步是 qmake 再执行 make的过程

make -j16 in /home/admin/workspace/xxx/build-xxx-Desktop_Qt_5_14_1_GCC_64bit-Release

make -j16 in /home/admin/workspace/MLESI/mlesi/build-MLESI-Desktop_Qt_5_14_1_GCC_64bit-Release

提取要点:make -j16

3.6 最后自己的xxx.spec 中 %build命令怎么写

  %build
/opt/Qt5.14.1/5.14.1/gcc_64/bin/qmake -spec linux-g++ CONFIG+=qtquickcompiler && /bin/make qmake_all
make  -j16

%build
/opt/Qt5.14.1/5.14.1/gcc_64/bin/qmake -spec linux-g++ CONFIG+=qtquickcompiler && /bin/make qmake_all
make  -j16

 至此,本人独有领悟就写完了,大家可以按照正常命令执行rpm打包就可以生成rpm包了

最后:如果你的开发环境中引用了指定路径的类库,可能会引用一些错误:

[' '%{buildarch}' = noarch ']'
+ QA_CHECK_RPATHS=1
+ case "${QA_CHECK_RPATHS:-}" in
+ /usr/lib/rpm/check-rpaths
*******************************************************************************
*
* WARNING: 'check-rpaths' detected a broken RPATH and will cause 'rpmbuild'
*          to fail. To ignore these errors, you can set the '$QA_RPATHS'
*          environment variable which is a bitmask allowing the values
*          below. The current value of QA_RPATHS is 0x0011.
*
*    0x0001 ... standard RPATHs (e.g. /usr/lib); such RPATHs are a minor
*               issue but are introducing redundant searchpaths without
*               providing a benefit. They can also cause errors in multilib
*               environments.
*    0x0002 ... invalid RPATHs; these are RPATHs which are neither absolute
*               nor relative filenames and can therefore be a SECURITY risk
*    0x0004 ... insecure RPATHs; these are relative RPATHs which are a
*               SECURITY risk
*    0x0008 ... the special '$ORIGIN' RPATHs are appearing after other
*               RPATHs; this is just a minor issue but usually unwanted
*    0x0010 ... the RPATH is empty; there is no reason for such RPATHs
*               and they cause unneeded work while loading libraries
*    0x0020 ... an RPATH references '..' of an absolute path; this will break
*               the functionality when the path before '..' is a symlink
*
*
* Examples:
* - to ignore standard and empty RPATHs, execute 'rpmbuild' like
*   $ QA_RPATHS=$[ 0x0001|0x0010 ] rpmbuild my-package.src.rpm
* - to check existing files, set $RPM_BUILD_ROOT and execute check-rpaths like
*   $ RPM_BUILD_ROOT=<top-dir> /usr/lib/rpm/check-rpaths
*
*******************************************************************************
ERROR   0002: file '' contains an invalid rpath '' in []
ERROR   0002: file '' contains an invalid rpath '' in []
ERROR   0002: file '' contains an invalid rpath '' in []
错误:/var/tmp/rpm-tmp.0yqpeS (%install) 退出状态不好


RPM 构建错误:
    /var/tmp/rpm-tmp.0yqpeS (%install) 退出状态不好

像上面这种 可以通过禁用检查,生成rpm

这一步只是一种检测是不是代码中使用了rpath,那我们可以简单的注释掉rpath检测就可以了,具体做法就是:

  

vi ~/.rpmmacros

    找到这行 

   %__arch_install_post /usr/lib/rpm/check-rpaths /usr/lib/rpm/check-buildroot 注释掉

   #%__arch_install_post /usr/lib/rpm/check-rpaths /usr/lib/rpm/check-buildroot

第四:总结

   整体来说网上rpm 打包的教程通用的rpmbuild 参数是什么很多,但是qt +centos 编译这一块儿,没有明确指出,所以根据领悟,我们可以将图形编译器中的命令行找出来贴到xxx.specs 中即可,当然路径,命令路径就代表着本地化,特殊化,如果想要rpm打包时执行和 qt creator 等IDE一行顺畅,就需要将IDE中的执行命令一点不差的搬到xxx.spec中%build下面

总结到此,备忘,同时也希望能帮到大家。

第五:Demo 下载

 过程中的show-timer1.0.0  

  rpmbuild 打成成功的demoe testrpm

  关于%build 后面关于 qmake 和make 的语句请大家结合上文自己撰写。

猜你喜欢

转载自blog.csdn.net/wyj372/article/details/126893874