Programming of static, shared, and dynamic libraries in Linux

Source: Internet
Author: User
Tags crypt
I. Classification of databases
There are two types of statements. If you are familiar with the DLL under the win platform, it is not hard to understand:

The library can be used in three forms: static, shared, and dynamic. the code of the static library is connected to the application developed by the developer during compilation, while the shared library is loaded only when the program starts to run. during compilation, you only need to specify the library function to be used. A dynamic library is another form of change in the shared library. the dynamic library is also loaded when the program is running, but unlike the shared library, the library function used is not started when the program is running, but is loaded only when the program statements need to use this function. the dynamic library can release the memory occupied by the dynamic library during the running of the program, freeing up space for other programs to use. because the shared library and dynamic library do not include the library function content in the program, but only contain references to the library function, the code size is relatively small.

Library files in Linux are divided into two categories: Shared Library and static library. The difference between the two is that only the Code required during program execution is dynamically loaded at runtime, static Loading during compilation. the best way to differentiate database types is to look at their file suffixes. Generally, shared libraries use. so (abbreviation of shared object), the static link library usually uses. end of a (abbreviated as archive ). by default, the shared library is usually green, and the static library is black.

Most of the databases that have been developed adopt shared libraries. the executable files in ELF format can be easily implemented by the shared library. Of course, the old. the out mode can also be used to share databases. in Linux, the standard format of executable files is elf.

. A is used to support older executable files in the. Out format.
. So is a library that supports executable files in ELF format.

. A is a static library file, which can be generated using the AR command.
. So is a dynamic library file, which can be generated by adding the specified options during compilation. For specific options, see the corresponding system documentation.

Ii. Database naming rules
The use of the GNU Library must comply with the library GNU Public License (lgpl license ). this Protocol is slightly different from the GNU license protocol. developers can use the GNU Library for software development for free, but must ensure that the source code of the library function is provided to users.

The available databases in the system are stored in the/usr/lib and/lib directories. the library file name consists of the prefix Lib, library name, and suffix. different database types have different suffix names. the extension name of the shared library is. so and version number. The suffix of the static library is. a. use the old. the suffix of the out-format shared library is. SA.
Libname. So. Major. Minor
Libname.

The name can be any string used to uniquely identify a database. this string can be a single word, a few characters, or even a letter. the Library name of the mathematics shared library is libm. so.5. The logo character here is m and the version number is 5. libm. A is a static Math library. x-Windows Database Name: libx11.so. 6. Here, X11 is used as the library identifier. The version number is 6.

Iii. database operation commands

Linux database operations can be completed using commands. Currently, LDD and ldconfig are commonly used.

1. LDD
LDD is short for library dependency display. It is used to display a shared library that can be used by an executable program.

$ LDD/usr/bin/mesg
Libc. so.6 =>/lib/tls/i686/cmov/libc. so.6 (0xb7eaf000)
/Lib/ld-linux.so.2 = & gt;/lib/ld-linux.so.2 (0xb7feb000)

2. ldconfig
After the library is installed in the system, you need to run ldconfig to make the dynamic link library known and shared by the system. the ldconfig command is mainly used in the default search directory (/lib and/usr/lib) and dynamic library configuration file/etc/lD. so. search for the shared dynamic link library (Format: lib *. so *) to create a dynamic loader (LD. so) the required connection and cache file. the default cached file is/etc/lD. so. cache. This file stores the list of dynamic link library names that have been sorted. ldconfig is usually run when the system starts. When you install a new dynamic link library, you need to manually run this command.

(1) Command Format
Ldconfig [Option] [libs]

(2) Main options
-V or -- verbose ldconfig displays the directory being scanned, The searched dynamic link library, and the name of the connection it created.

-F conf specifies that the configuration file of the dynamic link library is Conf, and the default value is/etc/lD. So. conf.

-C cache specifies that the generated cache file is cache, and the system defaults to/etc/lD. So. cache. The file stores the list of sharable Dynamic Link Libraries in sorted order.

-P or -- print-Cache enables ldconfig to print the names of all the shared libraries stored in the current cache file.

-R root: Change the root directory of the application to root.

-N ldconfig only scans the directory specified by the command line, does not scan the default directory (/lib,/usr/lib), and does not scan the configuration file/etc/lD. so. conf directory.

When you run the ldconfig command without any options, it is used to update the cache file. this command is mainly used to cache the DNS server (caching DNS server ). the high-speed buffer DNS server provides query history and uses these records to improve query efficiency.

When a query is sent to the high-speed buffer DNS server for the first time, the high-speed buffer DNS server records the entire Query Process, it can be used to answer all the same queries within a certain period of time, thus reducing the burden on the entire DNS system and increasing the query speed.

4. Database Upgrade

The Linux system software is updated quickly, and the new core is released almost every few weeks. The updates of other software are also very frequent. in most cases, it is not necessary to blindly follow the trend and upgrade the new features if necessary. in other words, do not upgrade for upgrade. in Linux, most software is compiled using a shared library, which contains common subroutines shared between different programs.

When running a program, if the following information is displayed: "incompatible library version. "indicates that you need to upgrade the Library to the version required by the program. the library is backward compatible. That is to say, the program compiled with the old version library can run on the newly installed version library, but not vice versa.

The upgrade of Linux library functions is an important task, which is often associated with the upgrade of other software packages. Therefore, files must be backed up before the operation. the following describes how to upgrade glibc 2.2.4.13 to version 2.3.2. The process is as follows:

1.download .gz compressed file and decompress it

Decompress the four .gz files downloaded from the gun CIDR site to a temporary directory:
CD/usr/caolinux
Tar xzvf glibc-2.3.2.tar.gz
CD glibc-2.3.2
Tar xzvf ../glibc-linuxthreads-2.3.2.tar.gz
Tar xzvf ../glibc-crypt-2.3.2.tar.gz
Tar xzvf ../glibc-localedata-2.3.2.tar.gz

2. Create a library function installation directory
Mkdir/usr/higlibc
CD/usr/higlibc

3. Create a compilation directory
Mkdir Cao
CD Cao
./Configure -- enable-add-ons = linuxthreads, crypt, localedata-Prefix =/usr/higlibc

4. Compilation and Installation
Make
Make check
Make install

5. Change the database link
Ln-S/usr/higlibc/lib/ld-linux.so.2/lib/ld-linux.so.2

Then, modify/etc/lD. So. conf, add/usr/higlibc/lib, and execute the following code:
Ldconfig-V

Update/etc/lD. So. cache, list the version number of each database, scan the Directory and the link to be created and updated.

6. Modify GCC settings

CD/usr/lib/GCC-lib
CP-r i386-redhat-linux higlibc

7. Update the Symbolic Link
CD/usr/higlibc/include
Ln-S/usr/src/Linux/include/Linux
Ln-S/usr/src/Linux/include/ASM
Ln-S/usr/x11r6/include/X11

8. Test and complete

5. Advanced shared database features
1. soname

A very important and difficult concept of a shared library is soname, short for the shared object name (short for shared object name ). this is a shared library (. so) file embedded in the control data name. as mentioned above, each program has a list of libraries to be used. the content of this list is the soname of a series of libraries. As shown in LDD, the shared library loader must find this list.

The key feature of soname is that it provides compatibility standards. when you want to upgrade a database in the system, and the soname of the new database is the same as that of the old database, you can still use the new database to connect the generated program. this feature makes it easy to upgrade the program using the shared library and locate errors in Linux.

In Linux, the application uses soname to specify the version of the desired library. library authors can also declare which versions are compatible by retaining or changing the soname, which frees programmers from the conflict of shared library versions.

View the/usr/local/lib directory and analyze the relationships between the shared library files of MiniGUI.

2. Shared Library Loader

When a program is called, the Linux shared library loader (also known as a dynamic connector) is automatically called. its function is to ensure that all the appropriate versions of the library required by the program are transferred to the memory. the name of the shared library loader is lD. so or ld-linux.so, depending on the Linux libc version, it must use a little external interaction in order to complete their work. however, it accepts the configuration information in the environment variables and configuration files.

File/etc/lD. so. conf defines the path of the standard system library. the shared library loader uses it as the search path. to change this setting, you must run the ldconfig tool as root. this will update/etc/LS. so. cache file, which is actually one of the files used inside the loader.

3. Use dlopen

Another powerful library function is dlopen (). this function will open a new library and load it into the memory. this function is mainly used to load symbols in the library. These symbols are unknown during compilation. for example, the Apache web server uses this function to load modules during running, which provides additional capabilities. A configuration file controls the process of loading modules. this mechanism does not need to be re-compiled when a module is added or deleted in the system.

You can use dlopen () in your own program (). dlopen () in dlfcn. H is defined and implemented in the DL library. it requires two parameters: a file name and a flag. the file name can be the soname in the library we have learned. indicates whether to calculate the database dependency immediately. if it is set to rtld_now, it is calculated immediately. If it is set to rtld_lazy, it is calculated only when necessary. in addition, you can specify rtld_global so that the libraries that will be loaded later can obtain the symbols.

After the library is loaded, you can use the handle returned by dlopen () as the first parameter of dlsym () to obtain the address of the symbol in the library. with this address, you can obtain the pointer of a specific function in the library and call the corresponding function in the loading library.

6. Use of Dynamic Link Libraries in Linux
Important dlfcn. h header files
When using a dynamic link library in Linux, the source program must contain the dlfcn. h header file, which defines the prototype of the function that calls the dynamic link library. The following describes these functions in detail.
1. dlerror
Prototype: const char * dlerror (void );
When the dynamic link library fails to execute the function, dlerror can return an error message. If the return value is null, the operation function is successfully executed.
2. dlopen
Prototype: void * dlopen (const char * filename, int flag );
Dlopen is used to open the dynamic link library with the specified name (filename) and return the operation handle.
Filename: if the name does not start with "/", it is not an absolute path name and will be searched in the following order.
(1) ld_library value in the user environment variable;
(2) Dynamic Link buffer file/etc/lD. So. Cache
(3) directory/lib,/usr/lib
Flag indicates when to resolve undefined symbols (called). There are two values:
1) rtld_lazy: indicates that it is resolved when the function code of the dynamic link library is executed.
2) rtld_now: indicates that all undefined symbols are resolved before dlopen returns. Once not resolved, dlopen returns an error.
If a dlopen call fails, the return value is null. Otherwise, the return value is the operation handle.
3. dlsym: The function execution address.
Prototype: void * dlsym (void * handle, char * symbol );
Dlsym returns the Execution Code address of the function corresponding to the symbol Based on the handle and symbol of the dynamic link library. The address can include parameters to execute the corresponding function.
For example, program code: void (* Add) (int x, int y);/* describes the dynamic function to be called add */
Add = dlsym ("XXX. So", "add");/* Open the XXX. So shared library and obtain the Add function address */
Add (89,369);/* call the Add function with two parameters 89 and 369 */
4. dlclose: Close the dynamic link library
Prototype: int dlclose (void * handle );
Dlclose is used to close the dynamic link library of the specified handle. It will be uninstalled only when the usage count of this dynamic link library is 0.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.