Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the management of dynamic link library on unix platform

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces to you what is the management of unix platform dynamic link library, the content is very detailed, interested friends can refer to, hope to be helpful to you.

The only constant in the world is change, especially for dynamic link libraries. The dynamic library is designed to facilitate the update of the program. It only needs to replace the library file and does not need to recompile the main program to run the updated version. In addition, the dynamic library is shared by multiple applications, and multiple programs use the same library, so the library will only load one copy.

However, the reality is often not so simple. Now that there is a new version of the dynamic library, there will be compatibility or incompatibility with the old version. When there are multiple applications in the system that use this dynamic library, if it is compatible with the old version of the update, if it is not compatible, then the application using the old version will not work properly. In particular, when dynamic libraries are centrally placed in the system-specified library directory (windows's system32), version conflicts form the famous "windows dll hell".

To solve this problem, many UNIX systems use the DT_SONAME field in ELF format to deal with it. For example, there is such an option in linux's ld command.

-h name

-soname=name

When creating an ELF shared object, set the internal DT_SONAME

Field to the specified name. When an executable is linked with a

Shared object which has a DT_SONAME field, then when the executable

Is run the dynamic linker will attempt to load the shared object

Specified by the DT_SONAME field rather than the using the file

Name given to the linker.

Indicates that using the-h or-soname option, you can specify the DT_SONAME field in the dynamic library to save the name of the dynamic library found at run time, so that the dynamic library name can be different at compile time and run time. For example, if you use-soname=libwel.so.1 when compiling libwel.so, then compiling the executable program-lwel, connecting to libwel.so, will record the runtime looking for libwel.so.1 loading in the search path.

ELF is a widely used binary target format. Similarly, the binary target of UNIX also has a.out format (SCO UNIX) and XCOFF format (AIX). These two formats cannot support soname.

So, how to make multiple versions of dynamic link libraries support at the same time? it is concluded that using this naming method, both new and old incompatible versions can coexist, and compatible versions can be upgraded normally. The dynamic library is named libxxx.so.n.m,soname and libxxx.so.n. The soft connection libxxx.so points to the latest n version libxxx.so.n for compiling executable programs, and the soft connection libxxx.so.n points to the latest m version of version n. In this way, the change of n indicates that the change of the old version is not compatible, and the change of m under the same n indicates the change of the compatible n version.

Let's take libwel.so as an example. Wel.c version 1.0 provides the function void welcome (void) {printf ("aaa\ n");}, which is compiled into the library libwel.so.1.0,soname as libwel.so.1. Establish a soft connection libwel.so.1,libwel.so. Use main1.c, the main program of the 1.0 library, to call welcome () and compile it into a main1 program. The 1.1 version of wel.c is changed to void welcome (void) {printf ("bbb\ n");}. The dynamic library only modifies the internal implementation, the external interface is compatible with version 1.0, and is compiled into the library libwel.so.1.1. At this time, the updated soft connection libwel.so.1 is libwel.so.1.1, and the main program main1 can output the updated bbb without any change. Version 2.0 of wel.c changes the external interface to provide the function void welcome (char * p) {printf ("% s\ n", p);}. Compiled into a library libwel.so.2.0,soname to libwel.so.2. Establish a soft connection libwel.so.2,libwel.so. The main program main2.c that uses the 2. 0 library should call welcome ("test") with one parameter. The main1 and main2 main programs are incompatible and must call the libraries of version 1 and version 2, respectively. At this point, libwel.so.1 in the library directory points to version 1.1, libwel.so.2 points to version 2.0, main1 will run with version 1.1, and main2 will run with version 2.0. Thus it can be seen that the application automatically uses the latest version when the small version is upgraded, and the "outdated" application using the old version is not affected when the large version is upgraded, thus avoiding the confusion of the dynamic library version.

Similarly, taking link as an example, the compilation method of dynamic library with soname on each UNIX platform is given. (not supported by AIX)

Linux:

Compile libwel.so:

Gcc-fPIC-shared-Wl,-soname,libwel.so.1-o libwel.so.1.0 wel.c-lc

Ln-sf libwel.so.1.0 libwel.so.1

Ln-sf libwel.so.1 libwel.so

-Wl,-soname,libwel.so.1 specifies that the actual soname of libwel.so.1.0 is libwel.so.1

Sco unix open server:

Compile libwel.so:

Cc-K pic-G-h libwel.so.1-o libwel.so.1.0 wel.c-lc

Ln-sf libwel.so.1.0 libwel.so.1

Ln-sf libwel.so.1 libwel.so

-h libwel.so.1 specifies that the actual soname of libwel.so.1.0 is libwel.so.1

HP UX:

Compile libwel.so:

Cc + z-c wel.c

Ld-b-o libwel.sl.1.0 wel.o + h libwel.sl.1-lc

Ln-sf libwel.sl.1.0 libwel.sl.1

Ln-sf libwel.sl.1 libwel.sl

+ h libwel.sl.1 specifies that the actual soname of libwel.sl.1.0 is libwel.sl.1

SUN OS:

Compile libwel.so:

Cc-G-h libwel.so.1-o libwel.so.1.0 wel.c-lc

Ln-sf libwel.so.1.0 libwel.so.1

Ln-sf libwel.so.1 libwel.so

-h libwel.so.1 specifies that the actual soname of libwel.so.1.0 is libwel.so.1

On the unix platform dynamic link library management is what is shared here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report