Basic use of Linux shared libraries

Source: Internet
Author: User

1. Create and use static databases
Creating a static database is quite simple. Generally, ar programs are used to combine some target files (. O,
Create a separate database and run ranlib to add some index information to the database.

2. Create and use a shared library
Special compilation and connection options
-D_reentrant defines the Preprocessor symbol _ reentrant, which activates some macro features.
-FPIC options generate code with independent locations. Because the library is transferred during running, this
This option is required because the address of the loaded memory is unknown during compilation. If
If this option is not used, the library file may not run correctly.
-The shared option tells the compiler to generate the shared library code.
-Wl,-soname-wl tells the compiler to pass the following parameters to the connector. -Soname specifies
The soname of the shared library.

# You can copy the library file to any directory listed in/etc/lD. So. conf and
Run ldconfig as root; or
# Run export LD_LIBRARY_PATH = 'pwd' to add the current path to the library search path.

1.7.9 use advanced shared database features
1. LDD Tool
LDD is used to show which shared libraries are required for the execution file and where the Shared Library Loading manager finds the required shared library.

2. 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 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. 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 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.

3. Shared Library Loader

When a program is called, the Linux shared library loader (also known as a dynamic connector) is 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 shared library loader is named lD. So or a ld-linux.so, depending on the Linux libc version, which must use a little external interaction to complete its work. 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.

You can use many environment variables to control the operations of the shared library loader (Table 1-4 + ).

Table 1-4 + environment variables of the shared library Loader
Variable meaning
Ld_aout_library_path is the same as LD_LIBRARY_PATH except that the binary format of A. out is not used.
Ld_aout_preload is the same as ld_preload except that the binary format of A. out is not used.
Ld_keepdir is only applicable to the. out library, and the Directories specified by them are ignored.
LD_LIBRARY_PATH adds other directories to the search path. Its content should be a colon
Directory List, which is in the same format as the PATH variable of the executable file.
If you call a program that sets the user ID or process ID, this variable is ignored.
Ld_nowarn is only applicable to the. out library. When the version number is changed, a warning is issued.
Ld_preload first loads User-Defined libraries so that they have the opportunity to overwrite or redefine standard libraries.
Separate multiple entries with spaces. For programs that set user ID or process ID,
Only marked libraries are loaded first. In/etc/lD. So. perload, specify
The global version number. This file does not comply with this restriction.
4. 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 () is defined in dlfcn. h 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.

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.