C language library-static library and shared library

Source: Internet
Author: User
Tags database sharding

When writing a C language program, we often encounter many repeated or commonly used parts. If we re-write each time, it is acceptable, but this will greatly reduce the work efficiency, it also affects code readability and is not conducive to code maintenance in the future. We can make them into corresponding function functions. It will be convenient to call them directly during use, and we can also upgrade the functions later.

For example, if I want to exchange the values of two variables multiple times in a piece of code, I can write multiple times in the code.

I = X;
X = y;
Y = I;

However, this makes it easy to write a change_two_int () function.
Define the following functions:
Void change_two_int (int * a, int * B)
{
Int C;
C = *;
* A = * B;
* B = C;
}
In this way, you only need to call change_two_int (& X, & Y); For each exchange. Is it much easier?

So what do we have to discuss with them? Library is to package the target files of these common functions together to provide corresponding function interfaces for programmers to use. Libraries are existing, mature, reusable code written by others. We only need to know how their interfaces are defined and can use them freely.

In reality, every program depends on many underlying libraries. It is impossible for everyone to start from scratch. Therefore, the existence of libraries is of extraordinary significance. For example, the commonly used printf function is the function provided by the C standard library. We only need to include the corresponding header file for use (non-static compilation also requires the corresponding library file ). Instead of worrying about how the printf function is implemented, this greatly improves the efficiency of coding by programmers. In terms of usage, database sharding can be divided into two types: static database and shared database. In Windows, static libraries are files suffixed with. Lib, and shared libraries are files suffixed with. dll. In Linux, the static library is a file suffixed with. A, and the shared library is a file suffixed with. So.
Take the static and dynamic libraries in Linux as an example. First, let's take a look at their generation methods.

Static Library:
First, compile the source file into the target file: gcc-c a. c B. C
Generate static library: Ar-RC libstatic. a A. o B. O

Shared Library:
Compile the file as the static library: gcc-c a. c B. C
Generate a shared library: gcc-FPIC-shared-O libshared. So a. o B. o

It can be seen that both the static library and the dynamic library process the target file. It can also be said that the library file is already a machine code file, and the loading process of the static library and the shared library is very different.

Static Library Link Method:
Gcc-O staticcode-L.-lstatic main. C-static (the default library is in the current folder)

How to link a shared library:
Gcc-O sharedcode-L.-lshared main. C (the default library is in the current folder)

When the program is connected to the static library, all the machine codes of the functions used by the program in the target file in the library are copied to the final executable file. This will lead to a relatively large amount of executable code generated, which is equivalent to the completion of the Code by the compiler, so it runs relatively faster. However, there is a drawback: occupying disk and memory space. the static library will be added to every program connected to it, and these programs will be loaded into the memory. virtually consumes more memory space.

The executable file connected to the shared library only contains the reference table of the functions required by the shared library, instead of all the function code. Only when the program is executed, the required function code is copied to the memory. In this way, the executable files are relatively small, saving disk space. Furthermore, the operating system uses virtual memory, allowing a shared library to reside in the memory and be used by multiple programs, it also saves memory. However, it takes a certain amount of time to connect to the database during running, and the execution speed is relatively slow. In general, the static database sacrifices the space efficiency, in exchange for the time efficiency, shared libraries sacrifice time efficiency in exchange for space efficiency. There is no difference between good and bad, but it depends on the specific needs.

In addition ,. after a program is compiled, some modifications and optimizations are sometimes required. If we want to modify a library function, the interface remains unchanged, the program that uses the Shared Library only needs to re-compile the shared library. The program that uses the static library needs to re-compile the static library, and then re-compile the program.

Database operation commands

Nm

Function:
List all symbols that are compiled into the target file or binary file. Purpose 1: Check the functions called by the program. Use 2 to check whether a given library or target file provides the required functions.

Syntax: Nm [Options] File

Common options:
-C converts a symbolic name to a user-level name. It is particularly useful in making C ++ function names readable.
-S: when used in the. A file, the output maps the symbol name to the index of the module or member name that defines the symbol.
-U only displays undefined symbols, that is, files externally defined in the file to be checked.
-L use the debug information output to define the line number of each symbol or the important bit of the undefined symbol.

Ar

Function: combines multiple. O files into a. a file.

Syntax: Ar [Options] lib *. A *. o

Common options:
-C if the archive file does not exist, create the file and do not display the AR warning.
-Q: Add *. O to the end of the archive file without checking whether to replace the file.
-R inserts the. o file into the archive file, replaces any existing files with the same name, and adds new members to the end of the file.
-S creates or upgrades the cross-index ing table from the symbol to the. A file and adds it to The. A file.
It is equivalent to ranlib [*. A]. After executing this command, you can use nm-s to view the generated index.

LDD
Function: displays the shared libraries required for executable programs.
Syntax
LDD [Options] File

Common options:
-D. Execute the relocation and report all the missing functions.
-R migrates functions and data objects and reports any missing functions or data objects.

Ldconfig

Function:
In the default search directory (/lib and/usr/lib) and dynamic library configuration file/etc/lD. so. search for the shared dynamic link library (lib *. so *) to create a dynamic loader (LD. so) the required connection and cache file. The default cache file is/etc/lD. So. cache. This file stores a list of Dynamic Linked Library names that have been sorted. This command is run when the system starts. When you install a new dynamic link library, you need to manually run this command.

Syntax:
Ldconfig [Options] path

For example, ldconfig/root/lib allows the system to share the dynamic link library under the/root/lib directory, that is, add the shared library under the specified directory to/etc/lD. So. cache. [Note] If the directory is not in/lib,/usr/lib,/etc/lD. in the directory list listed in soconf, when ldconf is run again, the dynamic link library under this directory will not be shared by the system.

Common options:
-V updates/etc/lD. So. cache content, version numbers of each database in the column, scanned directories, and all created and updated links.
-P Only displays the content of/etc/lD. So. cache, that is, the current list of shared libraries known to lD. So.
-N ldconf only scans the directory specified by the-n command
-F conf specifies that the configuration file of the dynamic link library is Conf, and the default value is/etc/lD. So. conf.
-C cache specifies that the generated cache file is cache. The default value is/etc/lD. So. cache.
When ldconf does not include any option, only the high-speed buffer file is updated.

Environment Variable
$ Ld_preload: A list of shared libraries separated by spaces. These libraries are loaded before other libraries so that they have the opportunity to overwrite or redefine standard libraries.
$ LD_LIBRARY_PATH is a list of directories separated by colons, which are accessed during shared library search.

Source: http://www.programbbs.com/bbs/view35-23657-1.htm

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.