The difference and use of LIB and DLL

Source: Internet
Author: User

There are two kinds of libraries:
One is that the library contains the information (ingress) of the DLL file and the location of the function in the file, which is provided by the DLL that is loaded in the process space by the runtime, called the dynamic link library.
One is that the library contains the function code itself, which is added directly to the program at compile time, called the Static link library.


There are two ways to link:
Dynamic linking uses a dynamic-link library that allows executable modules (. dll files or. exe files) to contain only the information required to locate the executable code of a DLL function at run time.
Static linking uses the static link library, which obtains all referenced functions from the static link library lib and places the library with the code in the executable file.

the differences between Lib and DLL are as follows:
(1) Lib is used at compile time, DLL is used at runtime. If you want to complete the compilation of the source code, you only need lib, if you want to make a dynamically linked program run, you only need the DLL.
(2) If there is a DLL file, then Lib is generally some index information, log the function of the DLL's entry and location, DLL is the specific content of the function, if only the Lib file, then the Lib file is statically compiled, the index and implementation are in it. Using a statically compiled Lib file, you do not need to hang the dynamic library when you run the program, the disadvantage is that the application is larger, and the flexibility of the dynamic library is lost, and the new version is released when the new application is published.
(3) In case of dynamic link, there are two files: one is lib file and one is DLL file. Lib contains the name and location of the function 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 application's executable file, it is not the called function code, but the address of the corresponding function code in the DLL, thus saving memory resources. DLL and LIB files must be released with the application or the application will produce an error. If you do not want to use the Lib file or no Lib file, you can use the WIN32 API function LoadLibrary, GetProcAddress load.

There are two files to be aware of using Lib:
(1). h header file containing class or symbol prototypes or data structures that describe the output in Lib. When the application calls Lib, it needs to include the file in the application's source file.
(2). LIB file.

There are three files to be aware of using DLLs:
(1). h header file containing the. h file that describes the output class or symbol prototype or data structure in the DLL. When an application calls a DLL, it needs to include the file in the application's source file.
(2). LIB file is a file generated by a DLL after it is compiled and successfully linked, and it is necessary to introduce the file into the application when other applications call the DLL, otherwise an error is generated. If you do not want to use the Lib file or no Lib file, you can use the WIN32 API function LoadLibrary, GetProcAddress load.
(3). dll file, the real executable, after the successful development of the application at the time of publishing, only the. exe file and the. dll file are required, and the. lib file and. h header files are not required.

ways to use Lib:
In static Lib, a Lib file is actually a collection of any obj files, and the obj file is generated by the CPP file compilation. When compiling this static library project, there is no link error at all, and even if it is wrong, it will only be exposed in the Ext file or DLL project using this lib.
Create a new Static library type project Lib in the VC, add the Test.cpp file and the Test.h file (including the function declaration in the header file), and then compile, generate the Lib.lib file.
There are two ways to use this lib for other projects:
(1) Add the Lib.lib file in the Project->link->object/library module (query the project directory first, then query the system Lib directory), or add the command #pragma comment (lib, ") in the source code. Lib.lib ").
(2) Copy the Lib.lib into the directory where the project is located, or execute the file-generated directory, or the system Lib directory.
(3) Add the corresponding header file test.h.

ways to use DLLs:
Using LIB in a dynamic link is not a collection of obj files, that is, there is no actual implementation, it simply provides the information needed to dynamically link to the DLL, which can be generated by the compiler when compiling a DLL project.


(1) Implicit link
The first method is to add the. lib file through the Project->link->object/library module (or add the directive #pragma comment (lib, "Lib.lib") to the source code), and place the. dll file in the directory where the project resides, and then add the corresponding. h header file.

1#include"stdafx.h"2#include"DLLSample.h"3 4 #pragmaComment (lib, "DLLSample.lib")//You can also set a link to the library in the project properties5 6 intMain ()7 {8Testdll (123);//A function in the DLL, declared in DllSample.h9         return(1);Ten}

(2) Explicit links
Requires a function pointer and WIN32 API function LoadLibrary, GetProcAddress mount, using this loading method, does not require the. lib file and the. h header file, only the. dll file is required (the. dll file is placed in the project directory).

1#include <iostream>2#include <windows.h>//using functions and some special variables3typedefvoid(*dllfunc) (int);4 intMain ()5 {6 DllFunc DllFunc;7HInstance hinstlibrary = LoadLibrary ("DLLSample.dll");8 9         if(Hinstlibrary = =NULL)Ten         { One FreeLibrary (hinstlibrary); A         } -DllFunc = (dllfunc) GetProcAddress (Hinstlibrary,"Testdll"); -         if(DllFunc = =NULL) the         { - FreeLibrary (hinstlibrary); -         } -DllFunc (123); +Std::cin.Get(); - FreeLibrary (hinstlibrary); +         return(1); A}

The LoadLibrary function takes a name as an argument, obtains an instance of the DLL (the HINSTANCE type is the handle to the instance), and usually calls the function to see if the function return succeeds, or null if unsuccessful (the handle is not valid). This calls the function FreeLibrary frees the memory that the DLL obtains.
The GetProcAddress function uses the DLL's handle and the name of the function as a parameter, returns the corresponding function pointer, must use a strong turn, determines if the function pointer is null, and if so, invokes the function FreeLibrary releases the memory obtained by the DLL. After that, you can use a function pointer to invoke the actual function.
Finally, remember to use the FreeLibrary function to free up memory.

Note: How does the application find the DLL file?
With LoadLibrary explicit linking, you can specify the full path to the DLL file in the parameters of the function, and if you do not specify a path or implicitly link, Windows will follow the following search order to locate the DLL:
(1) directory containing EXE files
(2) Project catalogue
(3) Windows system directory
(4) Windows directory
(5) A list of directories listed in the PATH environment variable

The difference and use of LIB and DLL

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.