Dynamic library libxxx.so in Linux

Source: Internet
Author: User

1, the concept of dynamic library . Dynamic link library compared with ordinary programs, there is no main function, which is a series of functions . Production of so dynamic link library files through shared and fpic compilation parameters. When the program calls the library function, it only needs to connect to the library.

2, the advantages of dynamic libraries . . That is, when a program is running to invoke a dynamic link library function, the operating system will first look at all running programs to see if there is a copy of this library function in memory. If so, let it share that copy; only the link is loaded. This mode, while bringing some "dynamic link" additional overhead, greatly saves the system's memory resources. C's standard library is the dynamic link library, which means that all running programs in the system share the same C standard library code snippet. . Users only need to upgrade the dynamic link library, without recompiling the link to other legacy code to complete the entire program upgrade. Windows is a good example. You can even actually sit into the link loading completely by the programmer in the program code control .   Programmers when writing programs, can clearly indicate when or under what circumstances, link load which dynamic link library function . You can have a fairly large piece of software, but each time it runs, only a small number of programs are loaded into memory due to different operational requirements. All functions are based on the principle of " There is demand to be transferred into the ", thus saving system resources greatly. Today's software, for example, typically opens several different types of files, which are often implemented using dynamic-link libraries. In one run, only one type of file will normally be opened. So until the program knows the type of the file and then loads the corresponding read-write function, instead of loading all the read and write functions from the beginning, I realize that they are not used at all in the whole program .

3, the creation of dynamic library . For example, the following implements a simple integer four transport dynamic link library, which defines the caculate.h and caculate.c two files, producing libcac.so dynamic link libraries.

4, the use of dynamic library . Write a test program main.c the function that calls this dynamic-link library. Compiling the production executable main is as follows:gcc main.c-o main-l./-LCAC (where-L indicates the path to the dynamic-link library , and-L is the name of the link library , omitting Lib).

5 . Get the function of the dynamic link library. Linux provides functions for Dlopen, Dlsym, Dlerror, and Dlcolose functions to get a dynamic-link library. Through this four function can implement a plug-in program, convenient program extension and maintenance. The function format is as follows.

Dlopen () is a powerful library function. The function opens a new library and loads it into memory. This function is primarily used to load symbols in the library, which are not known at compile time. Write a test program called the above production libcac.so library as shown below.

6, get a command to rely on the execution of the dynamic library, if there is a dependent library can not be found, then the program will not function properly.

# Absolute path to the LDD command

3, the creation of dynamic library.

Dynamic library libxxx.so in Linux

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.