Production of static library and dynamic library under linux

1. Production of static libraries

The naming rules of the library under linux: under linux, it is named libXXX.a, and the lib (library) prefix is ​​fixed, which means that this is a library. Next, introduce the production process of the static library.

1.1 Obtain the .o file through gcc compilation

Generally, the source program is preprocessed to expand the header files and macros, compiled to generate an assembly code .s file, and then compiled to generate an object code .o file (which contains some incomprehensible machine code). After completing the above three steps, you can get the .o file.

Create fun1.c file, fun2.c file, head.h file, the contents are very simple.

The o file can be generated by gcc -c fun1.c fun2.c code. -c is the compilation option of the gcc compiler, which means to compile and assemble the specified source file, but not to link.

1.2 Pack the .o file and use the ar tool (archive)

ar rcs libxxx.a xxx.o xxx.o

The meaning of rcs on the command line.
r - insert a file into a backup file (library)
c - create a backup file
s - index

It can be seen that the libtest.a static library file has been generated.

1.3 Use of static libraries

Delete the generated .o file and the original .c file, leaving only the library file and header file, and write the test program test.c. Under normal circumstances, the use of library files requires supporting header files, so that we or others can know what functions this library has that can be used. The code of the test file is as follows, which is using the functions in the library file.

Use the following compilation directives

gcc test.c -o test -ltest -L./

Among them, -l means to specify the library to be used when compiling the program. The name here is test, which does not contain the previous lib word; -L means to specify the path to search for the library when compiling, and ./ represents the current directory.

You can see that a green executable file has been generated, enter the ./test executable file, and you can see the normal output.

2. Production of dynamic library

2.1 Dynamic library generation process

The fun1.c, fun2.c, and head.h here are the same as before, and they all output a sentence.

gcc -c -fpic fun1.c fun2.c

Generate .o files without location. You can see that the fun1.o and fun2.o files are generated below. Among them -c means to compile and assemble the specified source file, but not to link; -fpic means to generate position-independent code;

gcc -shared fun1.o fun2.o -o libtest.so

Use the appeal command to generate a dynamic library file from the generated .o file. Among them, -shared indicates that a shared object file (dynamic library) is generated, and -o indicates the name and type of the object file.

2.2 Use of dynamic libraries

Compile the test source file with gcc

gcc test.c -o test -ltest -L ./

Among them, -l means to specify the library to be used when compiling the program. The name here is test, which does not contain the previous lib word; -L means to specify the path to search for the library when compiling, and ./ represents the current directory.

You can see that a green executable file has been generated. Enter the ./tset executable file. A new error occurs. When loading the shared library, the library cannot be opened.

Check the dynamic library dependencies through the ldd (list dynamic dependencies) command. You can see that the test source program depends on 4 dynamic libraries. The other three libraries have corresponding absolute addresses. In the second line, the dynamic library generated by ourselves shows not found . , which corresponds to the previous error, it cannot find the location of this dynamic library.

When compiling, the main program only knows the name of the dynamic library, but does not know the specific address of the dynamic library. When the program is started and a function in the dynamic library is used, the dynamic library will be dynamically loaded into the memory. This is different from static libraries. In static libraries, all codes are packaged into executable programs during linking, and all work is completed during compilation. When loading a dynamic library, the system's dynamic loader (ld-linux-x86-64.so) is needed to obtain its absolute path, which is the fourth line above.

It successively searches the DT_RPATH section of the file, the environment variable LD_LIBRART_PATH, the /etc/ld.so.cache file list, the /lib/, /usr/lib directories to find the library file and then loads it into the memory. When we install certain software, we will set environment variables , which actually allows the software to use dynamic libraries on the system.

Under normal circumstances, the DT_RPATH segment cannot be changed, so it is set here in the environment variable.

You can use the methods introduced in the previous article for processing. Here we choose a method to introduce.

Find the absolute path of the folder through the pwd command

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/nero/lib_make

Through the above command, add the absolute path of the self-made dynamic library to the environment variable. The export command is used to set or display environment variables. When you execute a program in the shell, the shell provides a set of environment variables. export can add, modify or delete environment variables for use by subsequent programs.

Observing through the ldd command, you can find that the Libtst.so dynamic library is no longer not found.

Execute it, it can run normally.

3. The advantages, disadvantages and differences between dynamic libraries and static libraries

the difference

Static library: When GCC links, it will package the code in the static library into an executable program, which is called the static linking method;

Dynamic library: When GCC links, the code of the dynamic library will not be packaged into the executable program, but only some key information will be copied, which is called dynamic linking.

Advantages of static libraries:

Static libraries are packaged into executable programs and load quickly;

There is no need to provide static libraries when publishing programs, making porting easy;

Disadvantages of static libraries:

Consumes system resources and wastes memory;

Updating, deploying, publishing troubles;

Advantages of dynamic libraries:

Resource sharing between processes can be realized;

Simple update, deployment and release;

Can control when to load dynamic libraries;

Disadvantages of dynamic libraries:

Loading speed is slower than static library;

Publishing programs need to provide dependent dynamic libraries

Summarize

It can be found that the advantages and disadvantages correspond to each other, because the static library is directly packaged into the program, so the loading speed is fast, but at the same time, there are 100 static library codes for 100 programs, and the disadvantage is a waste of memory. Because it is packaged directly, porting is very convenient, but it also means that if you modify the contents of the library, you have to recompile the entire program.

Guess you like

Origin blog.csdn.net/Damon_Sandy/article/details/130108142