Use of GCC dynamic libraries and static libraries

Article directory

  • 1. Definition of library
    • ·The difference between dynamic libraries and static libraries
      • 1. Static library:
        • advantage
        • shortcoming
      • 2. Dynamic library:
        • advantage
        • shortcoming
  • 2. Creation and use of libraries
    • 1. Preparation
      • Ⅰ Prepare .h header file and corresponding .c file
      • Ⅱ Generate .o file
    • 2. Generate library
      • Ⅰ Static library
      • Ⅱ Dynamic library
    • 3. Use the library
      • Ⅰ Static library
      • Ⅱ Dynamic library
  • 3. Examples
    • 1. Create file
    • 2. Generate .o file
    • 3. Generate static library
    • 4. Generate dynamic library
    • 4. Compile into executable file
    • 5. Test
      • Ⅰ Test the main link of the static library
      • Ⅱ Compile and link main2 of dynamic library
        • ① First copy liboxx.so to usr/lib/
        • ② Compile main2
        • ③ Test

1. Library definition

Readers can equate the so-called library file to a compressed package file, which usually contains more than one target file (that is, a binary file).

It is worth mentioning that the code stored in each object file in the library file is not a complete program, but practical functional modules. For example, C language library files provide a large number of functions (such as scanf(), printf(), strlen(), etc.), and C++ library files not only provide useful functions, but also a large number of pre-designed classes (such as string string class).

The generation of library files has greatly improved the development efficiency of programmers, because many functions do not need to be developed from 0 at all, and the library file containing the function can be directly called. Moreover, the calling method of the library file is also very simple. Taking the printf() output function in C language as an example, you only need to introduce the header file into the program to call the printf() function.

Why does calling a library file involve a header file? First of all, header files and library files are not the same thing. The biggest difference between them is:

The header file only stores the declaration part of these functional modules such as variables, functions or classes;
The library file is responsible for storing the specific implementation parts of each module;
Readers can understand this: all library files provide corresponding header files as the interface for calling them. In other words, the library file cannot be used directly and can only be called indirectly through the header file.

The biggest advantage of the access mechanism that combines header files and library files is that sometimes we just want others to use the functions we have implemented, and do not want to disclose the source code of the functions, so we can make them into library files so that users can obtain is a binary file, and the header file only contains the declaration part, thus achieving the purpose of “hiding the source code” without affecting user use.

In fact, library files are just a general term that refers to a type of compressed packages, which all contain functional and practical target files. You should know that although library files are used in the linking phase of the program, the compiler provides two ways to implement linking, called static linking and dynamic linking.

Library files that use static linking to implement linking operations are called static link libraries;
Library files that use dynamic linking to implement link operations are called dynamic link libraries;
In the actual development process of C and C++, in addition to using system library files, we can also manually create static link libraries or dynamic link libraries according to actual needs.

From:
GCC compiles the creation and use of C (C++) static link libraries (gcc -L, gcc -l) and dynamic link libraries (gcc -fPIC -shared)

·The difference between dynamic libraries and static libraries

1. Static library:

Static libraries are library files that are completely copied into the executable file at link time. When the program runs, the code of the static library is loaded into memory and executed together with the program.

Advantages

The use of static libraries is simple. You only need to link the library file to the executable file, without any other dependencies.
The execution speed of static libraries is relatively fast because the code has been completely copied into the executable file, and there is no need for dynamic loading and parsing.

Disadvantages

Static libraries are larger in size and will increase the size of the executable file.
If multiple executable files use the same static library, it will cause repeated code duplication and waste storage space.

2. Dynamic library:

Dynamic libraries are library files that are loaded and linked when the program is running. When the program needs to use the functions of the dynamic library, the dynamic library will be loaded into the memory, and then the function will be executed.

Advantages

The size of the dynamic library is relatively small and does not increase the size of the executable file.
Multiple executable files can share the same dynamic library, reducing the duplication of duplicate code and saving storage space.
It is more convenient to update and upgrade dynamic libraries. You only need to replace the library files without recompiling the executable files.

Disadvantages

The use of dynamic libraries is relatively complex and requires dynamic loading and linking of library files at runtime.
The execution speed of dynamic libraries is relatively slow because the library files need to be loaded and parsed dynamically.

2. Creation and use of libraries

1. Preparation

Both dynamic libraries and static libraries are created based on .o files, so you need to get the .o files through gcc

Ⅰ Prepare .h header file and corresponding .c file

like:
Declare a print1(int arg) function in A1.c

#include <stdio.h>
void print1(int arg){<!-- -->
printf("A1 print arg:%d\\
",arg);
}

Declare a print2(char *arg) in A2.c (the number of .c files is arbitrary, as long as it is included in the header file)

#include <stdio.h>
void print2(char *arg){<!-- -->
printf("A2 printf arg:%s\\
", arg);
}

Declare a header file A.h

#ifndef A_H
#define A_H
void print1(int);
void print2(char *);
#endif

Declare a test file test.c for testing the library

#include <stdlib.h>
#include "A.h"
int main(){<!-- -->
print1(1);
print2("test");
exit(0);
}

Ⅱ Generate .o file

gcc -c [filename.c]

Note: If you want to generate a dynamic library later, you must add “-fpic” after -c, such as

gcc -c -fpic A1.c A2.c

Otherwise, an error will be reported

2. Generate library

Ⅰ Static library

ar -rc libafile.a A1.o A2.o

II dynamic library

gcc -shared A1.o A2.o -o libofile.so

3. Using libraries

Ⅰ Static library

gcc -o test test.c libafile.a

test:

II dynamic library

gcc -o test test.c libofile.so


The corresponding .so file cannot be found.
This is due to the corresponding settings of Linux’s own system settings, that is, it only searches for the corresponding .so file under /lib and /usr/lib, so the corresponding so file needs to be copied to the corresponding path:

sudo cp your path/libsofile.so /usr/lib

Execute ./test again and it will run successfully

3. Example

Write the x2x function and x2y function (customized functions), the main function code will call x2x and x2y; write these three functions into three separate .c files, and use gcc to compile them into three .o target files respectively; convert x2x , x2y target file uses the ar tool to generate a .a static library file, and then uses gcc to link the target file of the main function with this static library file to generate the final executable program and record the size of the file.
Use the ar tool to generate a .so dynamic library file for the x2x and x2y target files, and then use gcc to link the target file of the main function with this dynamic library file to generate the final executable program, record the size of the file, and compare it with the previous make comparison

1. Create files

x2x:


x2y:

main:

2. Generate .o file

3. Generate static library

4. Generate dynamic library

Static libraries can be linked into dynamic libraries through gcc -shared

4. Compile into executable file

5. Test

Ⅰ Test the main link of the static library


Use ls command to view file size

ls -l file_name


It can be seen that the main file size of the linked static library is 16.08kb

Ⅱ Compile and link main2 of dynamic library

① First copy liboxx.so to usr/lib/

② Compile main2

③ Test



Size is 16.01kb
It can be seen that the executable file compiled using the dynamic library is smaller than the executable file of the static library.