Linux: Generation and use of static libraries and dynamic libraries

  During the compilation process of the program, it will go through four stages: preprocessing, compiling, assembling, and linking. The linking stage is divided into static linking and dynamic linking. This article is mainly to talk about the content related to the linking stage.

  As mentioned before, in the preprocessing stage, the function declaration in the header file will be copied to its own program, but the function cannot be used only if the declaration is not defined. Therefore, in the linking stage, it is responsible for associating the definition of the function with its own program.

  Links are divided into dynamic links and static links:

  • Dynamic link: link the dynamic library , and record the location information of the function in the library into the program.
  • Static link: link the static library , and copy the function code used in the library to the program.

  This article briefly talks about static libraries and dynamic libraries.

1. Understand the file composition

  First of all, you need to know the things used in the compilation phase: the program you wrote (which calls the function in it), the header file (which contains the function declaration), and the function library (which contains the function implementation).

  1. hello.h: Inside is the declaration of the function
  2. hello.c: is the implementation of the function declaration in hello.h
  3. main.c: call the function in hello.c

  As shown in the figure: the Say function is declared in hello.h, the Say function is implemented in hello.c, and the Say function is called in main.c by including the header file.

component
  Compile and execute this program, because the main file depends on hello.h, hello.c, and main.c, so they must be included when compiling.

execute main

  But do you feel that it is very troublesome, you have to write such a large pile when compiling, and it is not so troublesome when you usually write programs and compile.

  Because we usually use the library functions provided by the system, the system will search for the implementation of the functions in the system library according to the header files we added when compiling. If we don't want to write a bunch of dependent files when compiling, then we can make our own function implementation into a library file, so that we don't need to write so many things when compiling.

  The library files are divided into dynamic libraries and static libraries.

1. Dynamic library

1. Generate a dynamic library

  Use the hello.c file to generate your own dynamic library, a total of two steps:

  1. Process hello.c into a binary instruction file hello.o
    • gcc -fPIC -c .c file -o .o file
  2. Package the hello.o file to generate a library file
    • gcc --shared .o file -o lib custom-name.so

  As shown in the figure, pay attention to the naming rules of dynamic libraries in Linux: lib custom name .so, that is to say, lib and .so are in the prescribed format, and the custom name is the name of the library.

Generate dynamic library

2. Use dynamic library

  After the dynamic library is generated, everything will be fine, because at this time, if it is compiled according to the normal format, an error will still be reported.

report error

  Why is there no such error when using the system library, because the system library files are stored in a specific directory, if we use the functions in the system library, the system will go to the specified directory to find the corresponding library when compiling document. But the library file we made ourselves is not in a specific directory, so the system cannot find it, so it reports an error.

  So there is a problem here. The library files we wrote ourselves are not system libraries at all, so if we use our own library files, the system will not find them when we go to the specified directory to find them, and it will report an error. Therefore, we need to solve this problem.

(1) When generating an executable program, the link is used

  In order to solve the problem that the dynamic library cannot be found when linking, we can use the following methods:

  1. Add the library file we wrote ourselves to the storage directory of the system library file, so that it can be found when the system is linked. But this doesn't feel good, because it always feels like polluting the system library.
  2. Use the environment variable to configure the path of our own library file to the LIBRARY_PATH environment variable, and the system will find our library file path here, and then we can know which library we are linking.
    • export LIBRARY_PATH=${LIBRARY_PATH}: the path where our library files are located

  As shown in the picture:

link dynamic library

(2) Load and use when running the executable program

  When a program that relies on a dynamic library is running, it needs to load the dynamic library into the memory, and there is also a problem here. Because when the system loads the dynamic library, it also loads it in the specified directory, so this problem needs to be solved. The method is as follows:

  1. Add the library files we wrote ourselves to the storage directory of the system library files.
  2. Use the environment variable to configure the path of our own library file to the LD_LIBRARY_PATH environment variable. When the library file needs to be loaded into the memory, the system will find our library file path in it, and then we can know which one we loaded. library.
    • export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}: the path where our library files are located

2. Static library

  Using hello.c to generate our own static library is also a two-step strategy:

  1. Process hello.c into a binary instruction file hello.o
    • gcc (-fPIC optional) -c .c file -o .o file
  2. Package the hello.o file to generate a library file
    • ar -cr lib custom name .a .o file

  As shown in the picture:

Generate static library

2. Use a static library

  The problem encountered by static libraries is also how to find library files when linking. As shown below:

report error

  But the static library does not involve how to find the library file when loading. Because the static library does not need to be loaded, the implementation of the used library functions will be copied directly to our program during the linking phase, so that there is no need to depend on the library file at runtime. The solution is as follows:

  1. Add the library files we wrote ourselves to the storage directory of the system library files.
  2. Use the environment variable to configure the path of our own library file to the LIBRARY_PATH environment variable, and the system will find our library file path here, and then we can know which library we are linking.
    • export LIBRARY_PATH=${LIBRARY_PATH}: the path where our library files are located
  3. Use gcc -L to specify the library file link path
    • gcc main.c -o main -L path library name

  As shown in the picture:

link static library

Guess you like

Origin blog.csdn.net/weixin_57761086/article/details/127561764
Recommended