当前位置:网站首页>Part II - C language improvement_ 12. Packaging and use of dynamic / precision Library
Part II - C language improvement_ 12. Packaging and use of dynamic / precision Library
2022-07-26 23:26:00 【qq_ forty-three million two hundred and five thousand two hundr】
12.1 The basic concept of Libraries
The library is already written 、 ripe 、 Reusable code . Every program needs to rely on many underlying Libraries , It's impossible for everyone to write code from scratch , Therefore, the existence of library is of great significance .
In our developed applications, there are often some common codes that need to be used repeatedly , Just compile the code into a library file .
A library can be simply viewed as a collection of target files , These target files are compressed and packaged to form a file . Like in Windows On such a platform , Most commonly used c The language library consists of the runtime attached to the integrated development environment , These libraries are generally provided by the compiler .
12.2 windows Create and use a static library
12.2.1 The creation of static library
1. Create source files and header files

2. Write code in source and header files
2.1 The header file
#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 Source file
#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. Set the project as a static library
3.1 Open properties

3.2 Set as static library

3.3 Build solution


So far, the static library is created .
12.2.2 The use of static libraries
1. Create a new project , Used to call the static library

2. Configure static library
2.1 Find the header file of the static library (.h) And libraries (.lib)

2.2 Find the header file

2.3 Find the library file



3. Add header files and library files to the new project
3.1 Open the file path of the new project

3.2 Copy the header and library files found above to the directory of the new project

3.3 Add existing items to the new project


3.4 Add success

4. Call the functions in the static library
#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;
}Output results
3012.2.3 Advantages and disadvantages of static library
|
Memory and disk space
Static linking is a simple method , It is also easy to understand in principle , In the early days of underdeveloped operating systems and hardware , Most systems adopt this scheme . With the development of computer software , The shortcomings of this method soon exposed , That is, the way of static link is a serious waste of computer memory and disk space . Especially under multi process operating system , Static links waste a lot of memory space . In the present linux In the system , A common program will use c The language static library is at least 1MB above , So if there is 2000 A program like this , It will waste nearly 2GB Of disk space .
Program development and release
Space waste is a problem with static links , Another problem is the update of the program by static links 、 Deployment and release will also bring a lot of trouble . Such as those used in the program mylib.lib It is provided by a third-party manufacturer , When the manufacturer updates the capacity mylib.lib When , Then our program will get the latest version mylib.lib, Then recompile the link , Release the whole new program to users . The disadvantages of doing this are obvious , That is, once any module in the program is updated , The whole program needs to recompile the link 、 Publish to users , The user needs to reinstall the whole program .
12.3 windows Create and use dynamic library
There are two problems to be solved: waste of space and renewal , The simplest way is to separate the program modules from each other , Form a separate document , Instead of linking them statically . Simply speak , It is not to link the target programs that make up the program , Do not link until the program is running . in other words , Postpone the whole linking process until runtime , This is the basic idea of dynamic link .
12.3.1 The creation of dynamic library
1. Create source files and header files

2. Programming
2.1 Header file section
#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) The function of is to make the declared function can be called externally
__declspec(dllexport)int Sub(int a, int b);2.2 Source file section
#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. Set the project as a dynamic library


4. Build solution


12.3.2 The use of dynamic libraries
1. Create a new project

2. Configure dynamic library files
2.1 Find the dynamic library file generated above

2.2 Copy to the directory of the new project

2.3 Add an existing item



3. The editor calls the dynamic library
#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;
}Running results
10Question one :__declspec(dllexport) What does that mean? ?
There are two functions defined in the dynamic link library : The export function (export function) And internal functions (internal function). The export function can be called by other modules , Internal functions are used internally .
Question 2 : Dynamic library lib Files and static libraries lib The difference between documents ?
When using dynamic libraries , Often two documents are provided : A lead in warehouse (.lib) file ( Also known as “ Import library file ”) And a DLL(.dll) file . There is an essential difference between the introduction of dynamic library and static library , To a DLL The file is , Its import and storage documents (.lib) Include the DLL Symbolic names of exported functions and variables , and .dll The file contains the DLL Actual functions and data . In the case of using dynamic libraries , When compiling linked executables , Just link to the DLL Import library file , The DLL The function code and data in are not copied to the executable , Until the executable runs , To load the required DLL, Will be DLL Map to the address space of the process , And then visit DLL Functions exported in .
边栏推荐
- 第二部分—C语言提高篇_11. 预处理
- New employees of black maredge takeout
- 基本的SELECT语句
- Huawei atlas900 reveals the secret: it integrates thousands of shengteng 910 chips, and its computing power is comparable to 500000 PCs!
- Recruit | PostgreSQL database R & D engineers every week, with an annual salary of 60+, high salary for famous enterprises, and challenge yourself!
- MySQL 数据的导入
- 2. Realize the map of navigation bar and battle page
- PostgreSQL 与 Navicat:数据库行业的中坚力量
- JSON formatting gadget -- pyqt5 instance
- gateway基本使用
猜你喜欢

Regular expressions and bypass case recurrence

The interviewer asked: this point of JS

Introduction to the use of Jerry downloader forced download tool_ Ac695n696nad14ad15 full range support

Ribbon load balancing
![[shader realizes swaying effect _shader effect Chapter 4]](/img/ab/bdbc4a0f297541b532af81a49e2633.png)
[shader realizes swaying effect _shader effect Chapter 4]

Problems and solutions encountered in using nextline(), nextint() and next() in scanner

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

Is test development development development?

ESMFold: AlphaFold2之后蛋白质结构预测的新突破

研究阿尔茨海默病最经典的Nature论文涉嫌造假
随机推荐
30、 Modern storage system (management database and distributed storage system)
DAO:OP 代币和不可转让的 NFT 致力于建立新的数字民主
Ribbon负载均衡
逆袭黑马:数据库全栈工程师(DevDBOps)培训,把最好的课程送给您!
P5469 [NOI2019] 机器人(拉格朗日插值、区间dp)
Kt6368a Bluetooth chip development precautions and problem collection - long term update
公有云安全性和合规性方面的考虑事项
Related functions of strings
1. Configuration environment and project creation
基本的SELECT语句
Learn various details and thoughts of chatroom implementation in Muduo
Basic use of gateway
第二部分—C语言提高篇_8. 文件操作
What is the reason for oom during redis shake synchronization in shake database?
2022年物联网行业有哪些用例?
【MySQL】CentOS 7.9安装、使用MySQL-5.7.39二进制版
HCIA-R&S自用笔记(23)DHCP
Kingbasees SQL language reference manual of Jincang database (3.1.1.3. currency type)
Hcia-r & s self use notes (20) VLAN comprehensive experiment, GVRP
Silicon Valley class lesson 6 - Tencent cloud on demand management module (I)