1.首先找到打包目录
$ rpm --showrc|grep _topdir
-14: _builddir %{_topdir}/BUILD
-14: _rpmdir %{_topdir}/RPMS
-14: _sourcedir %{_topdir}/SOURCES
-14: _specdir %{_topdir}/SPECS
-14: _srcrpmdir %{_topdir}/SRPMS
-14: _topdir %{_usrsrc}/redhat
$ rpm --showrc|grep _usrsrc
-14: _topdir %{_usrsrc}/redhat
-14: _usrsrc %{_usr}/src
$ rpm --showrc|grep _usr
-14: _defaultdocdir %{_usr}/share/doc
-14: _topdir %{_usrsrc}/redhat
-14: _usr /usr
-14: _usrsrc %{_usr}/src
一般是在/usr/src/redhat目录下
2.编写spec文件
hello.spec
%define debug_package %{nil}
Summary: hello world rpm package
Name: hello
Version: 0.1
Release: 1
Source: hello-0.1.tar.gz
License: GPL
Packager: amoblin
Group: Application
URL: http://www.ossxp.com
#BuildRoot: /usr/src/redhat/BUILD/hello-0.1/ BuildRoot 代表最终的安装目录。换言之,如果 wget 最终安装在 /usr/local/bin/wget 和 /usr/local 中的其他子目录下,比如文档安装在 /usr/local/man 下,那么在 RPM 构建过程中 BuildRoot 代表 /usr/local。一旦设定了 BuildRoot,就可以使用 RPM_BUILD_ROOT 环境变量访问其值。应该始终在 spec 文件中设置 BuildRoot 并检查该目录的内容,确认包即将安装的内容。
%description
This is a software for making your life more beautiful!
%prep
%setup -q
#%setup -q
是一个 %prep
宏,用于自动解压 Source 中的特定 tarball 压缩文件
%build
g++ -o hello hello.cpp
%install
install -m 755 hello /usr/local/bin/hello
%files
/usr/local/bin/hello
把写好的SPEC文件放入SPECS文件夹里
3.编写源程序
随便找个地方新建文件夹 hello-0.1,然后编写hello.cpp。
#include<iostream>
using namespace std;
int main()
{
cout<<"Hello World!"<<endl;
}
打包hello-0.1文件夹,并把hello-0.1.tar.gz 放在SOURCES文件夹下
$ tar zcvf hello-0.1.tar.gz hello-0.1
hello-0.1/
hello-0.1/hello.c
$ mv hello-0.1.tar.gz /usr/src/redhat/SOURCES
4.打包
在SPECS目录下进行打包
5.验证
验证下是否成功了
参考:https://www.ibm.com/developerworks/cn/linux/l-rpm1/index.html