Linux dynamic library, static library

1. What is the library?

A library is a binary form of executable code that can be loaded into memory and executed by the operating system. It is the conversion of the source code into the source code in binary format, which is equivalent to encryption. Others can use the library but cannot see the content in the library.

2. Static library:

When the static function library is added to the target program before the program is executed (compiled)
, the rules of the static library library in the naming system in Linux are:

The naming method of the static library file name is "libxxx.a", the library name is prefixed with "lib", the suffix is ​​".a", and "xxx" is the static library name.
Advantages and disadvantages:

Advantages:
1. The static library is packaged into the program and the loading speed is fast
. 2. The release program does not need to provide a static library. It should already be in the app, which is convenient for transplantation .
Disadvantages:
1. Completely copied to the executable file during linking. There will be multiple redundant copies when used
2. Update, deployment, and release trouble
3. Dynamic library

The dynamic function library is dynamically (temporarily) called by the target program when the program is executed

The rules of dynamic library library in Linux naming system:

Insert picture description here

Advantages and disadvantages:

Advantages:
1. No copying when linking, the program is dynamically loaded into the memory by the system when it is running for the program to call, the system is only loaded once, multiple programs can be shared, saving memory.
2. The program upgrade is simple, because there is no library source code in the app. After the upgrade, as long as the library name remains the same, the function name and the parameters remain the same, and the optimization is implemented, the loading can be successful.
Disadvantages:
1. Loading speed is slower than static library
2. Publishing program needs to provide dependent dynamic library
4. Library production

The production of static library: format xxx.a

Step 1: Generate .o file from point C file

gcc  xxx.c  -c  xxx.o 

Step 2: Generate xxx.a static library file from .o file

ar rcs 静态库的名字   原材料
ar rcs libtest.a     a.o  b.o

The production of dynamic library: format xxx.so

gcc  -shared   -fpic  .c文件  -o  动态库名
gcc  -shared   -fpic  test.c  -o  libtest.so

-shared: specify to generate dynamic library
-fpic: act in the compilation phase, you have to use this option when generating object files to generate position-independent code

5. Use of the library

The use of static libraries:

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

-ltest: "-l" is the library to be used to generate the object file, the library name is cut off to the end
-L: tells the gcc compiler to find the library from the path specified by -L. The default is to find from /user/lib or /user/local/lib

The use of dynamic libraries is the same as static libraries

For programs with dynamic libraries, you can specify to find library files in the path specified by "LD_LIBRARY_PATH "s\ when the program is running

export  LD_LIBRARY_PATH = "/home/pi/back/test"
                           自己指定路径

Guess you like

Origin blog.csdn.net/hhltaishuai/article/details/107714123