Static, shared, and dynamic libraries

Source: Internet
Author: User
Some functions in C language do not need to be compiled, and some functions can be used in multiple files. Generally, these functions perform some standard tasks, such as database input/output operations or screen control. You can compile these functions in advance and place them in some special target code files, which are called libraries. Functions in the library file can be connected to applications through the Connection Program. In this way, you do not have to compile these general functions every time you develop a program.

Different types of applications use different function libraries. For example, the libdbm database group contains the DBM function for accessing database files. Programs that need to operate on the database are connected to the database. The mathematical application will use the mathematical library libm, and the X-Windows application will use the xlib library, libx11. In addition, all programs will use the standard C function library. Libc, which contains basic functions for memory management or input/output operations. These libraries are stored in the directories shared by the/usr/lib system, any user in the system can use these libraries. Of course, you can also create your own library functions for use by yourself or other specified personnel.

The library can be used in three forms: static, shared, and dynamic. The code of the static library is connected to the application developed by the developer during compilation, while the shared library is loaded only when the program starts to run. during compilation, you only need to specify the library function to be used. A dynamic library is another form of change in the shared library. The dynamic library is also loaded when the program is running, but unlike the shared library, the library function used is not started when the program is running, but is loaded only when the program statements need to use this function. The dynamic library can release the memory occupied by the dynamic library during the running of the program, freeing up space for other programs to use. Because the shared library and dynamic library do not include the library function content in the program, but only contain references to the library function, the code size is relatively small.

Most of the databases that have been developed adopt shared libraries. The executable files in ELF format make shared libraries easier to implement. Of course, the old A. out mode can also be used to share libraries. In Linux, the standard format of executable files is elf.

The use of the GNU Library must comply with the library GNU Public License (lgpl license ). This Protocol is slightly different from the GNU license agreement. developers can use the GNU Library for software development for free, but the source code of the library function must be provided to users.

The available databases in the system are stored in the/usr/lib and/lib directories. The library file name consists of the prefix Lib, library name, and suffix. Different database types have different suffix names. The extension name of the shared library consists of. So and version number. The extension name of the static library is.. The suffix of the shared library in the old A. Out format is. SA.

Libname. So. Major. Minor
Libname.

The name can be any string used to uniquely identify a database. The string can be a single word, a few characters, or even a letter. The Library name of the math shared library is libm. so.5. The logo character here is m and the version number is 5. Libm. A is a static Math library. X-Windows Database Name: libx11.so. 6. Here, X11 is used as the library ID, and the version number is 6.

Using the GCC compiler, you can connect the library with your own program, for example, libc. so.5 contains the standard input and output functions. When the connection program connects to the target code, it will automatically search for the program and connect it to the generated executable file. The standard input/output library contains many basic input/output functions, such as the printf function. You can also connect to some other system function libraries, such as math libraries. However, unlike libc. so.5, most other system libraries need to explicitly specify the library name in the command line.

The vast majority of shared libraries can be found in the/usr/lib and/lib directories. The two directories will be searched first during connection. Some libraries may also be stored in specific directories. The list of these directories is provided in the/etc/lD. conf configuration file. The Connection Program will also search for the listed directories. By default, Linux will first search for the shared version of the specified library. If it cannot be found, it will search for the static version. After updating the shared library or installing a new library, you must run the ldconfig command to update/etc/lD. CONF file ).

When referencing a library file in a searchable directory in the GCC compiler, use the-L option and library name. Enter-lm on the GCC command line to connect to the Standard Arithmetic library in the program.-l will first use libname. So for search. Here is libm. So. The following example uses the arithmetic library to create the bookrecs program. Note the-LM option here.

$ GCC main. c Io. C-o bookrecs-LM

There are other available libraries in the system, commonly used libncurses. A library, including some simple mouse movement routines. Use the-lncurses option in the command line to reference the libncurses. So library. The following example calls both the mathematical and cursor libraries.

$ GCC mian. c Io. C-o bookrecs-lm-lncurses

When referencing libraries in other directories, you must use the-ldir option to specify the directory. This option specifies other paths for Searching database functions. In the following example, you use the myio. So library file in the mydir directory during connection.

$ GCC main. C-o bookrecs-lmydir-lmyio

. A is used to support older executable files in the. Out format.
. So is a library that supports executable files in ELF format.

A static library is used to add all the code of the library file to the executable file during compilation and connection. Therefore, the generated file is large, but the library file is no longer needed during runtime. The dynamic library is the opposite. During the compilation and connection, the code of the library file is not added to the executable file. Therefore, the generated file is small, but the library file still needs to be loaded at runtime.

. A is a static library file, which can be generated using the AR command.
. So is a dynamic library file, which can be generated by adding the specified option during compilation. For specific options, see the corresponding system documentation ....

Under ibm aix:
$ (CC) $ (shopt) $ (shlibs) A. o B. O-o Lib $ @ $ (dbbuildtail)

Suppose you have test1.c test2.c test3.c, and write it into a dynamic link library.
1. Compile test1.o test2.o test3.o first.
2. GCC-shared-W1,-soname, libvtest. so.1-O libvtest. so.1.0 *. o

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.