Summary
1) Link definition: links the target file into an executable program
2) Two ways of linking:
- Static linking: The
object file will be directly *linked* into the executable program.
slib.a
is deleted, the program can still run - Dynamic link: The target file is dynamically loaded after the
program is started.
dlib.so
is deleted, the program cannot run
3) The choice between static link and dynamic link:
- Static linking: suitable for relatively simple programs, which do not need to be changed when developed, such as a purely local notepad;
all object files of 161ba0302b478b into an executable program may result in a large executable program.
- Dynamic link: If each module of a large-scale software is in the form of a dynamic library,
, you only need to modify this module, and then transfer dlib.so to the server. When the user is using the software, just go to the server to update the library directly, without the need to update the entire program.
Introduction to the linking process
Question: Each source file in the project will generate a
object file.o after compilation. How do these .o files generate the final
executable program.out?
A: main role of the linker is to
between the various modules to each other references handle portion, such that the normal of the interface between the individual modules.
1. Static link
- The linker directly adds the contents of the library to the executable program when
- The creation and use of static libraries
1) Generate static libraryobject file: gcc -c slib.c -o slib.o
2) Generatestatic library: ar -q slib.a slib.o (output: ar:creating slib.a; ar refers to archive,
all the object files listed later into slib.a)
3) Use static librarycompile: gcc 20-1.c slib.a -o 20-1.out (Use static library to compile)
2. Dynamic link
- The executable program will only dynamically load the library for linking when it is running
(when it is running, I will find the symbol I need)
library will not enter the executable program
// dlib.c
char* name()
{
return "Dynamic Lib";
}
int add(int a, int b)
{
return a+b;
}
// 20-2.c
#include <stdio.h>
#include <dlfcn.h>
int main()
{
void* pdlib = dlopen("./dlib.so", RTLD_LAZY); // pdlib指向了目录下dlib.so这个动态库
// 这个打开就相当于一个加载到内存里的指针
char* (*pName)(); // 定义函数指针
int (*pAdd)(int, int);
if(NULL != pdlib)
{
pName = dlsym(pdlib, "name"); // 查找pdlib指向的动态库里的函数
pAdd = dlsym(pdlib, "add");
if(NULL != pName && NULL != pAdd)
{
printf("Name: %s\n", pName());
printf("Ret: %d\n", pAdd(2, 3));
}
dlclose(pdlib); // 关闭pdlib指向的动态库
}
else
{
printf("Cannot open dynamic lib...");
}
}
// linux命令
gcc -shared dlib.c -o dlib.so : 创建了动态库dlib.so
gcc 20-2.c -ldl -o a.out : 使用动态库创建了可执行程序a.out
./a.out : 执行可执行程序
Note: -ldl tells the compiler that this program is compiled with a dynamic library, and our program depends on the dynamic library. If the dynamic library
dlib.so is deleted, the program will not run, and the output cannot open, because every time a.out is executed, it needs to go to the dynamic library to find the symbol (what do I want to use, I have to go every time Take it in your warehouse), the library is gone, so naturally I can’t find it.
This article is summarized from "C Language Advanced Course" by Tang Zuolin from "Ditai Software Academy".
If there are any errors or omissions, please correct me.
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。