libtool动态库版本系统之个人理解

来源:互联网 发布:教学钢琴的软件 编辑:程序博客网 时间:2024/05/16 17:12

 

首先看下libtool官方对library版本系统的说明。

 

7.3 Updating library version informationIf you want to use libtool's versioning system, then you must specify the version information to libtool using the -version-info flag during link mode (see Link mode). This flag accepts an argument of the form ‘current[:revision[:age]]’. So, passing -version-info 3:12:1 sets current to 3, revision to 12, and age to 1. If either revision or age are omitted, they default to 0. Also note that age must be less than or equal to the current interface number. Here are a set of rules to help you update your library version information: Start with version information of ‘0:0:0’ for each libtool library. Update the version information only immediately before a public release of your software. More frequent updates are unnecessary, and only guarantee that the current interface number gets larger faster. 1. If the library source code has changed at all since the last update, then increment revision (‘c:r:a’ becomes ‘c:r+1:a’). 2. If any interfaces have been added, removed, or changed since the last update, increment current, and set revision to 0. 3. If any interfaces have been added since the last public release, then increment age. 4. If any interfaces have been removed or changed since the last public release, then set age to 0.


简单解释下, libtool动态库的版本由三个字段组成current:revision:age。

动态库的版本应该开始于0:0:0.

1. 如果library的源码发生了变动, 则revision加1

2. 如果library的接口发生了任何改动(增加, 删除, 参数或返回值变动), 则current加1, revision置0

3. 如果library只是增加了接口, 则age加1

4. 如果library的接口被删除或参数返回值发生变动, 则将age置0

 

简单地讲:

current为当前接口改动的次数

revision为上次接口改变后源码修改(不改动接口)的次数

age为当前版本向前兼容的版本数

 

如【3:12:1】表示该library的接口改动了3次, 第3次接口改动后修改了12次源码, 该版本的library兼容依赖于current=2(3-1)版本library的程序。

 

将该库进行编译后, 会发现库的名字为libraryname.so.2.1.12, 即, 编译产生的库名为libraryname.so.c-a.a.r。表示该library对于依赖于current=c-a到current=c的程序都是可用的, 运行时不会出现任何异常。

实际上编译-version-info 3:12:1产生了三个动态库:libraryname.so, libraryname.so.2 libraryname.so.2.1.12,其中前两个都是第三个的符号链接。

libraryname.so是你在编译时用于链接的, 他指向你安装在该目录下最新的名为libraryname的so

libraryname.so.2是你在运行程序是, 程序所链接的库, 你用ldd命令就能看出

 

故由此引发了一个问题: 假设你的库有两个【3:0:1】【4:0:2】。 再假设在你编译程序的机器上安装了最新的【4:0:2】, 且你在程序中使用了该版本中新加的接口。当你程序编译好后, 你ldd发现你的程序依赖libraryname.so.2, 同时你将程序安装在了只安装了【3:0:1】的机器上, 你会发现你的程序能搜索到动态库, 却在运行的时候发现未定义的符号, 因为【3:0:1】中没有新添加的接口。 故你需要在运行机器上保证安装了同一主版本号最新的library, 以保证你的程序能正确运行。

原创粉丝点击