Static library and dynamic library production


foreword

When we write code, we often use existing interfaces, which are provided to us in the form of libraries, and there are two common forms, one is often suffixed with .a, which is a static library; the other is suffixed with .so is the suffix and is a dynamic library. The so-called static and dynamic refers to the linking process.


1. Introduction to static library and dynamic library

1. Static library

When the program is compiled and linked, the code of the library is linked into the executable file, and the static library is no longer needed when the program is running.

The static library is statically expanded in the file, so it can be expanded as many times as there are files, which consumes a lot of memory. If 100M is expanded 100 times, it is 10G, but the advantage of this is that the static loading speed is fast.
insert image description here

2. Dynamic library

The code of the dynamic library is only linked when the program is running, and multiple programs share the code of the library.

Using a dynamic library will load the dynamic library into the memory, and 10 files only need to be loaded once, and then these files will be temporarily loaded when they are used in the library, which is slower but saves memory.
insert image description here

2. Production and use of static libraries

The name of the static library starts with lib and ends with .a , for example: libmylib.a

Instructions generated by the static library:

ar rcs libmylib.a file1.o

1. Prepare the source code

test.c

#include <stdio.h>
#include "sub.h"

int main(int argc, char *argv[])
{
    
    
    int i = 6, j = 3;
    int k;

    printf("main fun! && i = %d\n", i);
    k = sub_fun(i, j);
    printf("k = %d\n", k);

    return 0;
}

sub.c

#include <stdio.h>

int sub_fun(int a, int b)
{
    
    
    printf("sub fun!\n");
    return a - b;
}

sub.h

int sub_fun(int, int);

2. Compile source code to generate .o file

gcc -c sub.c -o sub.o

insert image description here

3. Make a static library

在这里插入代码片

insert image description here

4. Use a static library

gcc test.c libmymath.a -o test
./test

insert image description here
The test.c file is only 213 bytes, while the test file is 16K in size, so when the static library is used, it is directly compiled into the file.
insert image description here

3. Production and use of dynamic library

The name of the dynamic library starts with lib and ends with .so , for example: libmylib.so

1. Generate position-independent .o files

gcc -c sub.c -o sub.o -fPIC

insert image description here
After using the -fPIC parameter, the generated function has nothing to do with the position, hang the @plt flag, and wait for dynamic binding

2. Create a dynamic library

gcc -shared -o libmymath.so sub.o

insert image description here

3. Use dynamic library

  • -l : specify the library name
  • -L : Specify the library path
gcc test.c -o test -l mymath -L ./
./test

insert image description here
Analysis of the cause of the error:

  • Linker: Works in the linking phase, requires -l and -L to work
  • Dynamic linker: works in the program running stage, and needs to provide the directory location of the dynamic library when working

Solution : specify the dynamic library path and make it effective, and then execute the file

4. Specify the dynamic library path and make it effective

Specify the location of the dynamic library through the environment variable: export LD_LIBRARY_PATH=dynamic library path

export LD_LIBRARY_PATH=./
./test

insert image description here
When the terminal is closed and the test is executed again, an error is reported again.
insert image description here
This is because the environment variable is the concept of a process. After closing the terminal and opening it, there are two processes, and the environment variable has changed.
To take effect permanently, you need to modify the bash configuration file: vi ~./bashrc

Permanent method:

  • vi ~/.bashrc
  • Write export LD_LIBRARY_PATH=dynamic library path save
  • source ~/.bashrc, then restart the terminal —> let the modified .bashrc take effect
  • execute ./test

insert image description here

4. Comparison

1. Advantages and disadvantages of static libraries

  • Fast loading of static libraries
  • The release program does not need to provide a static library, which is convenient for porting
  • Consumes system resources and wastes memory
  • Updating, Deploying, and Publishing Hassle

2. Advantages and disadvantages of dynamic libraries

  • Resource sharing between processes can be realized
  • Simple to update, deploy, and publish
  • Can control when dynamic libraries are loaded
  • Loading speed is relatively slow
  • The dependent dynamic library needs to be provided when publishing the program

My qq: 2442391036, welcome to communicate!


Guess you like

Origin blog.csdn.net/qq_41839588/article/details/132255029