Dynamic and Static Link Libraries

Source: Internet
Author: User

 

  Dynamic Link Library Static Link Library
Different files are generated. *. Dll *. Lib *. Lib
File Functions

Lib is required for compiling links, and DLL is required for running. If you want to complete code compilation, you can use Lib. If you want dynamic link programs to run, DLL is enough.

The LIB file must be connected to the application in the compiler;

DLL is called at runtime.

If only the Lib file is available, it will be statically compiled this week. The index and the actual content are in it.
Code sharing When using a DLL, you do not have to include it in the final EXE file. When executing an EXE, You can dynamically reference and uninstall this DLL file independent of the exe. Whether or not, all the commands in lib are directly included in the final generated EXE file.
Database inclusion Other dynamic or static link libraries can be included. Other dynamic or static libraries cannot be included. The static lib generation process only compiles without links. In fact, it is equivalent to the OBJ compiled by CPP, which is simply packaged into a file.
Differences between lib generation Contains the information about the DLL file where the function is located and the function location in the file, and the information becomes exported and exported. "Dynamic lib" is equivalent to an H file, which declares the export part of the Implementation part (DLL file. After compilation, only part of the export declaration is compiled into the host Program. The corresponding DLL file is required for running. Contains the function code. "Static lib" stores the export declaration and implementation in the Lib file. After compilation, all the code is embedded in the Host Program. After the # include header file contains these functions, the application can use the functions in the Lib file.
Differences between lib files and CPP files The CPP file is the source code, and the library file is the compiled Binary Code. For example, you can call the Windows API but cannot see the same source code. The CPP file is the source code, and the library file is the compiled Binary Code. For example, you can call the Windows API but cannot see the same source code.
Call Method Two types of implicit call and display call Include to use
Implicit call

Application:The application uses the Lib file to link to the required DLL file. The functions and data in the library are not copied to the EXE file. Therefore, in the executable file of the application, it stores not the called function code, but the memory address of the function to be called in the DLL. (LIB files are required)

Static call:The compilation system loads the DLL and uninstalls the DLL when the application ends. When the application that calls a DLL ends, if other programs in the system use the DLL, Windows will subtract 1 from the DLL application record, it is not released until all programs that use the DLL end. The static call method is simple and practical, but it is not as flexible as the dynamic call method.

The static call method does not need to use system APIs to load or uninstall the DLL and obtain the address of the exported function in the DLL.This is because, when programmers compile an application through static links. function symbols matching the exported characters in the Lib file will enter the generated EXE file ,. the file name of the corresponding DLL file contained in the Lib file is also stored in the EXE file by the compiler. When a DLL file needs to be loaded while the application is running, Windows will find and load the DLL based on the information, and then use the symbolic name to implement dynamic links to the DLL function. In this way, exe can directly call the DLL output function through the function name, just like calling other functions in the program.

Steps:

1. Ensure that both a. dll and A. Lib files have

2. The declaration of the function to be called and the following statements are written to a header file.

# Pragma comment (Lib, "A. lib ")//// Specify the connection to the static database

None
Explicit call

Application:If the DLL does not have a corresponding. Lib file, you can only use the display call (dynamic loading) method.

Dynamic call:The feature of this method is that programmers use API functions to load and uninstall DLL files. programmers can decide when to load or not to load DLL files. explicit links determine which DLL files to load during runtime.

To call a dynamic library dynamically, follow these steps:

1. Create a function pointer. the pointer data type must be consistent with the called DLL-derived function.

2. Call the DLL explicitly through the Win32 API function loadlibrary (). This function returns the instance handle of the DLL.

3. Use the Win32 API function getprocaddress () to obtain the function address of the DLL to be called and assign the result to the pointer type of the custom function.

4. Use the function pointer to call the DLL function.

5. After the call is completed, use the Win32 API function freelibrary () to release the DLL function.

None
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.