[C ++ Primer] differences between static link library (lib) and dynamic link library (dll) and their usage

Source: Internet
Author: User

If you are interested in static and dynamic link libraries in Linux, click --> you know
I. Concept
1) The static Link Library is the. lib file you are using. The code in the library needs to be connected to your executable file, so the static connection executable file is generally larger.
Usage
  
1> Format: # pragma comment (lib, "XXX. lib ")
2> development environment:
1. If you use VC, you can add your static library to Project Setting -- & gt; Link, or directly add the. lib file to your Project.
2. If Visual Studio is used, go to project → configure properties → connector → input → Add the. lib file to additional dependencies
Constructor
In the case of a static library, functions and data are compiled into a binary file (usually with the extension *. lib), the Visual C ++ compiler will restore these functions and data from the static library during the link process, and combine them with other modules in the application to generate executable files. This process is called "static link". In this case, because all the content required by the application is copied from the library, the static library itself does not need to be released together with the executable file.
Programming: Pay attention to two files when using lib:
(1) The. h header file contains the class or symbol prototype or data structure described in lib. When an application calls lib, the file must be included in the application's source file.
(2) See the. lib File above.
2) the abbreviation of Dynamic Link Library. DLL is a Library that contains code and data that can be used by multiple programs at the same time. DLL is not an executable file. Dynamic links provide a way for a process to call a function that does not belong to its executable code. The executable code of a function is located in a DLL, which contains one or more functions that have been compiled, linked, and stored separately from the processes that use them. DLL also helps to share data and resources. Multiple applications can simultaneously access the content of a single DLL copy in the memory. DLL is a library that contains code and data that can be used by multiple programs at the same time.
DLL example
ActiveX Control (. ocx) file: the example is the calendar control, which allows you to select a date from the calendar. ·
Control Panel (. cpl) file: An Example of the cpl file is an item located in the control panel. Each item is a dedicated DLL.
· Device Driver (. drv) File
Programming;
When using dll, pay attention to three files:
(1). h header file, which contains a. h file indicating the output class or symbol prototype or data structure in dll. When an application calls a dll, the file must be included in the application's source file.
(2 ). the LIB file is generated after the dll is compiled and linked successfully. It is used to introduce the file into the application when other applications call the dll. Otherwise, an error occurs. If you do not want to use the lib file or do not have the lib file, you can use the WIN32 API functions LoadLibrary and GetProcAddress for loading.
(32.16.dll file, which is a real executable file. Only the. exe file and. dll file are available when the application program behind the development is running, and the. lib file and. h file are not required.
Ii. Preliminary understanding

There are two types of databases:
One is that lib contains information about the DLL file where the function is located and the Function Location (entry) in the file. The code is provided by the DLL loaded in the process space during runtime, it is called the dynamic link library.
One is that lib contains the Function Code itself. during compilation, the code is directly added to the program, called the static link library of the static link library.
There are two Connection Methods:
The dynamic link uses the dynamic link library to permit the execution of the dig (.dll file or the .exe file) only contains the information required to locate the executable code of the DLL function at runtime.
Static links use static link libraries. The linker obtains all referenced functions from the static Link Library lib and puts the libraries and codes together in the executable files.


Iii. Differences between lib and dll are as follows:
(1) lib is used for compiling and dll is used for running. To compile the source code, you only need lib. To run the dynamic link program, you only need dll.
(2) If a dll file exists, lib is generally an index that records the entry and position of the function in the dll. The dll contains the specific content of the function. If only the lib file exists, the lib file is statically compiled, and the index and implementation are all in it. The static library does not need to be attached to the dynamic library when running the program. The disadvantage is that the application is large and the flexibility of the dynamic library is lost, only release new applications when releasing a new version.
(3) In the case of dynamic links, there are two files: one is the LIB file and the other is the DLL file. LIB contains the name and location of the function exported by the DLL. The DLL contains the actual functions and data. The application uses the LIB file to link to the DLL file. In the executable file of an application, the address of the corresponding function code in the DLL is not stored in the called function code, thus saving memory resources. DLL and LIB files must be released along with the application; otherwise, errors may occur in the application. If you do not want to use the lib file or do not have the lib file, you can use the WIN32 API functions LoadLibrary and GetProcAddress for loading.

 

Use lib:
In static lib, a lib file is actually a collection of any obj files, which are compiled and generated by the cpp file. When compiling such a static library project, there will be no link error at all; even if there is a mistake, it will only be exposed in the EXT file or DLL project using this lib.
Create a static library project Lib in VC and add test. cpp file and test. h file (including the function declaration in the header file), and then compile it to generate Lib. lib file.
There are two ways to use this lib for other projects:
(1) Add Lib to project> link> Object/Library Module. lib file (first query the project directory, then query the Lib directory); or add the command # pragma comment (lib, "Lib. lib ").
(2) Upload Lib. lib to the directory where the project is located, the directory generated by the execution file, or the system Lib directory.
(3) Add the corresponding header file test. h.

DLL method:
The lib in the dynamic link is not a collection of obj files, that is, there is no actual implementation in it, it only provides the information required to dynamically link to the DLL, this lib can be generated by the compiler when compiling a DLL project.
Method for creating a DLL Project (omitted ).
(1) implicit link
The first method is to add it to the project-> link-> Object/Library Module. lib file (or add the command # pragma comment (lib, "Lib. lib), and. place the dll file in the directory where the project is located, and then add the corresponding. h header file.
[Html]
# Include "stdafx. h"
# Include "DLLSample. h"
 
# Pragma comment (lib, "DLLSample. lib") // you can set the Library link in the project properties.
 
Int main ()
{
TestDLL (123); // The function in dll, which is declared in DllSample. h.
Return (1 );
}


(2) explicit link
The function pointer and WIN32 API functions LoadLibrary and GetProcAddress are required for loading. This loading method is not required. lib file and. h header file, only. dll file. dll files are placed in the project directory ).
[Html]
# 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 );
}


The LoadLibrary function uses a name as a parameter to obtain the DLL instance (the HINSTANCE type is the instance handle). Generally, after calling this function, you need to check whether the function returns success, if the call fails, NULL is returned (the handle is invalid). The FreeLibrary function is called to release the DLL Memory.
The GetProcAddress function uses the DLL handle and function name as the parameter to return the corresponding function pointer. At the same time, strong conversion is required. It determines whether the function pointer is NULL, if yes, call the FreeLibrary function to release the DLL Memory. Then, you can use the function pointer to call the actual function.
Remember to use the FreeLibrary function to release the memory.

Note: How does the application find the DLL file?
When you use the LoadLibrary explicit link, you can specify the full path of the DLL file in the function parameters. If you do not specify a path or perform an implicit link, windows will follow the search order below to locate the DLL:
(1) directory containing the EXE file
(2) project directory
(3) Windows System directory
(4) Windows Directory
(5) A series of directories listed in the Path environment variable


Author: tianshuai11

Related Article

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.