Differences between dynamic and static libraries and runtime libraries and import/export Libraries

Source: Internet
Author: User
Tags terminates import database

1. Runtime Library: a typical Runtime library example in UNIX is libc, which contains standard C functions, such as print () and exit, users can create their own runtime libraries (DLL in Windows), and the specific details depend on the compiler and operating system.

2. Static Library: functions and data are compiled into a binary file (typically with the extension. lib), the static library is actually linked to the EXE during the link, and the library itself does not need to be released together with the executable file.

3. Dynamic library: the dynamic library created with VC ++ contains two files, one lib file and one DLL file. This lib file is imported into the database, not a static library, import to database is also called input or import to database.

Note: In Windows, the extensions of dynamic libraries and runtime libraries are. dll, and the extension of COM components is. dll. The extensions of dynamic libraries and static libraries are. Lib.

How to call a dynamic library in Windows:
1. Implicit loading:ProgramContains lib files and. H files. implicit links are sometimes called dynamic links during static loading or loading. For example:
# Include "somedll. H"
# Pragma comment (Lib, "somedll. lib ")
Then you can directly call the functions in this DLL. Note that somedll. dll is still required during runtime.

2. Display loading: loadlibrary, getprocaddress, and freelibrary are used. The. h file and. Lib file are not required, but the function prototype must be known. Explicit links are sometimes called dynamic links during loading or running.

3. Difference: If the DLL is not found when the process starts, the operating system terminates the process that uses the implicit link. In this case, the process that uses the explicit link will not be terminated and can be restored from the error.

For detailed database structure information about Win32 DLL, Unix shared library, and common library, see the book "linker and loader.

Msdn:
1. Are you sure you want to use the link method:
There are two types of links: implicit link and explicit link.

Implicit Link
ApplicationCodeAn implicit link occurred when calling the export DLL function. When you call an executable fileSource codeWhen compiled or compiled, DLL function calls generate an external function reference in the object code. To resolve another reference, the application must be linked to the import library (. Lib file) provided by the DLL creator.

Importing data to the database only contains the DLL loading code and the code for calling the DLL function. After an external function is found in the import/export warehouse, the code of this function is notified to the linker In the DLL. To resolve external references to the DLL, the linker only needs to add information to the executable file to notify the system where to find the DLL Code when the process starts.

When the system starts a program that contains a dynamic link reference, it uses the information in the executable file of the program to locate the required DLL. If the system cannot locate the DLL, it terminates the process and displays a dialog box to report the error. Otherwise, the system maps the DLL module to the address space of the process.

If any dll has an entry point function (used to initialize and terminate code), the operating system calls this function. In the parameter passed to the entry point function, there is a code specified to indicate that the DLL is being attached to the process. If the entry point function does not return true, the system terminates the process and reports an error.

Finally, the system modifies the executable code of the process to provide the starting address of the DLL function.

Like the rest of the program code, the DLL Code maps to the address space of the process when the process starts and is loaded to the memory only when necessary. Therefore, the. Def file is used to control the loaded preload and loadoncall Code attributes in earlier versions of Windows.

Explicit link
Most applications use implicit links because this is the easiest method to use. However, explicit links are also required. The following are some common causes for using explicit links:

The application does not know the name of the DLL to be loaded until it is running. For example, an application may need to obtain the DLL name and the export function name from the configuration file.

If the DLL is not found when the process starts, the operating system terminates the process that uses the implicit link. In this case, the process that uses the explicit link will not be terminated and can be restored from the error. For example, a process can notify users of errors and allow users to specify other DLL paths.

If any DLL linked to a process that uses the implicit link has a failed dllmain function, the process will also be terminated. In this case, the process that uses the explicit link will not be terminated.

Because Windows loads all the DLL files during application loading, it is slow to start applications that implicitly link to many DLL files. To improve the Startup Performance, the application can implicitly link to the DLL immediately after loading and explicitly link to other DLL when needed.

You do not need to link the application with the import/export Link under the explicit link. If the DLL changes, the application that uses the explicit link does not need to be re-linked (assuming they call getprocaddress with the function name instead of the serial number value ), applications that use implicit links must be re-linked to the new import database.

The following are two disadvantages of explicit links:

If the DLL has a dllmain entry point function, the operating system calls this function in the context of the thread that calls loadlibrary. If the DLL has been attached to the process because loadlibrary has been called but the freelibrary function has not been called, this entry point function will not be called. If the DLL uses the dllmain function to initialize each thread of the process, the explicit link will cause problems because the existing thread will not initialize when loadlibrary (or afxloadlibrary) is called.

If the DLL declares the static scope data as _ declspec (thread), The dll will cause a protection error during explicit link. After loadlibrary is used to load a DLL, the DLL will cause a protection error whenever the code references this data. (Static scope data includes both global static items and local static items .) Therefore, when creating a DLL, you should avoid using the local storage area of the thread, or notify the DLL user of potential defects (when the user attempts to dynamically load the DLL.

2. Implicit link:
To implicitly link to the DLL, the executable file must obtain the following items from the DLL provider:

Header file (. h file) that contains the declaration of the exported function and/or C ++ class ). Classes, functions, and data must have _ declspec (dllimport). For more information, see dllexport and dllimport.

Import to database (. Lib files ). (When a DLL is generated, the linker creates a import/export database .)

The actual DLL (. dll file ).

The executable file using DLL must include the header file, which contains the export function (or C ++ class) in each source file, which contains the call to the export function. From the encoding point of view, the function call for exporting a function is the same as that for any other function call.

To generate a call executable file, it must be linked to the import/export warehouse. If an external file is generated, specify the file name of the import/export object. This file lists other objects (. OBJ) to be linked.

When calling executable files, the operating system must be able to locate the DLL file.

 
3. Explicit link:
In an explicit link, the application must call functions to explicitly load the DLL at runtime. To explicitly link to the DLL, the application must:

Call loadlibrary (or similar functions) to load the DLL and obtain the module handle.

Call getprocaddress to obtain the function pointer pointing to each export function to be called by the application. Because the application uses a pointer to call the DLL function, the compiler does not generate external references, so it does not need to be linked to the import database.

Call freelibrary after using the DLL.
Typedef uint (callback * lpfndllfunc1) (DWORD, uint );
...

Hinstance hdll; // handle to DLL
Lpfndllfunc1 lpfndllfunc1; // function pointer
DWORD dwparam1;
Uint uparam2, ureturnval;

Hdll = loadlibrary ("mydll ");
If (hdll! = NULL)
{
Lpfndllfunc1 = (lpfndllfunc1) getprocaddress (hdll,
"Dllfunc1 ");
If (! Lpfndllfunc1)
{
// Handle the error
Freelibrary (hdll );
Return some_error_code;
}
Else
{
// Call the Function
Ureturnval = lpfndllfunc1 (dwparam1, uparam2 );
}
}

4. Link the executable file to the DLL
One of the following two methods of linking an executable file to (or loading) dll:

Implicit Link

Explicit link

Implicit links are sometimes called dynamic links during static loading or loading. Explicit links are sometimes called dynamic links during loading or running.

Under the implicit link, use the DLL executable file to link to the import library (. Lib file) provided by the DLL creator ). When the DLL executable file is used for loading, the operating system loads the DLL. The client executable file calls the DLL export function, as if these functions are included in the executable file.

In an explicit link, function calls must be performed on the executable files using the DLL to explicitly load and uninstall the DLL and access the export function of the DLL. The client executable file must use the function pointer to call the export function.

The executable file can use the same DLL for two link methods. In addition, these mechanisms are not mutually exclusive because an executable file can be implicitly linked to a DLL and the other can be explicitly appended to the 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.