The Static connection library and dynamic link Library of Linux is not clear, after reading a blog post, now want to do a summary of their own to deepen the impression:
1, the basic concept of the library:
A library is a binary form of executable code that can be called into the operating system to be transferred into memory for execution.
Both the Windows and Linux systems have their own libraries, but the libraries of the two systems are not compatible because their compilers, connectors, and assemblers are not the same.
Under Windows, the suffix of the static connection library is. lib; the dynamic link library suffix is. dll
Under the Linux system, the suffix of the static link library is. A; the dynamic link library suffix is. so
2. Naming of static connection libraries and dynamic link libraries:
Static Connection library: LIBXXXX.A
With the Lib prefix, followed by the static library name XXXX (can own), the suffix is. A
Dynamic Link library: libxxxxx.so
With the Lib prefix, followed by the dynamic library name xxxx (can own), the suffix is. so
The name of the dynamic link library will also be: Libxxxxx.so.major.minor major refers to the main version number of the dynamic library; minor refers to the version number of the dynamic library.
2. Comparison of static link libraries and dynamic Connection libraries:
1) is different when the code is loaded into memory when it is run, when the program compiles the connection, a copy of the static link library needs to be loaded into memory, and when multiple programs are designed to the same static connection library, there are multiple copies in memory;
dynamic-link libraries are loaded into memory only when the program is running, and when you compile the connection, you simply provide a reference to the dynamic-link library (understood as a path).
2) Static library code in the process of compiling has been loaded into the executable program, so the volume is relatively large;
The dynamic library is loaded into memory at run time and is only referenced during compilation and compilation, so the code size is relatively small.
3, why need the existence of the library
Libraries are already existing, mature, reusable code. Can be referenced directly in the program, and reduce the workload.
4, under the Linux system, how to generate static libraries and dynamic libraries
Whether it is a static library or a dynamic library, they are produced in two steps:
The first step: the source file is compiled into the target file, the suffix is. O
Second step: Generate the library according to the command of the dynamic or static library
4.1 Static libraries
First step: gcc-c JIA.C//Generate a JIA.O target file
Step two: ar RCS libjia.a hello.o//Generate Static link library libjia.a
4.2 Dynamic Library
First step: gcc-c JIA.C//Generate a JIA.O target file
Step two: Gcc-shared-fpic-o libjia.so JIA.O//Generate a dynamic library libjia.so
5. Search paths and sequences for static and dynamic libraries:
Whether it is a dynamic library or a static library, there are connector ld to look for
5.1 Static Connection libraries:
1) Find the location specified by the parameters in the GCC command-l
2) Static environment variable library_path the specified search path;
3) default search path for static libraries/lib
4) default search path for static libraries/usr/lib
5.2 Dynamic Link library:
1) The search path of the dynamic library specified in the GCC command;
2) environment variable lid_library_path the specified search path;
3) The search path of the dynamic library specified in the configuration file/etc/ld.so.conf;
4) The search path of the default dynamic library/lib;
5) The search path of the default dynamic library/usr/lib;
6, new installation of a library, how to enable the system to find it
If installed under/lib or/usr/lib, then the default connector ld can be found, do not need other operations;
If you install it in a different directory, you need to add it to the/etc/ld.so.cache file by following these steps:
The first step: Edit the/etc/ld.so.conf file and add the path to the directory where the library files are located;
The second step: Run Ldconfig, the command will rebuild the/etc/ld.so.cache file;