1. 用Qt Creator
新建一个helloworld工程,并验证了在Qt Creator上编译通过。
2. 修改helloworld.pro文件
2.1. 在工程目录下添加了一个README文件。
[sam@sam helloworld]$ tree
.
|-- helloworld.pro
|-- helloworld.pro.user
|-- main.cpp
|-- mainwindow.cpp
|-- mainwindow.h
|-- mainwindow.ui
`-- README
2.2. 添加程序安装路径,如下所示:
target.path = /usr/sbin
documentation.files = README
documentation.path = /usr/share/helloworld
INSTALLS = target documentation
3. 打包helloworld source tar包
$ mv helloworld helloworld-1.0.0
$ tar -czvf helloworld-1.0.0.tar.gz helloworld-1.0.0
4. 构建非root Buildroot环境
4.1. 安装rpmdevtools工具
# yum install rpmdevtools
4.2. 生成非root Buildroot环境
$ rpmdev-setuptree
在路径~(为/home/sam)下生成rpmbuild目录,包含以下子目录:
[sam@sam rpmbuild]$ ls
BUILD RPMS SOURCES SPECS SRPMS
4.3. 把之前打包的helloworld source tar包复制到SOURCES目录下
5. 创建spec文件
5.1. 生成一个spec文件模板
$ cd ~/rpmbuild/SPECS/
$ rpmdev-newspec helloworld
5.2. 查看生成的spec模板
Name: helloworld
Version:
Release: 1%{?dist}
Summary:
Group:
License:
URL:
Source0:
BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root-%(%{__id_u} -n)
BuildRequires:
Requires:
%description
%prep
%setup -q
%build
%configure
make %{?_smp_mflags}
%install
rm -rf $RPM_BUILD_ROOT
make install DESTDIR=$RPM_BUILD_ROOT
%clean
rm -rf $RPM_BUILD_ROOT
%files
%defattr(-,root,root,-)
%doc
%changelog
5.3. 编辑spec文件
Name: helloworld
Version: 1.0.0
Release: 1%{?dist}
Summary: This is a beautiful world
Group: User Interface/X
License: LGPL
URL: http://www.hello_world.org
Source0: %{name}-%{version}.tar.gz
BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root-%(%{__id_u} -n)
%description
Hello, this is a beautiful world.
%prep
%setup -q
%build
qmake
make
%install
rm -rf $RPM_BUILD_ROOT
make install INSTALL_ROOT=$RPM_BUILD_ROOT
%clean
rm -rf $RPM_BUILD_ROOT
%files
%defattr(-,root,root,-)
%{_sbindir}/%{name}
%doc %{_datadir}/%{name}/*
%changelog
* Thu Aug 10 2012 Sam Kwok <sam_kwok@example.com> 1.0.0-1
- First release
5.3.1. %build
在/etc/profile文件中添加:
PATH=/home/sam/QtSDK/Desktop/Qt/4.8.0/gcc/bin:$PATH
export PATH
5.3.2. %install
把make install DESTDIR=$RPM_BUILD_ROOT
改成
make install INSTALL_ROOT=$RPM_BUILD_ROOT
因为在用qmake生成的Makefile里的install部分用INSTALL_ROOT,
而不是用DESTDIR。
Makefile的install信息,例如:
####### Install
install_target: first FORCE
@$(CHK_DIR_EXISTS) $(INSTALL_ROOT)/usr/sbin/ || $(MKDIR) $(INSTALL_ROOT)/usr/sbin/
-$(INSTALL_PROGRAM) "$(QMAKE_TARGET)" "$(INSTALL_ROOT)/usr/sbin/$(QMAKE_TARGET)"
-$(STRIP) "$(INSTALL_ROOT)/usr/sbin/$(QMAKE_TARGET)"
6. 编译rpm包
[sam@sam etc]$ cd ~/rpmbuild/SPECS/
[sam@sam SPECS]$ QA_RPATHS=$[ 0x0002 ] rpmbuild -ba helloworld.spec
打印信息最后一行为“+ exit 0”,表示编译成功。
7. 测试rpm包
# yum install rpmlint
$ cd ~/rpmbuild/RPMS/x86_64/
$ rpmlint helloworld-1.0.0-1.el6.x86_64.rpm
helloworld.x86_64: W: invalid-license LGPL
helloworld.x86_64: W: invalid-url URL: http://www.hello_world.org <urlopen error [Errno -2] Name or service not known>
helloworld.x86_64: E: binary-or-shlib-defines-rpath /usr/sbin/helloworld ['/home/sam/QtSDK/Desktop/Qt/4.8.0/gcc/lib']
1 packages and 0 specfiles checked; 1 errors, 2 warnings.
虽然测试时,会出现提示警告和错误,但不碍正常使用。