当前位置:网站首页>. 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 .
边栏推荐
- There is no shortcut to learning and development, and there is almost no situation that you can learn faster by leading the way
- el-table X轴方向(横向)滚动条默认滑到右边
- Dictionary tree prefix tree trie
- CV learning notes - Stereo Vision (point cloud model, spin image, 3D reconstruction)
- CV learning notes - feature extraction
- 03 fastjason solves circular references
- Pymssql controls SQL for Chinese queries
- For new students, if you have no contact with single-chip microcomputer, it is recommended to get started with 51 single-chip microcomputer
- Basic use and actual combat sharing of crash tool
- Interruption system of 51 single chip microcomputer
猜你喜欢

Leetcode 300 longest ascending subsequence

LeetCode - 1670 设计前中后队列(设计 - 两个双端队列)

El table X-axis direction (horizontal) scroll bar slides to the right by default

Opencv image rotation

My notes on the development of intelligent charging pile (III): overview of the overall design of the system software

Opencv note 21 frequency domain filtering

MySQL root user needs sudo login

Swing transformer details-1

Leetcode - 1670 design front, middle and rear queues (Design - two double ended queues)

RESNET code details
随机推荐
(1) What is a lambda expression
Application of 51 single chip microcomputer timer
Opencv gray histogram, histogram specification
Leetcode interview question 17.20 Continuous median (large top pile + small top pile)
4G module designed by charging pile obtains signal strength and quality
JS foundation - prototype prototype chain and macro task / micro task / event mechanism
Stm32 NVIC interrupt priority management
2021-01-03
LeetCode - 900. RLE 迭代器
Gif image analysis drawing RGB to YUV table lookup method to reduce CPU occupancy
01仿B站项目业务架构
Leetcode bit operation
Dynamic layout management
[combinatorics] combinatorial existence theorem (three combinatorial existence theorems | finite poset decomposition theorem | Ramsey theorem | existence theorem of different representative systems |
is_ power_ of_ 2 judge whether it is a multiple of 2
QT detection card reader analog keyboard input
It is difficult to quantify the extent to which a single-chip computer can find a job
byte alignment
Application of external interrupts
Leetcode - 933 number of recent requests