Linux function library uses

Source: Internet
Author: User

The Library of program functions can be divided into 3 types: static function library (libraries), shared function library (GKFX libraries) and dynamic load function library (dynamically loaded libraries)

The static function library is added to the target program before the program executes, while the shared function library is loaded into the program when the program starts, it can be shared by different programs, and the dynamic loading function library can be loaded dynamically at any time when the program is running. In fact, the dynamic function library is not another library function format, but the difference is how the dynamic loading function library is used by programmers. We'll show you the following example

static function library
The static function library is actually a simple set of common target files, which is generally used as a suffix of ". A" as a file. You can use the AR program to generate a static function library file. AR is the abbreviation of archiver. The static library is no longer used as much as it used to be, mainly because the shared library has a lot of advantages over it. Slowly, everyone likes to use the shared function library. However, in some places the static function library is still in use, one to maintain some of the previous program compatibility, and secondly, it is relatively simple to describe.
The static library function allows the programmer to link the program without recompiling the code, saving the time to recompile the code. However, in the face of such a fast computer today, the general program of the recompile will not take much time, so this advantage is not as obvious as it used to be. The static library is useful for developers, for example, if you want to use the functions you provide to others, but you want to keep the source code of the function secret, you can provide a static library file for others. In theory, the code generated using the ELF-formatted static library function can run faster than a program that uses a shared library of functions (or a dynamic library of functions), presumably 1-5%.
To create a static function library file, or to add a new target code to an existing static function library file, you can use the following command:

AR RCS my_library.a file1.o file2.o
In this example, the object code FILE1.O and FILE2.O are added to the My_library.a library file, and a new file is created if the my_library.a does not exist. When creating a static library function with the AR command, there are other parameters that can be selected to participate in the use of AR help. Don't repeat it here.
Once you have created a static function library, you can use it. You can use it as part of your compilation and connection process to generate your executable code. If you compile the executable code with GCC, you can specify the library function with the "-l" parameter. You can also use LD to do this, using its "-L" and "-l" parameter options

Functions in a shared function library are loaded when an executable program is started. If a shared function library is installed properly, all programs can automatically load the latest functions in the library when they are rerun. There are more features that can be implemented for Linux systems:

The library is upgraded but still allows the program to use the old version of the library. When executing a particular program, you can overwrite a specific library or function specified in the library. These libraries can be modified in the course of library functions being used

Shared library naming

Each shared function library has a special name, called "Soname". The soname name must be prefixed with "Lib", followed by the name of the library, then ". So", and finally the version number information. One exception, however, is that the very bottom C library functions are not named after Lib.
Each shared function library has a real name ("real name"), which is a file that contains the code for the real library function. The real name has a major version number, and a release version number. The last release version number is optional and can not be used. The major version number and release number allow you to know exactly what version of the library function you are installing.
In addition, there is a name that is required by the compiler to compile the name of the function library, the name is a simple soname name, and does not contain any version number information. The key to
managing shared libraries is to differentiate the names. When executable programs need to list the shared library functions they need in their own programs, it is only possible to use soname; in turn, when you want to create a new shared function library, you specify a specific file name that contains very detailed version information. When you install a new version of the library, you just have to copy the library files to some specific directory, run Ldconfig this practical. Ldconfig checks for the existing library file and then creates a soname symbol to link to the real library, while setting the/etc/ld.so.cache buffer file. We'll discuss this later.
Ldconfig does not set the name of the link, it is usually done during the installation process to complete the establishment of the link name, in general, this symbolic link simply points to the latest soname or the latest version of the function library file. It's a good idea to point this symbolic link to soname, because normally when you upgrade your library functions, you can automatically use the new version of the function library.
Let's take a look at the example:
/usr/lib/libreadline.so.3 is a fully complete soname,ldconfig can set a symbol to link to some other real library file, such as/usr/lib/ libreadline.so.3.0. There must also be a link name, for example/usr/lib/libreadline.so is a symbolic link to/usr/lib/libreadline.so.3

Dynamically loaded library of functions dynamically Loaded (DL) Libraries

Dynamically loaded library dynamically loaded (DL) libraries is a library of functions that can be loaded at any time during the program's run. They are particularly well suited for loading modules and plugin extension modules in functions, because they can be loaded dynamically when a program needs a plugin module. For example, the pluggable authentication Modules (PAM) system is a dynamic load function library that enables administrators to configure and reconfigure authentication information.

There is no special difference in format between DL libraries and other libraries under Linux, as we mentioned earlier, when they were created in the standard object format. The main difference is that these libraries are not loaded when the program is linked or started, but rather through an API that opens a library of functions, looks for symbol tables, handles errors, and closes libraries. Typically, this header file needs to be included in the C language environment.

The functions used in Linux are the Dlpoen () APIs, as in Solaris. Not all platforms used the same interface, such as HP-UX using the shl_load () mechanism, while the Windows platform used a different other calling interface. If your goal is to make your code highly portable, you should use some wrapping libraries, such that the wrapping library hides the interface differences between different platforms. One approach is to use the support for dynamic loading modules in the GLIBC library, which uses a number of potentially dynamic load function library interfaces to make them boast platform use

Transferred from: http://blog.chinaunix.net/uid-20548668-id-1646748.html

Linux function library uses

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.