Linux Static library and dynamic library usage instructions

Source: Internet
Author: User

Linux under the program run, there are two kinds of libraries, static libraries and dynamic libraries.

Static Library: The name is generally LIBXXX.A, the compilation will be integrated into the executable program, the advantage is that the runtime does not require external library support, the disadvantage is that after the compilation of large programs, once the static library changes, the program needs to be recompiled.

Dynamic Library: The name is generally libxxx. M.n.so,m is the primary version number, and N is the secondary version number. The advantage is that the runtime, the need to dynamically call the external library functions, save space, the disadvantage is that the environment must provide the corresponding library, dynamic library update upgrade is convenient.

Production and use of static libraries

Creation and use of dynamic libraries

The suffix of the dynamic library is. So, typically stored in/lib,/usr/lib and other directories, you can use the LDD tool to see which dynamic libraries are used by an executable program, see:

As you can see, the dynamic library used by our Common LS command is located in the/lib/i386-linux-gnu/directory.

Dynamic link is called by the operating system running the program, since it is the program, that according to the Linux style, there must be a configuration file for the settings, the dynamic link library configuration file is located in/etc/ld.so.conf, the specific content is as follows:

As you can tell, the system searches the path of the dynamic link library in a specific directory. Linux system in order to improve the performance of dynamic link library, put some common dynamic link library in/etc/ld.so.cache this file. How do I update the cache file for this dynamic link library? Linux provides the ldconfig command to update, which primarily searches for/lib and/usr/lib as well as the available dynamic link library files in the ld.so.conf.d/directory of the configuration file, and then re-creates the new dynamic-Link program/lib/ Ld-linux.so.2 the required connections and updates the dynamic-link library cache file Ld.so.cache.

Ldconfig–p | Head or grep XX: see what dynamic Link libraries are in the system

Ldconfig–v output dynamic link library scan directory and refresh Ld.so.cache cache

Root permission is required to execute ldconfig.

Since it is a dynamic link library, it needs to be developed for other people to use. According to the C language style, H header file provides the interface description of the function library, just like the stdio.h header file, the input and output we use must contain this header file. To use our own dynamic link libraries, include the header files provided by the dynamic link library.

Then the build dynamic-link library is compiled

Using the dynamic link library

If you are in the process of team development, it is recommended that you place the dynamic link library in/lib or/usr/lib, or a custom/usr/local/lib/directory, and then add the library path to your library's profile mytest.conf to copy this profile to/etc/ ld.so.conf.d/directory, the execution of Ldconfig is possible.

Dynamic load mode of dynamic link library

Linux provides a set of APIs for dynamically loading dynamic-link libraries, with more flexible use of dynamic-link libraries, with detailed code and comments as follows:

Post-compilation execution:

Precautions:

Executable programs need to add Dlfcn.h header files when using dynamic-link library execution

At compile time, you need to increase the –RDYNAMIC–LDL two parameters to produce execution code that can invoke the dynamic-link library.

Technorati Tags: dynamic library static Library

Reference Links:

Http://blog.chinaunix.net/uid-23069658-id-3142046.html

Linux Static library and dynamic library usage instructions

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.