当前位置:网站首页>第二部分—C语言提高篇_12. 动/精态库的封装和使用
第二部分—C语言提高篇_12. 动/精态库的封装和使用
2022-07-26 22:20:00 【qq_43205256】
12.1 库的基本概念
库是已经写好的、成熟的、可复用的代码。每个程序都需要依赖很多底层库,不可能每个人的代码从零开始编写代码,因此库的存在具有非常重要的意义。
在我们的开发的应用中经常有一些公共代码是需要反复使用的,就把这些代码编译为库文件。
库可以简单看成一组目标文件的集合,将这些目标文件经过压缩打包之后形成的一个文件。像在Windows这样的平台上,最常用的c语言库是由集成按开发环境所附带的运行库,这些库一般由编译厂商提供。
12.2 windows下静态库创建和使用
12.2.1 静态库的创建
1.创建源文件和头文件

2.编写源文件和头文件中的代码
2.1头文件
#pragma once
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <errno.h>
#include <ctype.h>
#include <assert.h>
#include <stdbool.h>
int Add(int a, int b);2.2源文件
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <errno.h>
#include <ctype.h>
#include <assert.h>
#include <stdbool.h>
#include "add_func.h"
int Add(int a, int b)
{
return a + b;
}3.将项目设置为静态库
3.1打开属性

3.2设置为静态库

3.3 生成解决方案


至此静态库创建完毕。
12.2.2 静态库的使用
1.创建一个新的项目,用来调用静态库

2. 配置静态库
2.1找到静态库的头文件(.h)和库文件(.lib)

2.2找到头文件

2.3找到库文件



3.在新项目中添加头文件和库文件
3.1打开新项目的文件路径

3.2将上文找到的头文件和库文件复制到新项目的目录中

3.3 在新项目中添加现有项


3.4添加成功

4.调用静态库里面的函数
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <errno.h>
#include <ctype.h>
#include <assert.h>
#include <stdbool.h>
#include "add_func.h"
int main()
{
printf("%d\n", Add(10, 20));
return 0;
}输出结果
3012.2.3 静态库优缺点
|
内存和磁盘空间
静态链接这种方法很简单,原理上也很容易理解,在操作系统和硬件不发达的早期,绝大部门系统采用这种方案。随着计算机软件的发展,这种方法的缺点很快暴露出来,那就是静态链接的方式对于计算机内存和磁盘空间浪费非常严重。特别是多进程操作系统下,静态链接极大的浪费了内存空间。在现在的linux系统中,一个普通程序会用到c语言静态库至少在1MB以上,那么如果磁盘中有2000个这样的程序,就要浪费将近2GB的磁盘空间。
程序开发和发布
空间浪费是静态链接的一个问题,另一个问题是静态链接对程序的更新、部署和发布也会带来很多麻烦。比如程序中所使用的mylib.lib是由一个第三方厂商提供的,当该厂商更新容量mylib.lib的时候,那么我们的程序就要拿到最新版的mylib.lib,然后将其重新编译链接后,将新的程序整个发布给用户。这样的做缺点很明显,即一旦程序中有任何模块更新,整个程序就要重新编译链接、发布给用户,用户要重新安装整个程序。
12.3 windows下动态库创建和使用
要解决空间浪费和更新困难这两个问题,最简单的办法就是把程序的模块相互分割开来,形成独立的文件,而不是将他们静态的链接在一起。简单地讲,就是不对哪些组成程序的目标程序进行链接,等程序运行的时候才进行链接。也就是说,把整个链接过程推迟到了运行时再进行,这就是动态链接的基本思想。
12.3.1 动态库的创建
1.创建源文件和头文件

2.编写程序
2.1头文件部分
#pragma
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <errno.h>
#include <ctype.h>
#include <assert.h>
#include <stdbool.h>
//__declspec(dllexport) 的作用是使得声明的函数可以被外部调用
__declspec(dllexport)int Sub(int a, int b);2.2源文件部分
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <errno.h>
#include <ctype.h>
#include <assert.h>
#include <stdbool.h>
#include "my_sub.h"
int Sub(int a, int b)
{
return a - b;
}3.将项目设置为动态库


4.生成解决方案


12.3.2 动态库的使用
1.创建一个新的项目

2.配置动态库文件
2.1找到上文中生成的的动态库文件

2.2复制到新项目的目录中

2.3添加现有项



3.编辑程序调用动态库
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <errno.h>
#include <ctype.h>
#include <assert.h>
#include <stdbool.h>
#include"my_sub.h"
int main()
{
printf("%d\n", Sub(20, 10));
return 0;
}运行结果
10疑问一:__declspec(dllexport)是什么意思?
动态链接库中定义有两种函数:导出函数(export function)和内部函数(internal function)。 导出函数可以被其它模块调用,内部函数内部使用。
疑问二:动态库的lib文件和静态库的lib文件的区别?
在使用动态库的时候,往往提供两个文件:一个引入库(.lib)文件(也称“导入库文件”)和一个DLL(.dll)文件。动态库的引入库文件和静态库文件有着本质的区别,对一个DLL文件来说,其引入库文件(.lib)包含该DLL导出的函数和变量的符号名,而.dll文件包含该DLL实际的函数和数据。在使用动态库的情况下,在编译链接可执行文件时,只需要链接该DLL的引入库文件,该DLL中的函数代码和数据并不复制到可执行文件,直到可执行程序运行时,才去加载所需的DLL,将该DLL映射到进程的地址空间中,然后访问DLL中导出的函数。
边栏推荐
- Product principles of non-financial decentralized application
- Basic use of gateway
- Practical project: boost search engine
- Several inventory terms often used in communication
- 8-其他编程语言--记录
- 2019 biometric forum successfully ended: these ten highlights should not be missed!
- 企业数据治理面临的六大挑战!
- ESMFold: AlphaFold2之后蛋白质结构预测的新突破
- 第二部分—C语言提高篇_9. 链表
- Differences between PHP round and sprintf functions
猜你喜欢

Arduino experiment I: two color lamp experiment

How to use data pipeline to realize test modernization

Esmfold: a new breakthrough in protein structure prediction after alphafold2

Ribbon负载均衡

SQL Basics

Is test development development development?

逆袭黑马:数据库全栈工程师(DevDBOps)培训,把最好的课程送给您!

Hcia-r & s self use notes (18) campus network architecture foundation, switch working principle, VLAN principle

【MySQL】CentOS 7.9安装、使用MySQL-5.7.39二进制版

Making wechat robot with go (I) sending messages
随机推荐
SQL 基础知识
Ribbon load balancing
Problems and solutions encountered in using nextline(), nextint() and next() in scanner
Huawei atlas900 reveals the secret: it integrates thousands of shengteng 910 chips, and its computing power is comparable to 500000 PCs!
Several inventory terms often used in communication
Regular expressions and bypass case recurrence
gateway基本使用
Weilai cup 2022 Niuke summer multi school training camp 2
Why am I still writing articles on CSDN? A journey of accompanying learning.
Three effective strategies for the transformation of data supply chain to be coordinated and successful
Reduce power consumption and upgrade functions! Qiyingtailun released the second generation voice AI chip: the module price is as low as 14.99 yuan!
[Luogu] p2709 little B's inquiry
8 other programming languages -- Recording
Restful interface specification
Arduino experiment I: two color lamp experiment
DAO:OP 代币和不可转让的 NFT 致力于建立新的数字民主
The most classic Nature paper on Alzheimer's disease is suspected of fraud
Disk expansion process and problems encountered in the virtual machine created by VMWare
json格式化小工具--pyqt5实例
Interview: your most impressive bug, for example