foreword
content
1. Why is there a library 2. Static
library 2.1 Generate a .c
file into a .o file with the same name
-
Static library (.a): The program copies the binary code of the library to the executable file of my program when the program is compiled and linked, and the static library is no longer needed when the program runs
-
Dynamic library (.so): The code of the dynamic library is only linked when the program is running, and the code of the library is shared by multiple programs
Such as the c language library:
- Dynamic library libc.so
- static library libc.a
The naming rule of the library is: lib header + library name + suffix remove the prefix lib, remove the . suffix, and the rest is the library name
There are two ways to generate executable programs: dynamic linking and static linking
ldd filename # 查看链接方式
1. Why is there a library
When developing, we often use code written by others, including compiling other people's source code, calling third-party libraries given by others, calling network function interfaces given by others...
Why should we use someone else's code?
- Improve development efficiency
- Increase program robustness
The so-called robustness refers to the robustness of the program, and the coupling between each module is low. For third-party libraries, we think there is no problem. If there is an error in my program, then there is no need to suspect that the library itself is faulty, but there must be a problem with the code logic written by myself, which reduces the scope of the error to be checked. Of course, the library here must refer to some authoritative libraries, such as standard libraries such as STL.
Well, how to use other people's code, it is very important to learn how to package and use the library
2. Static library
For this experiment, we have 5 original files:
add.h
add.c
sub.h
sub.c
main.c
#pragma once
#include <stdio.h>
int add(int x, int y);
#include "add.h"
int add(int x, int y) {
return x+y;
}
#pragma once
#include <stdio.h>
int sub(int x, int y);
#include "sub.h"
int sub(int x, int y) {
return x-y;
}
#include <stdio.h>
#include "add.h"
#include "sub.h"
int main() {
printf("%d\n%d\n", add(20, 10), sub(20, 10));
return 0;
}
2.1 Generate the .c file into a .o file with the same name
Calling the static library refers to linking the binary code of the static library into the executable file when the program is linked, so that there is no need to find the static library when running.
Well, since the static library is linked to the executable file in the linking step, then according to
Preprocessing - Compile - Assemble - Link
We need to complete the assembly step. Assembly is to convert assembly code into machine code, and the generated file is a .o
file
gcc -c add.c
gcc -c sub.c
2.2 Make a static library
The essence of making a static library is to package several .o files
ar -ac libmymath.a add.o sub.o
Use ar -tv to view the files included in the static library:
2.3 Using static libraries
We put the three files add.o sub.o libmymath.a in the slib folder
1. Use -I
we have to bring the header file directory when compiling and linking main.c
gcc main.c -o main -I ./slib/
seems to be missing something
2. Use -L
to bring the directory where the library file is located
gcc main.c -o main -I ./slib/ -L ./slib/
seems to be missing something
3. Use -l
the name of the library (the part between lib and . suffix)
gcc main.c -o main -I ./slib/ -L ./slib/ -lmymath
At this point, the executable is successfully generated:
run:
3. Dynamic library
-
An executable file linked with a dynamic library contains only a table of the function entry addresses it uses, not the entire machine code of the object file where the external function resides
-
Before the executable file starts running, the machine code of the external function is copied by the operating system from the dynamic library on disk into memory, a process called dynamic linking.
-
Dynamic libraries can be shared among multiple programs, so dynamic linking makes executable files 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.
3.1 Making a dynamic library
1. Generate position independent code
This dynamic library itself is not loaded into any area of memory mapped to the shared area, and any location will affect the relative change of the code address in the library, resulting in the non-executable library code of the library being mapped to the shared area, and each process uses it. The same code, no memory waste
gcc -fPIC -c add.c sub.c
2. Generate dynamic library files
gcc -shared -o libmymath.so add.o sub.o
3.2 Generating executable files using dynamic libraries
gcc main.c -o main -I dlib/ -L dlib/ -lmymath
3.3 Running
Execution main
found an error, what happened?
Did n't we already 编译
specify the header file directory, library file directory, library file name when we were there?
Oh, it was specified when compiling, but now it is in the running phase. In the running phase, there is no specified where to find the library file. Of course, I can't find it.
3 workarounds:
- Copy our
libmymath.so
copy to the system shared library path, usually/usr/lib64
or/usr/lib
- Add the dlib directory to the LD_LIBRARY_PATH environment variable
Here is an environment variable: LD_LIBRARY_PATH
, which stores the directory where the system defaults to find dynamic library files
Export environment variables:
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/root/class101/linux/lesson18/dlib
- ldconfig configuration
/etc/ld.so.conf.d
Create a .conf file arbitrarily in the /etc/ld.so.conf.d directory, and write the dynamic library directory in the .conf file
echo "/root/class101/linux/lesson18/dlib" > /etc/ld.so.conf.d/test.conf
ldconfig
4. Some details
detail one
Why does C language never explicitly use options such as -I -L -l when compiling?
1. Header files and library files are found by gcc in the default path 2. When gcc
compiles C code, it should look for libc by default
What if we don't want to use those options either?
Copy the header file library file to the default path -> library installation
detail two
gcc default dynamic link, if there is no dynamic link, go to static link
Makefile
main: main.c
gcc -o $@ $^ -I ./dlib -L ./dlib -lmymath
.PHONY: clean
clean:
rm -f main