Dynamic and static libraries in linux operating system (unfinished)

1. Static library and dynamic library

  • Static library (.a): The program links the library code into the executable file when compiling and linking. Static libraries will no longer be needed when the program is running
  • Dynamic library (.so): The code of the dynamic library is linked when the program is running. Multiple programs share the code of the library. An executable file linked with a dynamic library only contains a table of the entry addresses of the functions it uses, rather than the entire machine code of the target file where the external function is located.
  • Before the executable file starts running, the machine code of the external function is copied from the dynamic library on the disk to the memory by the operating system. This process is called dynamic linking.
  • Dynamic libraries can be shared among multiple programs, so dynamic linking makes the executable file smaller and saves disk space. The operating system uses a virtual memory mechanism to allow a dynamic library in physical memory to be shared by all processes that use the library, saving memory and disk space.

2. Static library

Generate static library

ar -rc libxxx.a add.o sub.o

View the directory listing in the static library

ar -tv libxxx.a
t:列出静态库中的文件
v:verbose 详细信息

 

Link static library

-L specifies the library path -l specifies the library name. After the test target file is generated, the static library is deleted and the program can still run.

library search path

  • Search the directories specified by -L from left to right.
  • Directory specified by environment variable
  • (LIBRARY_PATH) Directory specified by the system

  • /usr/lib

  • /usr/local/lib

3. Dynamic library

Generate dynamic library

  • shared: indicates generating a shared library format
  • fPIC: Generate position independent code (position independent code)
  • Library name rules: libxxx.so

Use dynamic libraries

compile options

  • l: Link to the dynamic library, as long as the library name is enough (remove lib and version number)
  • L: The path where the link library is located.

Here's something that makes me confused

If this is not done in one step

Instead use it like this

The two .gch formed as follows cannot be recognized by the command to package the static library 

Compile 1 through dynamic libraries to form executable files

 

Guess you like

Origin blog.csdn.net/m0_74234485/article/details/132631950