当前位置:网站首页>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 .
边栏推荐
- How to recover the original data when the U disk is damaged, and how to recover the damaged data when the U disk is damaged
- Security team: Recently, there is an rce vulnerability in the COREMAIL email client of windows, which may lead to the disclosure of the private key of the wallet
- 实战项目:Boost搜索引擎
- Import of MySQL data
- 第二部分—C语言提高篇_12. 动/精态库的封装和使用
- 第二部分—C语言提高篇_9. 链表
- Kalibr calibration realsensed435i -- multi camera calibration
- 程序员成长第二十九篇:如何激励员工?
- Six challenges facing enterprise data governance!
- 企业如何缓解物联网和工业物联网安全风险
猜你喜欢

Sign up now | frontier technology exploration: how to make spark stronger and more flexible

黑马瑞吉外卖之新增员工

Pyqt5 how to set pushbutton click event to obtain file address
![[shaders realize distorted outline effect _shader effect Chapter 2]](/img/b3/ab28039cce2521ff1d59f0de6bf852.png)
[shaders realize distorted outline effect _shader effect Chapter 2]

HCIA-R&S自用笔记(19)VLAN配置及实验、VLAN间路由

The interviewer asked: this point of JS

New employees of black maredge takeout

Public cloud security and compliance considerations

Disk expansion process and problems encountered in the virtual machine created by VMWare

数据库全栈工程师(DevDBOps)低首付、高回报,先就业后付款
随机推荐
Vit:vision transformer super detailed with code
Reinforcement learning weekly 55: lb-sgd, msp-drl & robust reinforcement learning against
[MySQL] CentOS 7.9 installation and use mysql-5.7.39 binary version
第二部分—C语言提高篇_11. 预处理
New employees of black maredge takeout
Kalibr calibration realsensed435i -- multi camera calibration
Esmfold: a new breakthrough in protein structure prediction after alphafold2
Professor Ashe, a Chinese scientist, made a positive response to the suspected fake Nature paper
华裔科学家Ashe教授对涉嫌造假的Nature论文的正面回应
Apifox -- a better API testing tool than postman
Six challenges facing enterprise data governance!
Kingbasees SQL language reference manual of Jincang database (3.1.1.14. scope type)
电脑开机后内存占用过高(50%以上)
Reduce power consumption and upgrade functions! Qiyingtailun released the second generation voice AI chip: the module price is as low as 14.99 yuan!
The memory occupation of the computer is too high after it is turned on (more than 50%)
杭州银行面试题【杭州多测师】【杭州多测师_王sir】
Hcia-r & s self use notes (23) DHCP
Eureka basic use
2022年物联网行业有哪些用例?
[H5 bottom scrolling paging loading]