Dynamic and static libraries
The linking method of the library : The default linking method of the gcc / g++ compiler is dynamic linking.
Dynamic linking: Linking the dynamic library to generate an executable program does not directly take the implementation instructions of the functions in the library and write it into the executable program. The executable program records the symbol information table of the functions in the library. When running the executable program, load the dynamic library into the memory. If the dynamic library does not exist, the program cannot run.
Static link: link the static library to generate an executable program, directly write the implementation code instructions of the functions we use in the library into the executable program file, and there is no dependency when the program runs.
The advantages and disadvantages of dynamic linking: the generated executable file is relatively small, the runtime library is loaded into the memory, and multiple programs can use the same in-memory library function code; the disadvantage is that the runtime needs to rely on the existence of the dynamic library, which does not exist It cannot run, and the loading speed is slow.
The advantages and disadvantages of static linking: the program runs without other dependencies and can be run directly; the loading speed is fast, but the executable program is relatively large, and if multiple programs use the function in the same library, the code of this function will have memory at runtime There will be multiple copies of the same library function code, which will cause redundancy.
When we write code, there will be many reused functions and codes. When packaged into a library, the functions and codes can be used anywhere, which greatly improves our programming efficiency. Now let's take a look at how to package a library function
Packaging and use of dynamic/static libraries
Sample code:
child.c
//child.c
#include <stdio.h>
int printChild()
{
printf("hello WhiteShirtI");
return 0;
}
child.h
//child.h
int printChild();
main.c
//main.c
#include <stdio.h>
#include "child.h"
int main()
{
printChild();
return 0;
}
Dynamic library
1. Compile and assemble each .c file into an object file
gcc -c -fPIC child.c -o child.o
2. Package it into a dynamic library ending with .so
gcc -share child.o -o libmychild.so
; the naming method of lib***.so
dynamic library There are three ways
to link and use when generating executable files : gcc main.c -o main -lmychild
-l specifies the library Name
But we found that we could not find the library file. We need to configure our library before linking. There are 3 methods for dynamic library
1. Put the library file in the specified path: /usr/lib64
or /usr/lib
put it in the system library will pollute me The original library of the door
2. Set the search path environment variable of the link library-add the path where the library file is located: export LIBRARY_PATH=$LIBRARY_PATH:.
3. Use the gcc -L option to specify the search path of the link library. View
gcc main.c -o main -L./ -lmychild
ldd main
the dynamic library that the executable program depends on main
Although we can link successfully, we cannot successfully execute the main program
Load and use method when running the executable program
1. Put the library file in the specified path: /usr/lib64
or /usr/lib64
2. Set the link library's connection path environment variable-add the path where the library file is located: export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:.
If the link is a dynamic library, it will be run at runtime Need to load the dynamic library; if the link is a static library, it does not need to be loaded at runtime
Static library
1. Compile and assemble each .c file into an object file
gcc -c child.c -o child1.o
2. Package it into a static library ending with .a
ar -cr libmyachild.a child1.o
; the naming method of the static library lib***.a
How to link the program to the static library:
1. Put the static library in the specified path
gcc The dynamic library is used by default, so how do we let him use the static library?
Put our static library in a file and let it go to the directory when linking. Since the directory only has static libraries, you can only use static libraries at this time. gcc main.c -o main -L./lib -mychild
2. Use static linking for all libraries: gcc -static main.c -o main -L./lib -lmychild
in the new version When installing glibc-devel, glibc and gcc-c++ under the Linux system, libc.a will not be installed. Only libc.so is installed. So when -static is used, libc.a cannot be used. Can only report that libc is not found.
Solution:
install glibc-static
sudo yum install glibc-static