Creation of dynamic libraries and static libraries

Source: Internet
Author: User

In essence, a library is a binary form of executable code that can be loaded into memory by the operating system. Because of the different nature of Windows and Linux, the binary of the libraries is incompatible. The function libraries supported by Linux operating system are classified into static and dynamic libraries, and dynamic libraries are also called shared libraries. The Linux system has several important directories to store the corresponding function libraries, such as/lib,/usr/lib; The header file is placed in the/usr/include.

Static libraries: The name of such a library is generally libxxx.a; the file compiled from the static function library is larger, because all the data of the library is integrated into the target code, and his advantages are obvious, that is, the compiled execution program does not require external library support, because all the functions used have been compiled into the executable file. Of course this is also called its disadvantage, because if the static function library changes, then your program must be recompiled, and the volume is larger.

Dynamic Library: The name of this kind of library is generally libxxx.so, the dynamic library is also called the shared library, compared with the static function library, the dynamic function library is not compiled into the target code when it is compiled, and your program executes to the related function to call the corresponding function in the function library, so the dynamic function library produces less executable file. Since the library is not integrated into your program, it is applied and invoked dynamically while the program is running, so you must provide the appropriate library in the running environment of the program. The change of dynamic function library does not affect your program, so the upgrade of dynamic function library is more convenient. And if multiple applications are going to use the same library, a dynamic library is a great fit to reduce the volume of your application.

Second, the creation of the function library

A. Creation of a static function library

Ar-rcs libname.a file1.o file2.o file3.o

B. Creation of a dynamic function library

Gcc-shared-fpic-o libname.o file1.c file2.c file3.c

Third, the use of static function library and dynamic function library

Generate a static library

Generate a dynamic library

Using the generated library:

Where: When using the Build library:

-I indicates the path to the specified header file

-L indicates the path of the specified library

-l Specifies the name of the library

Dynamic libraries and static libraries exist at the same time, using dynamic libraries by default. If you want to use a static library, add the-static link option.

Third, let the connection dynamic library's executable to run normally

The newly generated dynamic library does not have the specified path, we need to use the system dynamic loader.

A. The most straightforward way to copy the generated dynamic library to/usr/lib or/lib

B. Using Ld_library_path Environment variables

Export Ld_library_path=/home/app/libs

Creation of dynamic libraries and static libraries

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.