Detailed explanation of static library and dynamic library under linux

static library

Let's talk about why we need the library first?
When we use a lot of code in the program, such as (scanf, printf, etc.), we need to use these functions frequently in the program, so we compile these codes into library files, and we can link them directly when we need to use them.

definition:

 When the program 编译links the code of the static library into the executable program, the static library is no longer needed when the code is running. (A simple understanding is to pack a bunch of .ofiles together, and let our program link in when needed)

How to create and use:

  Here is an example of addition, subtraction, multiplication and division:

//创建所需文件
[root@localhost ku]# touch add.c add.h sub.c sub.h mul.c mul.h dev.c dev.h main.c 
[root@localhost ku]# ls
add.c  add.h  dev.c  dev.h  main.c  mul.c  mul.h  sub.c  sub.h
[root@localhost ku]# 
//编写所有文件代码
//add.c
#include"add.h"                                                                                                                       

int add(int x,int y)
{
    return x+y;
}
//add.h
 
#ifndef __ADD_H__
#define __ADD_H__

int add(int x,int y);                                                                                                                                       

#endif // __ADD_H__
//sub.c
#include"sub.h"
int sub(int x,int y)                                                                                                                  
{
    return x-y;
}
//sub.h
#ifndef __SUB_H__
#define __SUB_H__

int sub(int x,int y); 

#endif // __SUB_H__  
//mul.c
#include"mul.h"

int mul(int x,int y)                                                                                                                  
{
    return x*y;
}
//mul.h
#ifndef __MUL_H__
#define __MUL_H__

int mul(int x,int y); 

#endif //__MUL_H__   
//dev.c
#include"dev.h"

int dev(int x,int y)                                                                                                                  
{
    return x/y;
}
//dev.h
#ifndef __DEV_H__ 
#define __DEV_H__

int dev(int x,int y);                                                                                                                 

#endif // __DEV_H__
//main.c
#include<stdio.h>
#include"add.h"
#include"sub.h"
#include"mul.h"
#include"dev.h"

int main()
{
    int a,b;
    scanf("%d%d",&a,&b);
    printf("%d + %d = %d\n",a,b,add(a,b));
    printf("%d - %d = %d\n",a,b,sub(a,b));
    printf("%d * %d = %d\n",a,b,mul(a,b));
    printf("%d / %d = %d\n",a,b,dev(a,b));                                                                                            
    return 0;
}

//编译源文件
[root@localhost ku]# ls
add.c  add.h  dev.c  dev.h  main.c  mul.c  mul.h  sub.c  sub.h
[root@localhost ku]# gcc -c *.c //把所有.c文件生成.o文件
[root@localhost ku]# ls
add.c  add.h  add.o  dev.c  dev.h  dev.o  main.c  main.o  mul.c  mul.h  mul.o  sub.c  sub.h  sub.o
[root@localhost ku]# rm main.o -rf  //删除多余的.o文件
[root@localhost ku]# ls
add.c  add.h  add.o  dev.c  dev.h  dev.o  main.c  mul.c  mul.h  mul.o  sub.c  sub.h  sub.o
[root@localhost ku]# 
//生成静态库
[root@localhost ku]# ar -rc libmycal.a *.o
[root@localhost ku]# ls
add.c  add.h  add.o  dev.c  dev.h  dev.o  libmycal.a  main.c  mul.c  mul.h  mul.o  sub.c  sub.h  sub.o
[root@localhost ku]# 
//查看静态库
[root@localhost ku]# ls
add.c  add.h  add.o  dev.c  dev.h  dev.o  libmycal.a  main.c  mul.c  mul.h  mul.o  sub.c  sub.h  sub.o
[root@localhost ku]# ar -tv libmycal.a 
rw-r--r-- 0/0    683 Apr 26 20:46 2018 add.o
rw-r--r-- 0/0    683 Apr 26 20:46 2018 dev.o
rw-r--r-- 0/0    679 Apr 26 20:46 2018 mul.o
rw-r--r-- 0/0    687 Apr 26 20:46 2018 sub.o
[root@localhost ku]# 
//链接静态库生成可执行文件
[root@localhost ku]# ls
add.c  add.h  add.o  dev.c  dev.h  dev.o  libmycal.a  main.c  mul.c  mul.h  mul.o  sub.c  sub.h  sub.o
[root@localhost ku]# gcc main.c -L. -lmycal
[root@localhost ku]# ls
add.c  add.h  add.o  a.out  dev.c  dev.h  dev.o  libmycal.a  main.c  mul.c  mul.h  mul.o  sub.c  sub.h  sub.o
[root@localhost ku]# 
//运行结果
[root@localhost ku]# ls
add.c  add.h  add.o  a.out  dev.c  dev.h  dev.o  libmycal.a  main.c  mul.c  mul.h  mul.o  sub.c  sub.h  sub.o
[root@localhost ku]# ./a.out 
8 3
8 + 3 = 11
8 - 3 = 5
8 * 3 = 24
8 / 3 = 2
[root@localhost ku]# 

以上是整个静态库的生成及运用过程
To sum up in 3 steps:

 First compile the source file into an object file: gcc -c source file
 generates a static library: ar -rc lib (library name).a Object file
 uses a static library: gcc main.c -L (the path to the library) -l (the library name) )

Advantages and disadvantages of static libraries

 advantage:

 1. Save space: linker will only copy the objects you use.
 2. Packaging is simple.

 shortcoming:

 1. If there are global variables in the static library, using them in several modules will cause the global variables to have different values, which is a very serious problem.
 2. When the static library is compiled, the link check will not be performed, so the problems of so many static libraries cannot be checked during the generation of the static library.
 3. Several modules refer to the same static library. If one module is not compiled, it will cause huge differences and cause problems.
 4. A large number of library files will take up space

dynamic library

definition:

 Only when the program 运行links the code of the dynamic library, multiple programs share the code using the library.
 An executable file linked with a dynamic library only contains a table of the entry addresses of the functions it uses, not the machine code of the object file where the external functions are located.

How to create and use:

The case program is the same as above, only the operation steps are written here

[root@localhost ku]# ls
add.c  add.h  dev.c  dev.h  main.c  mul.c  mul.h  sub.c  sub.h
[root@localhost ku]# gcc -c -fpic *.c
// -fpic   表示编译为位置独立的代码,不用此选项的话编译后的代码是位置相关的所以动态载入时是通过代码拷贝的方式来满足不同进程的需要,而不能达到真正代码段共享的目的。
[root@localhost ku]# ls
add.c  add.h  add.o  dev.c  dev.h  dev.o  main.c  main.o  mul.c  mul.h  mul.o  sub.c  sub.h  sub.o
[root@localhost ku]# rm main.o -rf
[root@localhost ku]# ls
add.c  add.h  add.o  dev.c  dev.h  dev.o  main.c  mul.c  mul.h  mul.o  sub.c  sub.h  sub.o
[root@localhost ku]# gcc -shared -o libmycal.so *.o
[root@localhost ku]# ls
add.c  add.h  add.o  dev.c  dev.h  dev.o  libmycal.so  main.c  mul.c  mul.h  mul.o  sub.c  sub.h  sub.o
[root@localhost ku]# gcc main.c -L. -lmycal
//-L.   表示要连接的库在当前目录中
[root@localhost ku]# ls
add.c  add.h  add.o  a.out  dev.c  dev.h  dev.o  libmycal.so  main.c  mul.c  mul.h  mul.o  sub.c  sub.h  sub.o
[root@localhost ku]# ./a.out 
./a.out: error while loading shared libraries: libmycal.so: cannot open shared object file: No such file or directory
[root@localhost ku]# cp libmycal.so /lib/
//把动态库移动到系统库文件下
[root@localhost ku]# ls
add.c  add.h  add.o  a.out  dev.c  dev.h  dev.o  libmycal.so  main.c  mul.c  mul.h  mul.o  sub.c  sub.h  sub.o
//运行结果
[root@localhost ku]# ./a.out 
8 6
8 + 6 = 14
8 - 6 = 2
8 * 6 = 48
8 / 6 = 1
//第二种方法,更改库路径
[root@localhost ku]# ./main 
./main: error while loading shared libraries: libmycal.so: cannot open shared object file: No such file or directory
[root@localhost ku]# vim /etc/ld.so.conf.d/mycal.conf //在创建的文件里写上库的路径
//写好路径之后刷新缓冲区
[root@localhost ku]# ldconfig 
//运行结果
[root@localhost ku]# ./main 
8 6
8 + 6 = 14
8 - 6 = 2
8 * 6 = 48
8 / 6 = 1

Advantages and disadvantages of dynamic libraries:

advantage:

 1. Shared memory
 2. Independent upgrade components (plug-in installation, software update)
 3. Can display dynamic loading

shortcoming:

 1. When multiple applications in the system use a dynamic link library, but the required versions are different, then the dynamic link libraries will interfere with each other.
 2. Performance overhead. In order to achieve "shared code, but not shared data", the dynamic link library introduces a lot of overhead. When calling functions in the dynamic link library, it takes several indirect memory accesses to reach the function entry, and so does global data.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325078083&siteId=291194637