The difference and contact details of Lib and DLL in Windows environment

Source: Internet
Author: User

There are two types of libraries in C + +:
1, Lib contains the function of the DLL file and the location of the function in the file (portal), the code by the runtime loaded in the process space by the DLL provided, called Dynamic Link library, the Dynamics link libraries. (This method is more flexible and the program is small, but requires both. exe and DLL to be published)
2, Lib contains the function code itself, at compile time directly to the code into the program, called the Static link library of the passive link libraries. (This method is not very flexible, because Lib is compiled into the. exe, the written program is large, but only the EXE needs to be published, no DLL files are required)
There are two ways to link:
1. 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.
2. Static links use static link libraries, the linker 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.

(static connection)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, slightly.

(Dynamic Connection)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 Win32API 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 in the source code ( LIB, "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.
How to create a DLL project (slightly).
(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.

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

#pragma comment (lib,"DLLSample.lib")    //You can also set a link to the library in the project properties

intMain ()
{
Testdll (123); function in the //DLL, declared in DllSample.h
return(1);
}

(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).

#include<iostream>
#include<windows.h>         //using functions and some special variables
typedefvoid (*DllFunc) (int);
intMain ()
{
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 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 contact details of Lib and DLL in Windows environment

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.