Programming of static, shared, and dynamic libraries in Linux-it should be helpful to see this running problem.

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.
 
The 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,
 

Article Source: http://www.diybl.com/course/6_system/linux/Linuxjs/20071226/94196.html

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.