Add Linux dynamic Connection Library

Source: Internet
Author: User
I. Introduction

Generally, the link to the function library is completed during the compilation period (compile time. All related object files and libraries are linked to form an executable file ). When the program is running, it has nothing to do with the function library. All required functions have been copied to the executable file. Therefore, these function libraries are called static libaray and are named in the form of "libxxx..
In fact, we can also postpone the loading of the library function link to the runtime ). This is also known as the dynamic link library technology.

Ii. features and advantages of Dynamic Link Library

First, let's take a look at the benefits of postponing the library function to load during the running of the program:
1. Resource Sharing between processes
That is to say, when a program is running to call a Dynamic Linked Library function, the operating system first checks all the running programs to see if there is a copy of the library function in the memory. If yes, share the copy that already exists in the memory; otherwise, the link needs to be loaded. Although this mode brings some additional overhead of "Dynamic Links", it greatly saves the system's memory resources. C's standard library is a dynamic link library, that is, all running programs in the system share the code segment of the same C standard library.
2. upgrade some programs easily. You only need to upgrade the dynamic link library, instead of re-compiling the link to other original code to complete the upgrade of the entire program. Windows is a good example.
3. Even link loading can be completely controlled by programmers in program code.
When writing a program, the programmer can specify when or when to load the dynamic link library function. You can have a relatively large piece of software, but each time you run it, only a small part of the program is loaded into the memory due to different operation requirements. All functions are based on the principle of "calling only when needed", which greatly saves system resources.

3. Create a dynamic link library

Compile Parameter Parsing
The most important option is the GCC command line option-shared, which specifies to generate a dynamic Connection Library (let the connector generate a T-type export symbol table, sometimes it also generates a weak-connection W-type export symbol). External programs cannot connect without this sign, which is equivalent to an executable file.

-FPIC: indicates that the compiled code is in a separate position. If this option is not used, the compiled code is location-related. Therefore, during dynamic loading, the code is copied to meet the needs of different processes, but cannot achieve the purpose of truly sharing code segments.

-Lpath: indicates the library to be connected to in this directory.

-LXXX: an implicit naming rule is used by the compiler to search for a dynamically connected database. That is, the name is preceded by LIB and followed by. So to determine the name of the database.

LD_LIBRARY_PATH: The environment variable indicates the path where the dynamic connector can load the dynamic library.

If you have the root permission, you can modify/etc/lD. so. CONF file, and then call/sbin/ldconfig to achieve the same purpose. However, if you do not have the root permission, you can only use the LD_LIBRARY_PATH output method.

Because of the sharing feature of dynamic linked libraries, they are not copied to executable files. During compilation, the compiler only performs checks such as function names. When the program is running, the called Dynamic Linked Library Function is placed somewhere in the memory, and all programs that call it will point to this code segment. Therefore, these codes must use relative addresses instead of absolute addresses. During compilation, we need to tell the compiler that these object files are used for dynamic link libraries, so we need to use the position independent code ).

For the GCC compiler, you only need to add the-FPIC label, such:
Gcc-FPIC-C file1.c
Gcc-FPIC-C file2.c
Gcc-shared libxxx. So file1.o file2.o

Notice the last line. The-shared tag tells the compiler that this is to create a dynamic link library. This is very different from the creation of static link libraries. The latter uses AR commands. It is also noted that the name of the dynamic link library is in the form of "libxxx. So" and the suffix is ". So ".

Iv. Use of Dynamic Link Library

When using a dynamic link library, you first need to let the compiler check some syntax and definitions during compilation.
This is basically the same as the use of static libraries. The-lpath and-LXXX labels are used. For example:
GCC file1.o file2.o-lpath-LXXX-O program.exe

The compiler first searches for the libxxx. So file in the path directory. If no file is found, it searches for libxxx. A (static library ).

During the program running, you also need to tell the system where to find your dynamic link library file. In UNIX, the environment variable LD_LIBRARY_PATH is defined. You only need to assign the PATH value to this variable.
CSH command:
After setenv LD_LIBRARY_PATH path_to_dll is properly arranged, you can run the LDD command to check whether the connection is normal.
LDD program.exe
If everything goes well, it will output a list of the function libraries you use. Otherwise, you will complain that a database cannot be found.

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.