当前位置:网站首页>. DLL and Differences between lib files
. DLL and Differences between lib files
2022-07-03 10:08:00 【Moresweet cat】
Reproduced in https://www.cnblogs.com/zkwarrior/p/10948174.html
There are two kinds of Libraries :
One is LIB Contains the... Where the function is located DLL File and function location information in the file ( entrance ), The code is loaded in the process space by the runtime DLL Provide , Called dynamic link library dynamic link library.
One is LIB Contains the function code itself , Add code directly to the program at compile time , Called static link library static link library.
There are two ways to link :
Dynamic link uses dynamic link library , Allow executable modules (.dll File or .exe file ) Only include positioning at run time DLL Information needed for the executable code of a function .
Static links use static link libraries , Linker from static link library LIB Get all referenced functions , And put the library in the executable file with the code .
About lib and dll The difference is as follows :
(1)lib It's used when compiling ,dll It's used at runtime . If you want to compile the source code , It only needs lib; If you want to make the dynamic link program run , It only needs dll.
(2) If there is dll file , that lib Usually some index information , Recorded dll Entry and location of functions in ,dll Is the specific content of the function ; If only lib file , So this lib The file is compiled statically , Indexing and implementation are all in it . Using statically compiled lib file , There is no need to hang the dynamic library when running the program , The disadvantage is that the application is relatively large , And lose the flexibility of dynamic libraries , When you release a new version, you need to release a new application .
(3) In the case of dynamic links , There are two files : One is LIB file , One is DLL file .LIB Include by DLL Name and location of exported function ,DLL Contains actual functions and data , Application usage LIB File link to DLL file . In the application's executable , It does not store the function code to be called , It is DLL The address of the corresponding function code in , This saves memory resources .DLL and LIB The file must be released with the application , Otherwise, the application will generate errors . If you don't want to use lib File or not lib file , It can be used WIN32 API function LoadLibrary、GetProcAddress load .
Use lib Two files need to be noted :
(1).h The header file , contain lib Specifies the class or symbol prototype or data structure of the output . Application calls lib when , You need to include this file in the application's source file .
(2).LIB file , A little .
Use dll Three documents need to be noted :
(1).h The header file , contain dll Specifies the output class or symbol prototype or data structure .h file . Application calls dll when , You need to include this file in the application's source file .
(2).LIB file , yes dll Compiling 、 The file generated after the link is successful , The function is when other applications call dll when , This file needs to be introduced into the application , Otherwise, an error will occur . If you don't want to use lib File or not lib file , It can be used WIN32 API function LoadLibrary、GetProcAddress load .
(3).dll file , Real executable , After successful development, the application is released , Just need to have .exe Document and .dll file , It doesn't need to .lib Document and .h The header file .
Use lib Methods :
static state lib in , One lib The file is actually any obj A collection of documents ,obj File is cpp Generated by file compilation . When compiling this static library project , You won't encounter link errors at all ; Even if there is a mistake , Will only use this lib Of EXT Documents or DLL Exposed in the project .
stay VC Create a new one in static library Type of Engineering Lib, Join in test.cpp Document and test.h file ( The header file contains the function declaration ), Then compile , It was generated Lib.lib file .
Other projects need to use this lib There are two ways :
(1) stay project->link->Object/Library Module Add Lib.lib file ( First query the project directory , Re query system Lib Catalog ); Or add instructions to the source code #pragma comment(lib, “Lib.lib”).
(2) take Lib.lib Copy into the directory where the project is located , Or the directory generated by the execution file , Or the system Lib Directory .
(3) Add the corresponding header file test.h.
Use DLL Methods :
Use... In dynamic links lib, No obj A collection of documents , That is, there will be no actual implementation , It just provides dynamic links to DLL Information needed , such lib You can compile a DLL When the project is completed, it is generated by the compiler .
establish DLL The method of Engineering ( A little ).
(1) Implicit linking
The first way is : adopt project->link->Object/Library Module Add .lib file ( Or add instructions to the source code #pragma comment(lib, “Lib.lib”)), And will .dll Put the file into the directory where the project is located , And then add the corresponding .h The header file .
#include "stdafx.h"
#include "DLLSample.h"
#pragma comment(lib, "DLLSample.lib") // You can also set the link of the library in the project properties
int main()
{
TestDLL(123); //dll The function in , stay DllSample.h In a statement
return(1);
}
(2) Explicit links
Function pointer and WIN32 API function LoadLibrary、GetProcAddress load , Using this loading method , Unwanted .lib Document and .h The header file , It only needs .dll File can ( take .dll Put the file into the project directory ).
#include <iostream>
#include <windows.h> // Use functions and some special variables
typedef void (*DLLFunc)(int);
int main()
{
DLLFunc dllFunc;
HINSTANCE hInstLibrary = LoadLibrary("DLLSample.dll");
if (hInstLibrary == NULL)
{
FreeLibrary(hInstLibrary);
}
dllFunc = (DLLFunc)GetProcAddress(hInstLibrary, "TestDLL");
if (dllFunc == NULL)
{
FreeLibrary(hInstLibrary);
}
dllFunc(123);
std::cin.get();
FreeLibrary(hInstLibrary);
return(1);
}
LoadLibrary The function takes a name as an argument , get DLL Example (HINSTANCE The type is the handle to the instance ), Usually, after calling this function, you need to check whether the function returns successfully , If not, return NULL( Invalid handle ), The function is called FreeLibrary Release DLL Acquired memory .
GetProcAddress Functions use DLL The handle of the function and the name of the function are used as parameters , Return the corresponding function pointer , At the same time, strong rotation must be used ; Judge whether the function pointer is NULL, If so, call the function FreeLibrary Release DLL Acquired memory . thereafter , You can use the function pointer to call the actual function .
Finally, remember to use FreeLibrary Function to free memory .
Be careful : How the application finds DLL file ?
Use LoadLibrary Explicit links , Then... Can be specified in the parameters of the function DLL The full path to the file ; If the path is not specified , Or make implicit links ,Windows The following search order will be followed to locate DLL:
(1) contain EXE File directory
(2) Project directory
(3)Windows System catalog
(4)Windows Catalog
(5) Listed in Path A series of directories in the environment variable
.h Header files are required at compile time ,lib It's needed when linking ,dll It's what the runtime needs .
Attached to the dependency is .lib No .dll, If it's generated DLL, Then it must also generate LIB file . If you want to compile and link the source code , Header files and lib That's enough . If you also make the dynamically connected program run , Yes dll That's enough . In the development and debugging phases , Of course, it's better to have both .
.h .lib .dll The relationship between the three is :
H The purpose of the document is : Declare function interfaces
DLL The purpose of the document is : Function executable code
When we refer to a H Functions in files , How does the chainer know which to call DLL What about the documents ? This is it. LIB Role of documents : Tell the linker Where is the function called DLL in , Function execution code in DLL Where in , That's why additional dependencies are needed .LIB file , It acts as a bridge . If you generate a static library file , There is no DLL , Only lib, At this point, the executable part of the function is also in lib In file
At present lib There are two kinds of suffix libraries , One is static link library (Static Libary, hereinafter referred to as “ Static library ”), The other is dynamic link library (DLL, hereinafter referred to as “ Dynamic library ”) Import library of (Import Libary, hereinafter referred to as “ Import library ”). A static library is one or more obj Packing of documents , So some people just take it from obj File generation lib The process is called Archive, It's a combination of . For example, you link a static library , If there's a mistake , It will find out exactly which obj There is a mistake , Static state lib It's just a shell . Dynamic libraries usually have corresponding import libraries , Convenient program static loading dynamic link library , Otherwise you may need to be yourself LoadLibary Transfer in DLL file , And then by hand GetProcAddress We got the corresponding function . With the import library , You just need to link the import library and call the function according to the declaration of the function interface in the header file . There is a big difference between an import library and a static library , They are essentially different things . The static library itself contains the actual execution code 、 Symbol tables and so on , And for the import library , The actual execution code is in the dynamic library , The import library only contains the address symbol table and so on , Make sure the program finds some basic address information for the corresponding function .
General dynamic library programs are lib Document and dll file .lib Files must be connected to the application at compile time , and dll Files are called only at run time . If there is dll file , So the corresponding lib Files are usually index information , The specific implementation is dll In file . If only lib file , So this lib The file is compiled statically , Indexing and implementation are all in it . Statically compiled lib Documents are good : You don't need to hang up the dynamic library when you install it for users . But there are also shortcomings , That's why the app is bigger , And lose the flexibility of dynamic libraries , During version upgrade , At the same time, you need to release new applications . In the case of dynamic libraries , There are two files , And one is the water diversion (.LIB) file , One is DLL file , The import file contains the files that are DLL The name and location of the exported function ,DLL Contains actual functions and data , Application usage LIB The file links to the DLL file , Functions and data in the library are not copied to the executable , So in the application's executable , It does not store the function code to be called , It is DLL The memory address of the function to be called in , In this way, when one or more applications are running, the program code is linked with the function code to be called , This saves memory resources . As can be seen from the above description ,DLL and .LIB The file must be released with the application , Otherwise, the application will generate errors .
Both static library and shared library are one obj A collection of documents , But after static linking , There are their own needs in the execution program obj A copy of , After dynamic linking , The executor simply contains a reference to the shared library . A shared library is equivalent to a shared library consisting of multiple obj It's a combination of files obj file , After linking, all its code is loaded , Whether you need it or not .
It seems that a conclusion can be drawn :
The storage space of statically linked programs is larger than that of dynamically linked programs , Because the code copy in the library is included in the execution program ;
And the dynamically linked program uses more running space than the statically linked program , Because it loads unnecessary code into the runtime space .
边栏推荐
- Gpiof6, 7, 8 configuration
- LeetCode - 5 最长回文子串
- Leetcode - 1670 design front, middle and rear queues (Design - two double ended queues)
- Tensorflow2.0 save model
- 2021-01-03
- Do you understand automatic packing and unpacking? What is the principle?
- Stm32f407 key interrupt
- 2021-10-27
- Simple use of MySQL (addition, deletion, modification and query)
- 01仿B站项目业务架构
猜你喜欢
Opencv gray histogram, histogram specification
Leetcode interview question 17.20 Continuous median (large top pile + small top pile)
QT self drawing button with bubbles
It is difficult to quantify the extent to which a single-chip computer can find a job
I think all friends should know that the basic law of learning is: from easy to difficult
Swing transformer details-2
Leetcode - 895 maximum frequency stack (Design - hash table + priority queue hash table + stack)*
Gpiof6, 7, 8 configuration
LeetCode - 1670 设计前中后队列(设计 - 两个双端队列)
Uniapp realizes global sharing of wechat applet and custom sharing button style
随机推荐
YOLO_ V1 summary
Opencv gray histogram, histogram specification
When the reference is assigned to auto
2020-08-23
Basic knowledge of MySQL database (an introduction to systematization)
Leetcode bit operation
LeetCode - 933 最近的请求次数
After clicking the Save button, you can only click it once
Installation and removal of MySQL under Windows
Serial communication based on 51 single chip microcomputer
Swing transformer details-1
LeetCode - 703 数据流中的第 K 大元素(设计 - 优先队列)
Stm32f04 clock configuration
4G module IMEI of charging pile design
CV learning notes - camera model (Euclidean transformation and affine transformation)
ADS simulation design of class AB RF power amplifier
CV learning notes - feature extraction
Stm32 NVIC interrupt priority management
Vscode markdown export PDF error
LeetCode - 1172 餐盘栈 (设计 - List + 小顶堆 + 栈))