Static libraries and dynamic libraries
In Windows, a static library is a file with a. lib suffix, and a shared library is a file with a. dll suffix. In Linux, a static library is a file with a suffix of. A, and a shared library is a file with a suffix of. So.
Taking the static and dynamic libraries under Linux as an example, let's look at how they're generated, first of all.
Static libraries:
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 libraries:
Compile as a target file as a static library: Gcc–c A.C B.C
Build shared library: Gcc–fpic–shared–o libshared.so a.o B.O
This shows that both static and dynamic libraries are the processing of the target file, it can be said that the library file is a machine code file, static library and shared library loading process is very different.
How to link static libraries:
Gcc–o staticcode–l.–lstatic main.c–static (default library in current folder)
How to link a shared library:
Gcc–o sharedcode-l.–lshared main.c (default library in current folder)
When the program is connected to a static library, all the machine codes contained in the target file in the library will be copied to the final executable file for all functions that are used by the program. This results in a relatively variable amount of executable code being generated, which is equivalent to the compiler adding the code completely, which is relatively faster to run. However, there is a drawback: taking up disk and memory space. The static library is added to each of the programs it connects to, and the programs are loaded into memory when they run. It also consumes more memory space.
An executable that is connected to a shared library contains only the reference table for the function it requires, not all function code, and only those required function code are copied into memory when the program executes. This makes the executables smaller, saves disk space, and further, the operating system uses virtual memory, which allows a shared library to reside in memory for use by multiple programs, while saving memory. However, because the runtime to link the library will take a certain amount of time, the execution speed is relatively slow, in general, the static library is sacrificed space efficiency, in exchange for time efficiency, shared library is sacrificed time efficiency in exchange for space efficiency, no good and bad difference, only see the specific needs.
In addition, after a program has been compiled, sometimes need to do some modification and optimization, if we want to modify is just the library function, in the premise of the interface is not changed, the use of shared libraries only need to recompile the shared library, and the use of static library program will need to recompile the static library, the program will be recompiled again.
related commands for library operations
nm
Function:
Lists all the symbols that are programmed into the destination file or binary file. Purpose one: To see what function the program calls; purpose two: To see if a given library or target file provides the required function.
Syntax: NM [options] File
Common options:
-C converts the symbol name to the user-level name. is particularly useful for making C + + function names readable.
-S when used with a. A file, the output maps the symbol name to the index of the module or member name that defines the symbol.
-U displays only undefined symbols, which are files defined outside of the file being inspected.
-L uses the debug information output to define the line number of each symbol, or the important bits of the symbol are undefined.
ar
Function: Combine multiple. o files together to become. a files.
Syntax: AR [options] lib*.a *.O
Common options:
-C If the archive file does not exist, it is created and does not display an AR alert.
-Q adds the *.O to the end of the archive file without checking for replacements.
-R inserts an. o file into the archive file, replaces any existing file with the same name, and adds the new member to the end of the document.
-S creates or upgrades a cross-index mapping table from the symbol to the. A file, and adds it to the. a file.
Equivalence and Ranlib [*.A]. After the command is executed, nm–s can be used to view the generated index.
LDd
Function: Displays the shared libraries required to run the executable program.
Grammar
LDD [Options] File
Common options:
-D performs a relocation and reports all missing functions.
-R performs relocation of functions and data objects and reports any missing functions or data objects.
Ldconfig
Function:
Under the directories listed in the default search directory (/lib and/usr/lib) and in the dynamic library configuration file/etc/ld.so.conf, a shareable dynamic-link library (lib*.so*) is searched to create the connection and cache files required for the dynamic loader (ld.so). The cache file defaults to/etc/ld.so.cache, which holds a list of sorted dynamic-link library names. This will run when the system starts, and when the user installs a new dynamic link library, the command needs to be run manually.
Grammar:
ldconfig [Options] Path
For example: Ldconfig/root/lib lets the system share a dynamic-link library under the/root/lib directory, that is, adding a shared library under the specified directory in/etc/ld.so.cache. [note] If the directory is not in the list of directories listed in/lib,/usr/lib,/etc/ld.soconf, the dynamic-link library under this directory will not be shared by the system when ldconf is run again.
Common options:
-V updates the contents of/etc/ld.so.cache, lists the version number of each library, scans the directory, and links to all created and updated.
-P displays only the contents of the/etc/ld.so.cache, which is the current list of shared libraries known to ld.so.
-N ldconf scan only the directory specified by the-n command
-F CONF Specifies that the configuration file for the dynamic-link library is CONF and the system defaults to/etc/ld.so.conf.
The-c cache specifies that the generated cache file is cached and the system defaults to/etc/ld.so.cache.
When ldconf does not have an option, only the cache files are updated.
Environment Variables
$LD _preload the list of shared libraries separated by spaces, loaded before other libraries, giving them an opportunity to overwrite or redefine the standard library.
$LD _library_path a colon-delimited list of directories that are accessed when shared library searches