7

利用rpmbuild 打包可执行文件和链接库生成rpm 包 - aalanwyr

 2 years ago
source link: https://www.cnblogs.com/aalan/p/16702812.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Background:

Background: 遇到一个打包可执行程序和链接库生成rpm 包的需求,查遍了很多网站的资料,现在整理下解决方案。

前期参考的是这篇帖子: https://codeantenna.com/a/jUEqRgoEsZ

CSDN 很多帖子都是抄的rpm 官方提供的教程:https://www.thegeekstuff.com/2015/02/rpm-build-package-example/

这个帖子介绍了rpmbuild 的所有操作,包括把源码放到rpmbuild 创建的文件夹中,通过配置spec完成从源码=》编译=》打包=》安装的全流程,很详细但是太复杂了。我遇到的场景是,已经构建了CMAKELIST, 可以通过写好的脚本一键编译生成可执行程序和需要的动态连接库,我们这里只需要打包一下即可。

 Processing:

该打包过程只是记录了一下将二进制放入rpm中的过程和注意事项,不涉及预编译、编译和安装等繁琐的过程。

 1 、安装依赖

此次打包是SUSE 系统,需要提前安装 rpmbuild 工具。

zypper in -y rpm-build rpmdevtools

2、创建安装目录

 通过rpm 构建默认的文件夹,rpm 默认创建文件夹的路径都是在~/ 目录,如果你把rpmbuild 目录移动到其他地方,进去build ,会显示找不到文件,可以修改rpmbuild 的路劲信息,但是本教程就在~/ 目录下打包。

rpmdev-setuptree

# ls -lF /home/rpmbuild/
drwxr-xr-x. 2 home home 4096 Feb  4 12:21 BUILD/
drwxr-xr-x. 2 home home 4096 Feb  4 12:21 BUILDROOT/
drwxr-xr-x. 2 home home 4096 Feb  4 12:21 RPMS/
drwxr-xr-x. 2 home home 4096 Feb  4 12:21 SOURCES/
drwxr-xr-x. 2 home home 4096 Feb  4 12:21 SPECS/
drwxr-xr-x. 2 home home 4096 Feb  4 12:21 SRPMS/

3、编写spec文件

    在SPECS目录中,创建并编写一个**.spec文件,名字无所谓,格式如下,这个文件很关键,这是告诉rpmbuild 如何打包,其他不相关的我直接删了,只留最需要的

%files 是告诉rpm 打包方法,即你最终想如何把你的程序安装到linux 系统里面。

Summary: test package.
Name: Test
Version: 1.0
Release: 200
License: GPL
Group: test
AutoReqProv: no

%description
test process rpm.
%files
/usr/bin/test
/usr/test

       我现在有两个资源,一个test程序需要在安装之后放到/usr/bin目录,之前cmake 会生成test 和libtest.so 这两个得在一块才能运行,所以我把这两个文件都放在/usr/test/ 中,然后 在/usr/bin/中链接一个可执行程序 ../test/test .

  4、放置资源

    放入资源的时候需要根据编写的spec文件来放置,在BUILDROOT目录要先创建一个目录Test-1.0-200.x86_64,该目录是根据spec文件中配置拼接而成:Name-Version-Release.arch。

    然后在Test-1.0-200.x86_64中创建目录usr/bin,usr/test/,然后将test、libtest.so 放入usr/test/ ,然后cd usr/bin, ln -s ../test/test , 这样就把可执行程序链接到了usr/bin,这样后期安装也是这么按照到linux 下的/usr

      其实打包方式很灵活的,需要结合当前项目的需求灵活考虑

    进入SPECS目录,执行 rpmbuild -bb **.spec 命令,就可以在RPMS目录中生成相应的rpm包了。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK