Linux static Link Library and dynamic link library

Source: Internet
Author: User

I. Introduction
For more information, see
On Compiling and linking gcc/g ++ in UNIX systems.

Generally, the links to the function library are 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, because all the required functions have been copied to their own doors. Therefore, these function libraries are called static libaray (static libaray), which is usually named in the form of "libxxx..

In fact, we can also postpone the loading of some library function links to the runtime ). This is 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. resources can be shared between processes.
What is the concept? 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, check whether the library function has been copied in the memory. If there is, share the copy; only no links are 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. You can even sit down to the link and load it completely controlled by the programmer in the 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. For example, the current software can usually open several different types of files, and these read/write operations are usually implemented using dynamic link libraries. In a single run, only one type of file will be opened. So until the program knows the file type, it loads the corresponding Read and Write Functions, instead of loading all the Read and Write functions at the beginning, and then finds that they are not used in the whole program.

3. Create a dynamic link library
Due to the sharing feature of Dynamic Linked Library functions, 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 position independent code (PIC )).

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 practical 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 folder. If the file is not 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:
Setenv LD_LIBRARY_PATH your/full/path/to/DLL

After everything 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.