[Linux system programming] Detailed explanation of Linux static library

00. Catalog

01. Overview

The so-called "program library", simply put, is a file that contains data and execution code. It cannot be executed independently, and can be used as a part of other execution programs to complete certain functions.

The existence of the library can make the program modular, can speed up the recompilation of the program, can realize code reuse, and can make the program easy to upgrade.

Program library can be divided into static library (static library) and shared library (shared library) .

The essence and working mode of the library files in Linux and Windows are the same, but the file formats and file suffixes corresponding to the libraries are different on different platforms. There are two kinds of libraries called in the program , 静态库and 动态库no matter which library file is essentially a source file, it is only a binary format that can only be recognized by a computer.

There are generally two purposes for using a library in a project. One is to make the program more concise without maintaining too many source files in the project. The other is to keep the source code confidential. After all, not everyone wants to write the program open source.

When we get the library file (dynamic library, static library), if we want to use it, we must have the declaration of the API function provided in these libraries, that is, the header file. Add these to the project, and then we can function.

02. Static library file format

A static library can be considered as a collection of object codes, which are added to the executable code before the executable program runs and become part of the executable program.

According to the custom, ".a" is generally used as the file extension. The naming of static libraries is generally divided into three parts:

  • Prefix: lib
  • Library name: you can define it yourself
  • Suffix: .a

So the name of the final static library should be: libxxx.a

【Kind tips】

  • In Linux, the static library is libprefixed with , .asuffixed with , and the name of the library is specified in the middle, that is:libxxx.a
  • In Windows, the static library is generally libprefixed with and libsuffixed with, and the name of the library needs to be specified in the middle, namely:libxxx.lib

03. Static library file generation process

To generate a static library, you need to assemble the source file ( 使用参数 -c) to get the target file in binary format ( .o 格式), and then package the target file with ara tool to get the static library file ( libxxx.a).

When using arthe tool to create a static library, three parameters are required:

  • 参数c: Create a library, regardless of whether the library exists or not, will be created.
  • 参数s: Creates an index of object files, which speeds up time when creating larger libraries.
  • 参数r: Insert a module (replace) in the library. By default new members are added at the end of the library, and if the module name already exists in the library, the module with the same name is replaced.
    insert image description here

04. Static library production

4.1 The source code structure is as follows


deng@local:~/tmp$ tree lib
lib
├── add.c
├── add.h
├── mdiv.c
├── mdiv.h
├── mul.c
├── mul.h
├── sub.c
└── sub.h

0 directories, 8 files
deng@local:~/tmp$

4.2 Generate all .c files into corresponding .o files

deng@local:~/tmp/lib$ gcc -c add.c
deng@local:~/tmp/lib$ gcc -c sub.c
deng@local:~/tmp/lib$ gcc -c mul.c
deng@local:~/tmp/lib$ gcc -c mdiv.c
deng@local:~/tmp/lib$ ls
add.c  add.o   mdiv.h  mul.c  mul.o  sub.h
add.h  mdiv.c  mdiv.o  mul.h  sub.c  sub.o
deng@local:~/tmp/lib$


# 或者以下方式
deng@local:~/tmp/lib$ gcc -c add.c sub.c mul.c mdiv.c
deng@local:~/tmp/lib$ ls
add.c  add.o      mdiv.c  mdiv.o  mul.h  sub.c  sub.o
add.h  libtest.a  mdiv.h  mul.c   mul.o  sub.h
deng@local:~/tmp/lib$

4.3 Generate static library

deng@local:~/tmp/lib$ ar -rcs libtest.a add.o sub.o mul.o mdiv.o
deng@local:~/tmp/lib$ ls
add.c  add.o      mdiv.c  mdiv.o  mul.h  sub.c  sub.o
add.h  libtest.a  mdiv.h  mul.c   mul.o  sub.h
deng@local:~/tmp/lib$


4.4 Static library publishing

After the static library is created, the .a file and the header file need to be released to users together.

05. Static library testing

5.1 The project structure is as follows


deng@local:~/tmp$ tree test/
test/
├── add.h
├── libtest.a
├── mdiv.h
├── mul.h
├── sub.h
└── test.c

0 directories, 6 files
deng@local:~/tmp$


5.2 Compile test.c to generate executable files

deng@local:~/tmp/test$ gcc test.c -o test -I./ -L./ -ltest


Parameter Description:

  • -L: Indicates the directory where the library to be connected is located
  • -I./: I (uppercase i) indicates that the directory of the specified header file is the current directory
  • -l (lowercase L): Specify the library required for linking, remove the prefix and suffix

5.3 Perform verification

deng@local:~/tmp/test$ ./test
x + y = 18
x - y = 12
x * y = 45
x / y = 5
deng@local:~/tmp/test$

06. Advantages and disadvantages of static libraries

Advantages :

  • Static libraries are packaged into the application for fast loading
  • The release program does not need to provide a static library, which is convenient for porting

Disadvantages :

  • The same library file data may be loaded in multiple copies in memory, consuming system resources and wasting memory
  • Updating the library file requires recompiling the project file to generate a new executable program, wasting time.

insert image description here

07. Technical exchange

Interested enthusiasts in IoT, embedded, C++, C direction, etc. can add WX to communicate together. Click the private message on the homepage to add blogger WeChat.

08. Appendix

Guess you like

Origin blog.csdn.net/dengjin20104042056/article/details/131925846