Knowledge about the library of Linux Programming

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. Static LibraryCodeThe application developed by the developer is connected during compilation.ProgramWhile the shared library is loaded only when the program starts running. 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 enabled when the program is running.
The statement in the program needs to use this function before loading. 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 are not
The program contains the library function content, but only references the library function, so 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. Best database type differentiation
The method is to look at their file suffixes. Generally, the shared library uses. So (shared
The static link library usually ends with. A (archive ). By default, the shared library is usually green, while the static library is black.

Most of the databases that have been developed adopt shared libraries. The executable files in ELF format make shared libraries easier to implement. Of course, the old A. out mode can also be used to share libraries. 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 agreement. developers can use the GNU Library for software development for free, but must ensure that the library functionSource code.

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 consists of. So and version number. The extension name of the static library is.. The suffix of the shared library in the old A. Out format is. SA.
Libname. So. Major. Minor
Libname.

The name can be any string used to uniquely identify a database. The 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 library name: libx11.so. 6. Here X11 is used
Is the library identifier, and the version number is 6.

3. 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. Ldconfig
The usage of the command is mainly in the default search directory (/lib and/usr/lib) and dynamic library configuration file/etc/lD. so. search for shared Dynamic Links Under the directories listed in Conf.
Library (such as lib *. So *), and then create the connection and cache files required for the Dynamic Loading Program (LD. So. The default cache file is/etc/lD. So. cache. This file
Save the list of sorted dynamic link library names. ldconfig runs normally 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. The default value is/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.

When you run the ldconfig command without any options, it is used to update the cache file. This command is mainly used for caching DNS servers ). 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. Library 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.
You need to upgrade the features of the new version. In other words, do not upgrade for upgrade. In Linux, most software is compiled using a shared library, which contains a public sub-instance shared between different programs.
.

When running a program, if you see the following information: "incompatible library version.", You need to upgrade the Library to the required version of the program. The library is backward compatible, that is, 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, you must back up files before performing operations. 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 shared library is soname, short for shared object
Name ). This is a name embedded in the control data for the shared library (. So) file. As mentioned above, each program has a list of libraries to be used. This list contains 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 library and the soname of the old library
Similarly, the old database is used to connect the generated program, and the new database can still run normally. 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 database. 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 shared library loader (also known as a dynamic connector) is also automatically called. It is used 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, which depends on Linux libc
To complete your work, you must use a little external interaction. However, it accepts configuration information in environment variables and configuration files.

The/etc/lD. So. conf file 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 the/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, Apache
Web
The server uses this function to load the module during running, which provides additional capabilities. A configuration file controls the process of loading modules. This mechanism enables you to add or delete a module in the system.
No need to re-compile.

You can use dlopen () in your own program (). Dlopen () is defined in dlfcn. h and in 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
Rtld_now is calculated immediately. If rtld_lazy is set, it is calculated only when necessary. In addition, you can specify
Rtld_global, which allows the libraries loaded later to 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 );
If the function fails to be executed in the dynamic link library, dlerror returns an error message. If the returned value is null, the operation is successful.
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: obtains 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. This address can contain parameters to execute corresponding functions.
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.

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.