linux系统中 库分为静态库和,Linux系统静态库与共享库

66b52468c121889b900d4956032f1009.png

8种机械键盘轴体对比

本人程序员,要买一个写代码的键盘,请问红轴和茶轴怎么选?

This article mainly introduces the statics library and shared library on Linux and has done some experiments for better comprehension.

Static library,又称为归档文档(archive). 在Linux系统中一般以.a作为后缀名.用以声明除了C语言标准库之外的库文档的目录. 这个声明是静态的,也就是说,当许多应用进程同时运行并且都是用来自同一个函数库的函数时,在内存中就会存有这个函数的多份拷贝.这将大量消耗内存和磁盘空间. 类似与Windows中的静态链接库.lib文档

共享库(shared library / dynamic library)

共享库克服了静态库的不足,典型的后缀名是.so。类似与Windows下的dll文档。

In Arch Linux, the paths of shared library files are declared in /etc/ld.so.conf. You can add your specified path into this file and then using sudo ldconfig for generating their so-name files if there is update of these library files happening.

The naming suggestion of Linux shared library

Every shared library has its filename and so-name(Short for shared Object name, 简单共享名). The following naming rules are commonly obeyed:

filename: libname.so.x.y.z

so-name: libname.so.x

x 代表了主版本号,主版本号之间不同通常是无法相互兼容的。

y 代表次版本号,可以向下兼容。

z 代表发布版本号,之间可以相互兼容。

当运行 ldconfig 命令后,系统会为制定目录下面的动态库文档新建与 so-name 同名的软链接。当编译完进程需要链接的时候,查找的就是这些对应的 so-name。可以用环境变量 LD_LIBRARY_PATH 指定so-name files所在的目录。

First experiment

Supposing that we want to create a shared library for calling function hello declared by hello.h, we start by writing our code here:

1

2

3

4

5

6

7void (const char* name)

{

printf("hello %s!n", name);

}1

2void (const char* name);

Then we compile it by gcc to generate shared lib:

1gcc hello.c -fPIC -shared -Wl,-soname,libhello.so.0 -o libhello.so.0.0.1

Let me explain every option of the above command. -fPIC means generating position independent code, i.e., address jumping is relative rather than absolute. This option is required in generating library file because lib file usually locates at some place and is called by programs from other places, or the program is generated at some place but is moved to other path. -shared -o indicates a shared library file .so.x.y.z. And -Wl,-soname,libhello.so.0 specifies its so-name as ‘libhello.so.0’.

Now we check our files and should see a new file like this picture:

Next we update by ldconfig

1ldconfig -n shared-library/

Note that -n specifies the dir only being processed(Because we only created one lib file under shared-library, it has no need to update all). If you have added the path into /etc/ld.so.conf, you can also simply run sudo ldconfig and see the same change:

As we can see, the so-name symbolic link has been created. Now we can test this new lib by writing a test code:

1

2

3

4

5

6

7

8int main()

{

hello("handy");

return 0;

}

Then we create a symbolic link to the so-name file in order for gcc compiler specification:

Now we make these three stages of shared library prepared(.so, .so.x and .so.x.y.z), then we compile and link, with relevent paths specified:

1gcc main.c -I /home/shane/Experiments/shared-library/ -L. -lhello -o main

where -I specifies the path of hello.h, -L for the path of libhello.so.

Since we have specified the path of so-name to gcc compiler but have not done that for Linux executer(one of the features of shared library), an error of failing to find the so-name file appears when running the program. So we use LD_LIBRARY_PATH to set it and run again:

1export LD_LIBRARY_PATH="$HOME/Experiments/shared-library/"

More exploration

用ldd查看其依赖的动态库:

我们发现main进程依赖的动态库名字是libhello.so.0,既不是libhello.so也不是libhello.so.0.0.1。其实在生成main进程的过程有如下几步:链接器通过编译命令-L. -lhello在当前目录查找libhello.so文档

读取libhello.so链接指向的实际文档,这里是libhello.so.0.0.1

读取libhello.so.0.0.1中的SONAME,这里是libhello.so.0

将libhello.so.0记录到main进程的二进制数据里

所以你看,进程并不知道 so-name file 在哪里,我们当然要在运行进程前 specify 一波了。

Second experiment

Now we emulate the situation of updating lib files. Suppose that we modify our code:

1

2

3

4

5

6

7# include

void hello(const char* name)

{

printf("hello %s, welcome to the world!n", name);

}

Since the change is trivial, we keep the so-name when compiling:

1gcc hello.c -fPIC -shared -Wl,-soname,libhello.so.0 -o libhello.so.0.0.2

Now there are two versions exist, we update by ldconfig and see the change:

The so-name file link to the new version of lib file. And we run the program and see the immediate change:

So you see, this is the significance or the essence of so-name mechanism. We don’t have to re-link the program after modifying the shared library code.

Summary

In practical production, the compilation and execution are usually departed. Generally:specify the so-name when generating shared lib files

Ensure the availability of libXXX.so file by -L and -l when linking executable program

Ensure the existence of shared lib file and use LD_LIBRARY_PATH to specify the directory of its so-name link when running program

References

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/259462.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

软件工程概论作业01

软件工程作业01 写一个能自动生成三十道小学四则运算题目的 “软件”,要求:除了整数以外,还要支持真分数的四则运算(需要验证结果的正确性)、题目避免重复、可定制出题的数量。 思路:随机生成两个数进行计算…

成员指针运算符 .* 和 -*

转载: http://www.groad.net/bbs/thread-5548-1-1.html 有一种特殊的指针叫做成员指针,它们通常指向一个类的成员,而不是对象中成员的特定实例。 成员指针并不是真正的指针,它只是成员在对象中的偏移量,它们分别是&am…

捕捉Entity framework 6的详细异常提示

采用 try{}catch (Exception e){throw;}不能捕捉到详细异常提示, e.message的内容为"Validation failed for one or more entities. See EntityValidationErrors property for more details." 如果需要获取详细的异常提示,采用 1 try2 {3 return…

8.16——熟悉安装linux系统

一、linux的版本——CentOS CentOS(Community ENTerprise Operating System)是Linux发行版之一,它是来自于Red Hat Enterprise Linux依照开放源代码规定释出的源代码所编译而成。由于出自同样的源代码,因此有些要求高度稳定性的服…

linux中设置默认权限的命令,Linux默认权限掩码

Linux教程Linux教程:http://www.fdlly.com/m/linux文章目录默认权限掩码设置权限掩码以文字的方式设置权限掩码查看系统当前的权限掩码默认权限掩码当我们创建文件或目录时,系统会自动根据权限掩码来生成预设权限;默认情况下的umask值是022(可…

percona-toolkit工具包安装

percona-toolkit工具包同percona-xtrabackup一样都是用Perl写的工具包,percona-toolkit工具包是一组高级的管理mysql的工具包集,可以用来执行各种通过手工执行非常复杂和麻烦的mysql和系统任务,在生产环境中能极大的提高效率,安装…

C++允许重载的运算符和不允许重载的运算符

C中绝大部分的运算符允许重载&#xff0c;具体规定见表10.1。 表10.1 C允许重载的运算符双目算术运算符 (加)&#xff0c;-(减)&#xff0c;*(乘)&#xff0c;/(除)&#xff0c;% (取模) 关系运算符 (等于)&#xff0c;! (不等于)&#xff0c;< (小于)&#xff0c;> (大…

Google Mesa概览

Google Mesa的文章&#xff1a;https://research.google.com/pubs/pub42851.html https://gigaom.com/2014/08/07/google-shows-off-mesa-a-super-fast-data-warehouse-that-runs-across-data-centers/ 为什么未来的Hadoop是实时的&#xff1a; https://gigaom.com/2013/03/0…

C++数组参数应用方式探讨(转)

对于经验丰富的编程人员来说&#xff0c;C编程语言应该是他们经常使用于程序开发的一种实用性语言。那么&#xff0c;在C中&#xff0c;C数组参数永远不会按值传递。它是传递第一个元素&#xff08;准确地说是第0个&#xff09;的指针。 例如&#xff0c;如下声明&#xff1a; …

一篇关于兼容问题的基础总结

1.添加兼容文件(以 es5-shim 为例) 方法一&#xff1a; <script src"https://cdnjs.cloudflare.com/ajax/libs/es5-shim/4.5.7/es5-shim.min.js"></script>在你的开发中&#xff0c;在需要为他做兼容的文件引入改文件 方法二(以模块引入)&#xff1a; 在…

假如生活欺骗了你

假如生活欺骗了你&#xff0c; 不要悲伤&#xff0c;不要心急&#xff01; 忧郁的日子里需要镇静&#xff1a; 相信吧&#xff0c;快乐的日子将会降临。 心儿永远向往着未来&#xff1b; 现在却常是忧郁&#xff0c; 一切都将会过去&#xff1b; 而那过去了的&#xff0c…

linux编译mmc驱动,Embeded linux之MMC驱动

一、注册平台设备platform_device_register(&usr_mci_device);二、填写平台设备结构体static struct platform_device usr_mci_device {.name "xxx",.id 0,.dev {.release usr_mci_platdev_release,.dma_mask &usr_mmc_dmama…

redis windows下的环境搭建

先说下安装吧&#xff01;感觉这东西跟mongodb差不多&#xff0c;安装和布置挺简单&#xff0c;下载地址&#xff1a;https://github.com/dmajkic/redis/downloads 下载下来的包里有两个&#xff0c;一个是32位的&#xff0c;一个是64位的。根据自己的实情情况选择&#xff0c;…

application/json 四种常见的 POST 提交数据方式

四种常见的 POST 提交数据方式 HTTP/1.1 协议规定的 HTTP 请求方法有 OPTIONS、GET、HEAD、POST、PUT、DELETE、TRACE、CONNECT 这几种。其中 POST 一般用来向服务端提交数据&#xff0c;本文主要讨论 POST 提交数据的几种方式。 我们知道&#xff0c;HTTP 协议是以 ASCII 码传…

C++的4种类型转换关键字及其特点

C中有四种类型转换关键字&#xff0c;分别是reinterpret_cast,static_cast,const_cast,dynamic_cast.这是C 为了减少强制转换的副作用&#xff0c;并且在查错时使程序员能够快速定位&#xff08;总是最值得怀疑的&#xff09;强制转换&#xff0c;在标准C中新增加了4个关键字*…

linux系统数据库类型,linux下的数据类型

sys/types.h sys/types.h中文名称为基本系统数据类型。在应用程序源文件中包含 以访问 _LP64 和 _ILP32 的定义。此头文件还包含适当时应使用的多个基本派生类型。尤其是以下类型更为重要&#xff1a;caddr_t 核心地址。clock_t 表示系统时间(以时钟周期为单位)。comp_t 压缩的…

jsp乱码

自从重装系统之后电脑运行程序总是容易出现一些微妙的乱码&#xff0c;一直都没有彻底解决&#xff0c;有时候在别的机器上运行无误的代码一到我的机器上就出现一些问题。 myeclipse编码方式怎么改都无效&#xff0c;每次只能再代码中加上几行转码的语句 今天终于找到罪魁祸首-…

如何使用Notepad++格式化XML文件

经常会从数据库中读到挤在一起的XML, 整理它们的格式需要使用一些工具. 比如笔者之前使用过online的tool. 后来经同事介绍, 改用VS2008的CtrlK, CtrlF来整理. 但是VS2008有点庞大, 开启起来还是有点慢, 用起来也远不如Notepad顺手. 于是笔者Google了一把. 找到了下面的步骤, 非…

@MySQL的存储引擎

1.存储引擎 查看MySQL提供了哪些存储引擎 mysql> show engines; ----------------------------------------------------------------------------------------------------------------------------- | Engine | Support | Comment …

联想u盘linux安装教程,联想笔记本用U盘安装 winXP系统教程

联想笔记本用U盘安装 winXP系统教程。联想笔记本是指联想集团生产的便携手提电脑。 联想集团成立于1984年&#xff0c;由中科院计算所投资20万元人民币、11名科技人员创办&#xff0c;到今天已经发展成为一家在信息产业内多元化发展的大型企业集团。今天小编将给大家介绍使用U盘…