There are two types of libraries in Linux: dynamic library and static library (shared library)
The difference between the two lies in that the code is loaded at different times.
The code of the static library has been loaded into the executable program during compilation, so the size is relatively large.
The code of the dynamic library (shared library) is loaded into the memory only when the executable program runs. It is referenced only in simple mode during compilation, so the code size is relatively small.
If different applications call the same library, you only need an instance of the dynamic library (shared library) in the memory.
The biggest difference between a static library and a dynamic library is that, in a static situation, the Library is directly loaded into the program. When a dynamic library is linked, it only retains the interface, the dynamic library is independent of the program code, which can improve the reusability of the code and reduce the Coupling Degree of the program.
The static library will be connected to the target code during program compilation,The static library is no longer needed when the program runs..
The dynamic library is not connected to the target code during program compilation, but isRunning the program is loaded, so dynamic inventory is required when the program is running
A static library
The class library name is generally libxxx. a. The files compiled using the static function library are relatively large, because all the data in the function library will be integrated into the target code, and its advantages are obvious, that is, the compiled execution program does not need the support of external function libraries, because all the functions used have been compiled. Of course, this will also become his shortcoming, because if the static function library changes, then your program must be re-compiled.
The code of the static library is linked to the application during compilation. Therefore, the library file must exist during compilation and must be passed to the compiler through the "-l" parameter. When the application starts execution, the library function code will be transferred to the process memory segment along with the program until the process ends. The execution process does not require the original static inventory.
In UNIX, use the AR command to create or operate static libraries.
Ar archivefile objfile
Archivefile: archivefile is the name of the static library.
Objfile: objfile is the intermediate target file name with. O as the extension. Multiple objects can be used in parallel.
Parameter meaning
-R: insert the objfile file to the end of the static library or replace the file with the same name in the static library.
-X Extract files from static library files objfile
-T print the member file list of the static library
-D. Delete the object objfile from the static library.
-S: resetting static library file Indexes
-V Create File Redundancy Information
-C. Create a static library file
Example:
/****************** hello.h **************/void hello(void);
/****************** hello.cpp **************/#include<iostream>#include"hello.h"using namespace std;void hello(void){ cout <<"Hello "<<endl;}
/****************** main.cpp **************/#include"hello.h"int main(int argc,char *argv[]){ hello(); return 0;}
1. compile it into a static library
Both static and dynamic libraries are created by the. o file. Therefore, we must first compile the source program Hello. c into a. o file through GCC.
HC @ linux-v07j :~ /Weiming/tt> G ++-O hello. O-C hello. cpp
HC @ linux-v07j :~ /Weiming/tt> ar cqs libhello. A hello. o
HC @ linux-v07j :~ /Weiming/tt> ls
Hello. cpp hello. h hello. oLibhello.Main. cpp
2. Link
HC @ linux-v07j :~ /Weiming/tt> G ++ main. CPP libhello. a-O out1 (G ++-O aout main. CPP. /libhello. A or G ++-O aout main. CPP-L. /-lhello)
HC @ linux-v07j :~ /Weiming/tt> ls
Hello. cpp hello. h hello. O libhello. A main. cppOut1
HC @ linux-v07j :~ /Weiming/tt> LDD out1
Linux-gate.so.1 => (0xffffe000)
Libstdc ++. so.6 =>/usr/lib/libstdc ++. so.6 (0xb7e36000)
Libm. so.6 =>/lib/libm. so.6 (0xb7e11000)
Libgcc_s.so.1 =>/lib/libgcc_s.so.1 (0xb7e06000)
Libc. so.6 =>/lib/libc. so.6 (0xb7ce3000)
/Lib/ld-linux.so.2 (0xb7f1b000)
Ii. Dynamic library
The class library name is generally libxxx. so; compared with the static function library, the dynamic function library is not compiled into the target code during compilation, and the corresponding function in the function library is called only when your program executes the relevant function, therefore, the executable files generated by the dynamic function library are relatively small. Because the function library is not integrated into your program, but dynamically applied for and called when the program is running, the corresponding library must be provided in the running environment of the program. Changes to the dynamic function library do not affect your program, so it is easier to upgrade the dynamic function library.
Different UNIX systems link dynamic library methods with different implementation details
Compile the PIC type. o The intermediate file method generally uses the-KPIC or-FPIC option of the C language compiler. In some UNIX versions, the C language compiler includes the PIC standard by default. the method for creating the final dynamic library generally uses the-G or-shared option of the C language compiler, or directly uses the tool LD to create.
The most important option is the GCC command line:
-Shared this option specifies to generate a dynamic Connection Library (let the connector generate a T-type export symbol table, and sometimes generate a weak connection W-type export symbol). External programs cannot connect without this sign. Equivalent to an executable file
-FPIC: indicates that the compiled code is in a separate position. If this option is not used, the compiled code is location-related. Therefore, during dynamic loading, the code is copied to meet the needs of different processes, but cannot achieve the purpose of truly sharing code segments.
-L.: indicates that the database to be connected is in the current directory.
-Ltest: an implicit naming rule is used by the compiler to search for a dynamically connected database. That is, add lib before the given name and. So to determine the library name.
LD_LIBRARY_PATH: The environment variable indicates the path where the dynamic connector can load the dynamic library.
If you have the root permission, you can modify/etc/lD. so. CONF file, and then call/sbin/ldconfig to achieve the same purpose. However, if you do not have the root permission, you can only use the LD_LIBRARY_PATH output method.
The source files d1.c and d2.c are compiled into dynamic libraries d1.so and d2.so respectively.
/************ d1.h***************/void print();
/*************** d1.cpp *******************/#include <iostream>#include "d1.h"using namespace stdint p = 1;void print(){ cout<< p <<endl;}
/************ d2.h***************/void print();
/*************** d2.cpp *******************/#include <iostream>#include "d2.h"using namespace std;int p = 2;void print(){ cout<< p <<endl;}
Linux and other GCC Compilers
G ++-FPIC-C d1.cpp d2.cpp/* the intermediate target file d1.o and d2.o */
G ++-shared-O libd1.so d1.o/* create a dynamic library file d1.so */
G ++-shared-O libd2.so d2.o/* create a dynamic library file d2.so */
Or simply one step.
G ++-o-FPIC-shared-O libd1.so d1.cpp
G ++-o-FPIC-shared-O libd2.so d2.cpp
In some versions of GCC, you can also use the-G replace-shared option.
Call a dynamic library
Stealth Call dynamic library
/************** Main. CPP *********************/void print (); // use # include "d1.h" (# include "d2.h") to replace int main (INT argc, char * argv []) {print ();}
# Cp./libd1.so libd. So (CP./libd2.so libd. So)
# G ++-O dout main. cpp./libd. So (or G ++-O dout main. cpp-L./-LD)
HC @ linux-v07j :~ /Weiming/TT/DD> LDD dout
Linux-gate.so.1 => (0xffffe000)
Libd. So =>./libd. So (0xb7f0f000) // This dynamic library file is more than static Compilation
Libstdc ++. so.6 =>/usr/lib/libstdc ++. so.6 (0xb7e2b000)
Libm. so.6 =>/lib/libm. so.6 (0xb7e06000)
Libgcc_s.so.1 =>/lib/libgcc_s.so.1 (0xb7dfa000)
Libc. so.6 =>/lib/libc. so.6 (0xb7cd8000)
/Lib/ld-linux.so.2 (0xb7f12000)
In the preceding example, the dynamic library libd. So is in the same directory as the execution program. If you remove libd. So and then execute the program, the program cannot be executed normally.
When you need to load the dynamic library code, UNIX will find the dynamic library in a certain path
There are two methods to notify the correct location of the dynamic library of UNIX systems .,
1) with compilation path
# G ++-O dout main. cpp./libd. So (or G ++-O dout main. cpp-L./-LD)
When the program is executed, the program automatically operates the dynamic library libd. So in the current path.
2) Change Environment Variables
# Ld_libpary_path = ./
# Export ld_libpary_path
Different UNIX dependent dynamic library search paths environment variable names are different
Search for PATH environment variables in UNIX dynamic libraries
AIX lib_path
Linux ld_libpary_path
Hp_unix paht
Sco unix ld_libpary_paht
One of the advantages of replacing a static library with a dynamic link library is that the library content can be upgraded at any time.
When the dynamic library is replaced by library files with identical interfaces, the executable program can quickly switch to the Code in the new dynamic library, saving the compilation trouble.
For example, change libd2.so to libd. So.
Display the dynamic library for calling
Displays the dynamic library called. library files are not required during compilation and can be dynamically stored in any location during execution. Shared Objects in the library must be applied for before use. Different dynamic library versions, as long as their shared object interfaces are the same, they can be loaded dynamically.
// Open the dynamic library # include <dlfcn. h> void * dlopen (const char * pathname, int mode); // obtain the dynamic library object address include <dlfcn. h> void * dlsym (void * handle, const char * Name); // include <dlfcn. h> char * dlerror (VID); // disable the dynamic library include <dlfcn. h> int dlclose (void * handle );
Dynamic library loading will occupy a certain amount of system resources, such as memory. Therefore, when you do not need to share the dynamic library or do not need to share the dynamic library for a period of time, you need to uninstall it. The function dlclose closes the dynamic library pointed to by the handle parameter and unmounts the memory and other resources it occupies. The handle parameter is invalid after this call.
In fact, because the dynamic library may be shared by multiple processes at the same time, when a process points to dlclose, the resource is not immediately uninstalled. Only when all processes announce that the dynamic library is closed, the operating system starts to recycle dynamic library resources.
Summary:
When compiling a static library, use the-C option first, and then use the AR tool to generate. The way to compile the dynamic library depends on the unxi of different versions. The usage of the implicit call dynamic library is the same as that of the static library, and the function family of the shared library must be dynamically loaded to call the dynamic library.
The method for implicitly calling a dynamic library is the same as that for using a static library. The GCC command for compiling a dynamic library into a target program is the same. When the static library and the dynamic library have the same name, which library file does the GCC command use?
Tests show that when the static library and the dynamic library have the same name, the GCC command takes precedence over the dynamic library. to ensure that the static library is used, the-static option can be added during compilation. Therefore, multiple third-party programs can run normally without the corresponding dynamic library, I like to add-static when compiling the final application.