1. What is a database?
A large number of libraries exist on Windows and Linux platforms.
Essentially, a library is executable.CodeCan be loaded into memory by the operating system for execution.
Because Windows and Linux are essentially different, their binary libraries are incompatible.
This article only introduces libraries in Linux.
2. Database types
There are two types of libraries in Linux: static library and shared library (dynamic library ).
The difference between the two lies in that the code is loaded at different times.
The static library code has been loaded and executable during compilation.Program, So the volume is large.
The code of the shared library is loaded into the memory only when the executable program runs. It is only referenced in the compilation process, so the code size is small.
3. Significance of inventory
Libraries are existing, mature, reusable code written by others. You can use them, but remember to abide by the license agreement.
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.
The benefit of the shared library is that if different applications call the same library, there is only one instance of the shared library in the memory.
4. How library files are generatedIn Linux
The suffix of the static library is. A, which is generated in two steps.
Step 1. Generate a bunch of. O from source file compilation,Each. O contains the symbol table of this compilation unit.
Step 2. the AR command converts many. O files into. A static library.
The suffix of the dynamic library is. So, which is generated by GCC and specific parameter compilation.
For example:
$ Gcc-FPIC-C *. C $ gcc-shared-wl,-soname, libfoo. so.1-O libfoo. so.1.0 *.
5. How are database files named? Are there any specifications?
In Linux, the library files are generally stored in/usr/lib,
The static library name is generally libxxxx. A, where XXXX is the Lib name
The name of the dynamic library is generally libxxxx. So. Major. Minor, XXXX is the Lib name, major is the main version number, and minor is the minor version number.
6. How to know which libraries an executable program depends on
The LDD command allows you to view a shared library that executable programs depend on,
For example, # LDD/bin/lnlibc. so.6
=>/Lib/libc. so.6 (0 × 40021000)/lib/ld-linux.so.2
=>/Lib/LD-linux. so.2 (0 × 40000000)
The ln command depends on the libc library and LD-Linux library.
7. How to locate shared library files during execution of executable programs
When the system loads executable code, it can know the name of the library it depends on, but it also needs to know the absolute path
In this case, the system dynamic loader (dynamic linker/loader) is required)
The executable program in the ELF format is completed by the ld-linux.so *, which successively searches the dt_rpath segment of the ELF File-environment variable LD_LIBRARY_PATH-/etc/lD. so. cache file list-/lib/,/usr/lib directory find the library file and load it into the memory
8. How can the system find a new library?
If it is installed in/lib or/usr/lib, the LD can be found by default without any other operations.
If it is installed in another directory, add it to the/etc/lD. So. cache file. follow these steps:
1. Edit the/etc/lD. So. conf file and add the path to the directory where the file is stored.
2. Run ldconfig. This command will recreate the/etc/lD. So. cache file.
A library of operating tools
1 nm command to list all symbols compiled into the target file or binary file
One purpose is to view the functions called by the program.
Another purpose is to check whether a given library or target file provides the required functions.
NM [Options] File
NM lists the symbols in the reported file.
-C | -- demangle converts a symbolic name to a user-level name.
-S
-U only displays undefined symbols
-L
The default output format of nm is a list composed of three columns: Symbol value, symbol type, and symbol name.
T: The symbol appears in the text or code area of the target file.
U: The symbol is not defined in this member.
2 AR command
The ar command is used to operate highly structured archive files. This command is most commonly used to create static databases-including one or more target files
Ar can also create and maintain a cross-index table with symbol names, such as a cross-index table between function and variable names and defining their members.
Ar {dmpqrtx} [member] archive files .....
-C if the archive object does not exist, create the archive object from multiple objects without the warning from Ar
-S: Create or upgrade a cross-index ing table between symbols and define their members
-R inserts files into the archive file to replace any existing members with the same name. Add new members to the end of the archive file
-Q: add files to the end of the archive file without checking whether the files are replaced.
3 LDD command
LDD [Options] File
4 ldconfig command
5. Environment Variables and configuration files
The dynamic connector/loader lD. So uses two environment variables.
The first one is LD_LIBRARY_PATH, which is a list of targets separated by colons. You can search for shared libraries at runtime in these directories.
The second is ld_preload, a space-separated, appended shared library specified by the user. It needs to be loaded before all databases are loaded.
LD. So uses two configuration files
/Etc/lD. So. conf lists the directories to search for shared libraries.
/Etc/lD. So. preload is the disk-based version of the environment variable $ ld_preload.
2. Compile and use a static library
A static library (shared library) is a file that contains the target file. These target files are called modules or members and can be reused in pre-compiled code.
To use the library code, you mustSource codeFile contains the appropriate header file and connects to the library
<Stdarg. h> snsic variable length parameter list Tool
Create a static Library:
1. Compile the Code as the target file
Gcc-C liberr. C-o liberr. o
2. Create an archive file with AR:
Ar RCS liberr. A liberr. o
The static library liberr. A is generated. Next, a program is linked to liberr..
GCC errtest. C-o errtest-static-L.-lerr
To verify that the program has been statically linked, use the file command.
File errtest
3. Write and use a static library
Advantages of shared libraries compared with static databases:
1. The Shared Library occupies less system resources.
2. Shared libraries are much faster than static databases at the minimum.
3. Shared libraries greatly simplify code Maintenance
You can use gcc-print-search-dirs to view the default directory of the compiler.
Among them, libraries is the list of search paths of the library files, separated by a: sign.
Objdump-D main // view the main function through Disassembly
Advantages of using static libraries: the connector can extract only the required parts from the static library for link.
Gcc-C-FPIC *****. c
-F is followed by some compilation options. PIC is one of them, indicating that the location is irrelevant to the code.
Objdump-Ds inserts disassembly commands and source code for analysis.
Readelf
Therefore, the loading address of each segment of the shared library is not fixed and can be loaded to any location.
GCC main. C-g-L.-lstack-istack-O main
The link address of the dynamic library is searched by the dynamic connector for the dynamic link, and the search path of the shared library is determined by the dynamic connector. The search path for LD. So (8:
1. First, search for the path recorded by the Environment Variable LD_LIBRARY_PATH.
2. Search for the cached file/etc/lD. So. cache. This cache file is generated after the ldconfig command reads the configuration file/etc/lD. So. conf.
You can add the absolute path of the library file to/etc/lD. So. conf.
3 default path/usr/lib
The process of creating a dynamic library:
1 gcc-FPIC-g-C liberr. C-o liberr. o
Gcc-g-shared-wl,-soname, liberr. So-O liberr. so.1.0.0 liberr. O-lC
Ln-s liberr. so.1.0.0 liberr. so.1
Ln-s liberr. so.1.0.0 liberr. So
Gcc-G errtest. C-o errtest-L.-lerr
$ LD_LIBRARY_PATH = $ (PWD)./errtest
Or
Export LD_LIBRARY_PATH = $ (PWD)
./Errtest
According to the naming convention of the shared library, each shared library has three file names: Real Name, soname, and linker name.
The real library file name is real name, which contains the complete version number of the shared library.
Soname is the name of a symbolic link. It only contains the primary version number of the shared library. The. Dynamic of the referenced program records only the soname of the shared library.
Linker name is only used when compiling links. The GCC-L option should point to the directory where linker name is located.
Recompile libstack to point to its soname
Gcc-shared-wl,-soname, libstack. so.1-O libstack. so.1.0 stack. O push. O pop. O is_empty.o
In this way, the generated libstack. so.1.0 is real name, but the library file records that its soname is libstack. so.1.
If you add the directory of libstack. so.1 to/etc/lD. So. conf and run the ldconfig command, ldconfig automatically creates a soname symbolic link.
If you want to use the shared library during compilation, you should create another linker name link.
Ln-s libstack. so.1.0 libstack. So