The difference and use of LIB and DLL under windows

There are two kinds of libraries:
one is that LIB contains the DLL file where the function is located and the information (entry) of the function location in the file, and the code is provided by the DLL loaded in the process space at runtime, which is called dynamic link library.
One is that LIB contains the function code itself, and the code is directly added to the program at compile time, which is called a static link library.
There are two ways of linking:
Dynamic linking uses a dynamic link library, which allows executable modules (.dll files or .exe files) to contain only the information needed to locate the executable code of the DLL function at runtime.
Static linking uses a static link library, and the linker obtains all referenced functions from the static link library LIB, and puts the library into the executable file together with the code.

The difference between lib and dll is as follows:
(1) lib is used at compile time, and dll is used at runtime. If you want to compile the source code, you only need lib; if you want to make the dynamically linked program run, you only need dll (if the dll is not found at runtime, an error will be reported).
(2) If there is a dll file, then lib is generally some index information, which records the entry and location of the function in the dll, and the specific content of the function in the dll; if there is only a lib file, then this lib file is compiled statically, and the index and implementation are included. Using a statically compiled lib file does not require a dynamic library when running the program. The disadvantage is that the application is relatively large, and the flexibility of the dynamic library is lost. When a new version is released, a new application must be released.
(3) In the case of dynamic linking, there are two files: one is a LIB file and the other is a DLL file. LIB contains the function names and locations exported by the DLL, the DLL contains the actual functions and data, and the application uses the LIB file to link to the DLL file. In the executable file of the application program, what is stored is not the called function code, but the address of the corresponding function code in the DLL, thereby saving memory resources. DLL and LIB files must be distributed with the application, otherwise the application will generate errors. If you don't want to use lib file or don't have lib file, you can use WIN32 API function LoadLibrary, GetProcAddress to load.

Two files need to be paid attention to when using lib:
(1) .h header file , which contains the class or symbol prototype or data structure described in lib. When an application calls lib, this file needs to be included in the source file of the application.
(2) .LIB files , omitted.

Three files should be paid attention to when using dll:
(1) .h header file , which contains the .h file that describes the output class or symbol prototype or data structure in the dll. When the application program calls the dll, the file needs to be included in the source file of the application program.
(2) The .LIB file is a file generated after the dll is successfully compiled and linked. Its function is that when other applications call the dll, the file needs to be introduced into the application, otherwise an error will occur. If you don't want to use lib file or don't have lib file, you can use WIN32 API function LoadLibrary, GetProcAddress to load.
PS: Why is only the DLL file generated in the project, but not the LIB file?
The export of DLL is one more step than LIB, you need to add a module definition file (.def), and add the following code to the file

LIBRARY

EXPORTS

dll_add //注意在这写的是函数名,而不是函数申明

(3) .dll file , the real executable file, when the successfully developed application is released, it only needs to have .exe file and .dll file, and does not need .lib file and .h header file.

The method of using lib:
In static lib, a lib file is actually a collection of any obj files, and obj files are generated by compiling cpp files. When compiling this kind of static library project, you will not encounter link errors at all; even if there are errors, they will only be exposed in the EXT file or DLL project that uses this lib.
Create a new static library project Lib in VC, add the test.cpp file and test.h file (the header file includes the function declaration), and then compile to generate the Lib.lib file.
There are two ways for other projects to use this lib:
(1) Add the Lib.lib file in project->link->Object/Library Module (first query the project directory, and then query the system Lib directory); or add the instruction #pragma comment(lib, “Lib.lib”) to the source code.
(2) Copy Lib.lib into the directory where the project is located, or the directory generated by executing the file, or the system Lib directory.
(3) Add the corresponding header file test.h.
1.
Add dependencies to let vs know the file name to depend on.
Add library directory to let vs know the directory of the file to depend on

. Add an h file to
let vs know the file to depend on
. The method of creating a DLL project (omitted). (1) Implicit linking



The first method is: add the .lib file through project->link->Object/LibraryModule (or add instructions to the source code

#pragma comment(lib, “Lib.lib”)),并将.dll文件置入工程所在目录,然后添加对应的.h头文件。

#include "stdafx.h"
#include "DLLSample.h"

#pragma comment(lib, "DLLSample.lib")    //你也可以在项目属性中设置库的链接

int main()
{
    
    
        TestDLL(123);   //dll中的函数,在DllSample.h中声明
        return(1);
}

(2) Explicit linking
requires function pointers and WIN32 API functions LoadLibrary and GetProcAddress to be loaded. With this loading method, .lib files and .h header files are not needed, only .dll files are needed (put the .dll files into the project directory) .

#include <iostream>
#include <windows.h>         //使用函数和某些特殊变量
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);
}

The LoadLibrary function uses a name as a parameter to obtain an instance of the DLL (the HINSTANCE type is the handle of the instance). Usually, after calling this function, you need to check whether the function returns successfully. If not, return NULL (the handle is invalid). At this time, call the function FreeLibrary to release the memory obtained by the DLL.
The GetProcAddress function uses the handle of the DLL and the name of the function as parameters to return the corresponding function pointer, and at the same time must use forced transfer; judge whether the function pointer is NULL, if so, call the function FreeLibrary to release the memory obtained by the DLL. Thereafter, the actual function can be called using the function pointer.
Finally, remember to release the memory using the FreeLibrary function.

Note: How does the application find the DLL file?
Use LoadLibrary to explicitly link, then you can specify the full path of the DLL file in the parameters of the function;
if you do not specify the path, or it is an implicit link, Windows will follow the following search order to locate the DLL:

(1) The directory containing the EXE file
(2) The project directory
(3) The Windows system directory
(4) The Windows directory
(5) A series of directories listed in the Path environment variable

Guess you like

Origin blog.csdn.net/sanqiuai/article/details/124936399