软件安装流程

前面软件篇提到了通过 RPM 和 YUM 在线安装的机制安装软件,除了这两种方式之外还有一种通过源码来安装软件的方式。
 
如上流程图所示,程序员使用特定语言(c/c++/...)编写源文件,通过编译器翻译成机器可以执行的可执行文件,也就是二进制文件。其中,如果源文件依赖函数库的话,在编译的时候还需要将相应的函数库给链接上。
 
 
走一遍操作的流程看看程序是怎么从源文件到可执行文件的。
编写源文件 hello.cpp 和 thanks.cpp,其中 hello.cpp 引用 thanks.cpp 的函数:
[root@lianhua tarball]$ cat hello.cpp
#include <stdio.h>
#include "thanks.h" int main(void)
{
printf("Hello World\n");
thanks(); return 0;
}
[root@lianhua tarball]$ cat thanks.cpp
#include <stdio.h> int thanks(void)
{
printf("thanks lianhua, I love you\n"); return 0;
}
[root@lianhua tarball]$ cat thanks.h
int thanks();
 
源文件有了,使用 Liunx 自带的 c/c++ 编译器 gcc 编译源文件:
[root@lianhua tarball]$ gcc -c hello.cpp
[root@lianhua tarball]$ ll
total 16
-rw-r--r-- 1 root root 117 May 10 22:44 hello.cpp
-rw-r--r-- 1 root root 1560 May 10 22:45 hello.o
-rw-r--r-- 1 root root 80 May 10 22:45 thanks.cpp
-rw-r--r-- 1 root root 14 May 10 22:44 thanks.h
[root@lianhua tarball]$ gcc -o hello hello.o
hello.o: In function `main':
hello.cpp:(.text+0xf): undefined reference to `thanks()'
collect2: error: ld returned 1 exit status
 
报错,显示函数 thanks 没定义。出错原因是因为 hello.cpp 引用到了该函数,但是编译的时候没有链接,所以对症下药:
[root@lianhua tarball]$ gcc -c hello.cpp thanks.cpp
[root@lianhua tarball]$ ll
total 20
-rw-r--r-- 1 root root 117 May 10 22:44 hello.cpp
-rw-r--r-- 1 root root 1560 May 10 22:46 hello.o
-rw-r--r-- 1 root root 100 May 10 22:46 thanks.cpp
-rw-r--r-- 1 root root 14 May 10 22:44 thanks.h
-rw-r--r-- 1 root root 1520 May 10 22:46 thanks.o
[root@lianhua tarball]$ gcc -o hello hello.o thanks.o
[root@lianhua tarball]$ ll
total 32
-rwxr-xr-x 1 root root 8552 May 10 22:47 hello
-rw-r--r-- 1 root root 117 May 10 22:44 hello.cpp
-rw-r--r-- 1 root root 1560 May 10 22:46 hello.o
-rw-r--r-- 1 root root 100 May 10 22:46 thanks.cpp
-rw-r--r-- 1 root root 14 May 10 22:44 thanks.h
-rw-r--r-- 1 root root 1520 May 10 22:46 thanks.o
[root@lianhua tarball]$ file hello
hello: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, BuildID[sha1]=1801febeffa3328a578dac21ee994dd50893ed3f, not stripped
[root@lianhua tarball]$ ./hello
Hello World
thanks lianhua, I love you
 
可以看到,将 thanks.cpp 编译成目标文件 thanks.o,然后链接到该目标文件生成了可执行的二进制文件 hello。其中,file 命令可以判断文件是二进制文件或一般文本文件。
 
 
这是一种生成可执行文件的方式,还有一种提供函数库给编译器编译的方式。
函数库分两种静态函数库和动态函数库,静态函数库在编译的时候被放到可执行文件中,动态函数库在编译的时候会创建“指针”指向该动态函数库。所以,使用静态函数库的可执行文件要比使用动态函数库的可执行文件大。但是基于静态函数库的可执行文件在编译完之后就不依赖于库,放到哪都可以执行,基于动态函数库的可执行文件则不行,它需要能引用到该动态函数库。

静态函数库的可执行文件

[root@lianhua tarball]$ ar rcs liblianhua.a thanks.o
[root@lianhua tarball]$ ll
total 36
...
-rw-r--r-- 1 root root 1668 May 10 23:09 liblianhua.a
[root@lianhua tarball]$ gcc hello.cpp -llianhua -L/root/hxia/tarball
[root@lianhua tarball]$ gcc hello.cpp -llianhua -L/root/hxia/tarball -o lianhua
[root@lianhua tarball]$ ll
total 60
-rwxr-xr-x 1 root root 8552 May 10 22:47 hello
-rw-r--r-- 1 root root 117 May 10 22:44 hello.cpp
-rw-r--r-- 1 root root 1560 May 10 22:46 hello.o
-rwxr-xr-x 1 root root 8552 May 10 23:11 lianhua
-rw-r--r-- 1 root root 1668 May 10 23:09 liblianhua.a
-rw-r--r-- 1 root root 100 May 10 22:46 thanks.cpp
-rw-r--r-- 1 root root 14 May 10 22:44 thanks.h
-rw-r--r-- 1 root root 1520 May 10 22:46 thanks.o
[root@lianhua tarball]$ ./lianhua
Hello World
thanks lianhua, I love you
 
通过 ar 命令将 thanks.o 生成为 lianhua 的静态函数库,编译器链接该静态函数库生成可执行的二进制文件 lianhua。其中,静态函数库以 .a 结尾,动态函数库以 .so 结尾。

动态函数库的可执行文件

[root@lianhua shared]$ gcc -fPIC -shared thanks.cpp -o liblianhua.so
[root@lianhua shared]$ ll
total 20
-rw-r--r-- 1 root root 117 May 10 23:18 hello.cpp
-rwxr-xr-x 1 root root 8112 May 10 23:20 liblianhua.so
-rw-r--r-- 1 root root 100 May 10 23:18 thanks.cpp
-rw-r--r-- 1 root root 14 May 10 23:18 thanks.h
[root@lianhua shared]$ gcc hello.cpp -L. -llianhua -o hello
[root@lianhua shared]$ ll
total 32
-rwxr-xr-x 1 root root 8528 May 10 23:21 hello
-rw-r--r-- 1 root root 117 May 10 23:18 hello.cpp
-rwxr-xr-x 1 root root 8112 May 10 23:20 liblianhua.so
-rw-r--r-- 1 root root 100 May 10 23:18 thanks.cpp
-rw-r--r-- 1 root root 14 May 10 23:18 thanks.h
[root@lianhua shared]$ ./hello
./hello: error while loading shared libraries: liblianhua.so: cannot open shared object file: No such file or directory
 
命令 gcc -fPIC -shared 将 thanks.cpp 编译成动态函数库 lianhua,生成可执行文件 hello。但是,在执行 hello 的时候报错了,提示找不到 lianhua 这个动态函数库。错误原因是动态链接时默认从 /usr/lib 目录下找动态函数库,将 lianhua 拷到该目录下,更新配置:
[root@lianhua shared]$ cp libthanks.so /usr/lib/
[root@lianhua shared]$ vi /etc/ld.so.conf
[root@lianhua shared]$ ldconfig
[root@lianhua shared]$ ./hello
Hello World
thanks lianhua, I love you
 
ldconfig 命令将动态函数库从硬盘加载到内存中,这样执行可执行文件的时候执行引用内存中的动态函数库,速度会快很多。
 
Liunx 的 ldd 命令可以查看可执行文件引用了哪些动态函数库:
[root@lianhua tarball]$ ldd hello
linux-vdso.so.1 => (0x00007ffcc9bec000)
libc.so.6 => /lib64/libc.so.6 (0x00007f4175126000)
/lib64/ld-linux-x86-64.so.2 (0x00007f41754f3000)
[root@lianhua tarball]$ ldd ./shared/hello
linux-vdso.so.1 => (0x00007fff7d9da000)
libthanks.so => /lib/libthanks.so (0x00007f6896b53000)
libc.so.6 => /lib64/libc.so.6 (0x00007f6896786000)
/lib64/ld-linux-x86-64.so.2 (0x00007f6896d55000)
可以发现基于动态函数库的 hello 指向了 thanks 这个库。同时,比较两个可执行文件大小也会发现基于静态函数库的可执行文件 hello 要比基于动态函数库的可执行文件要大。
 
 
想象下,这是只有一个函数的编译情况,在实际项目中有成百上千上万个函数,各个函数分布在不同的目录这时候在用这种 gcc 指定函数库编译的方式就特别不适用了。
在这种情况下,会写 cmake 文件定义编译规则,每个目录下都有 cmake 文件。再执行 cmake 命令生成 makefile 文件,然后通过 make 命令就能根据 makefile 中的规则编译生成可执行文件。
 
具体这个流程实操不介绍了,有兴趣的看看这个文档
 
 
 
(完)
 

每天学五分钟 Liunx 1000 | 软件篇:源码安装的更多相关文章

  1. Linux一些常用软件的源码安装

    Sreen: wget ftp://ftp.gnu.org/pub/gnu/screen/screen-4.0.3.tar.gz .tar.gz cd screen- ./configure make ...

  2. 【程序包管理】Linux软件管理之src源码安装编译

    在很多时候我们需要自定义软件的特性,这时就需要用到源码安装.那么,网上有很多编译源码的工具,那么,我们怎么知道别人使用的是什么工具呢.其实我也不知道(*^▽^*). 那么本篇博客主要是写C代码的源码安 ...

  3. salt源码安装软件和yum安装软件

    上面简单列出了源码安装的sls文件书写思路. 涉及到一些固定的思路:如, 1,拷贝 解压安装时候需要依赖tar.gz存在 如果已安装则无需再次安装. 2,启动脚本 加入chk时候需要文件存在,如果已添 ...

  4. 搭建LNAMP环境(五)- PHP7源码安装Redis和Redis拓展

    上一篇:搭建LNAMP环境(四)- 源码安装PHP7 一.安装Redis 1.创建redis用户组和用户 groupadd redis useradd -r -g redis -s /sbin/nol ...

  5. linux下源码安装软件

    在linux下的很多软件都是通过源码包方式发布的,这样做对于最终用户而言,虽然相对于二进制软件包,配置和编译起来繁琐点,但是它的可移植性却好得多,针对不同的体系结构,软件开发者往往仅需发布同一份源码包 ...

  6. Linux源码安装软件

    Linux环境下 源码编译安装软件 ==== 1. 下载,步骤略 ==== 2. 验证,部分官方下载同时提供签名,MD5,sha1,sha256等校验信息. $ gpg --verify gnupg- ...

  7. Android必学-异步加载+Android自定义View源码【申明:来源于网络】

    Android必学-异步加载+Android自定义View源码[申明:来源于网络] 异步加载地址:http://download.csdn.net/detail/u013792369/8867609 ...

  8. linux操作之软件安装(二)(源码安装)

    源码安装 linux上的软件大部分都是c语言开发的 , 那么安装需要gcc编译程序才可以进行源码安装. yum install -y gcc #先安装gcc 安装源码需要三个步骤 1) ./confi ...

  9. [svc]salt源码安装软件和yum安装软件

    上面简单列出了源码安装的sls文件书写思路. 涉及到一些固定的思路:如, 1,拷贝 解压安装时候需要依赖tar.gz存在 如果已安装则无需再次安装. 2,启动脚本 加入chk时候需要文件存在,如果已添 ...

  10. Linux 之 软件安装-yum、rpm、源码安装

    软件安装-yum.rpm.源码安装 参考教程:[千峰教育] 一.Yum安装软件: 基本说明: 1.yum相当于windows上面的360的软件中心,appstore,安卓的应用商店. 2.yum是re ...

随机推荐

  1. org.springframework.context.ApplicationContextException: Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang.NullPointerException报错问题

    这个原因是 高版本SpringBoot整合swagger 造成的 我的项目是2.7.8 swagger版本是3.0.0 就会出现上面的报错 解决方式: 1.配置WebMvcConfigurer.jav ...

  2. MySQL运维9-Mycat分库分表之枚举分片

    一.枚举分片 通过在配置文件中配置可能的枚举值,指定数据分布到不同数据节点上,这种方式就是枚举分片规则,本规则适用于按照省份,性别,状态拆分数据等业务 二.枚举分片案例 枚举分片需求:现有 tb_en ...

  3. 实践解析HPA各关联组件扭转关系

    本文分享自华为云社区<HPA各关联组件扭转关系以及建议>,作者:可以交个朋友. 一.背景 应用程序的使用存在波峰波谷现象,在应用流量处于低谷期间,可以释放因过多的Pod而浪费的硬件资源.在 ...

  4. 文心一言 VS 讯飞星火 VS chatgpt (162)-- 算法导论13.1 2题

    二.用go语言,对图13-1中的红黑树,画出对其调用TREE-INSERT 操作插人关键字 36 后的结果.如果插人的结点被标为红色,所得的树是否还是一棵红黑树?如果该结点被标为黑色呢? 文心一言,代 ...

  5. 2023"安洵杯"第六届网络安全挑战赛-Misc WP

    dacongのsecret 题目 我的解答: 题目给出一张png图片和一个加密压缩包,压缩包里面还存在另一张jpg图片 看名字就知道是盲水印.由于压缩包里的图片提不出来,因此是单图盲水印,我们使用工具 ...

  6. ElasticSearch之查看集群的参数

    参考Cluster get settings API. 命令样例,不指定参数,如下: curl -X GET "https://localhost:9200/_cluster/setting ...

  7. IIS通过ARR实现负载均衡

    一.实现整体方式介绍 项目中部署在windows服务器上的项目,需要部署负载均衡,本来想用nginx来配置的,奈何iis上有几个项目,把80端口和443端口占用了,nginx就用不了了(因为通过域名访 ...

  8. C语言之小明的加减法

    1.题目内容: 叛逆期的小明什么都喜欢反着做,连看数字也是如此(负号除外),比如: 小明会把1234它看成4321:把-1234看成-4321:把230看成032 (032=32):把-230看成-0 ...

  9. PythonAnywhere 部署Flask项目

    一. 注册账号 官网:https://www.pythonanywhere.com/ 二. 将GitHub上的项目发送至PythonAnywhere 三.配置环境及运行 git clone https ...

  10. 文心一言 VS 讯飞星火 VS chatgpt (25)-- 算法导论4.2 7题

    七.设计算法,仅使用三次实数乘法即可完成复数 a+bi和c+di 相乘.算法需接收a.b.c和d 为输入,分别生成实部 ac-bd 和虚部ad+bc. 文心一言: 可以使用如下算法来计算复数 a+bi ...