Comprehensive Analysis of Linux dynamic libraries and windows dynamic libraries

Source: Internet
Author: User

This article analyzes the calling methods and programming methods of Linux dynamic libraries and windows dynamic libraries. The purpose of the dynamic library is to reduce the program size, save space, improve efficiency, and have high flexibility. Using the dynamic library technology makes it easier to upgrade the software version. Functions in the dynamic library are not part of the execution program, but loaded as needed, its Execution Code can be shared among multiple programs at the same time.

Windows dynamic library technology

Dynamic Link Library is an important technical means for Windows applications to share resources, save memory space, and improve usage efficiency. Common dynamic libraries include external functions and resources, and some dynamic libraries only contain resources, such as Windows Font Resource files, which are called resource dynamic link libraries. Generally, the dynamic library uses. dll,. drv,. fon, and so on as the suffix. The corresponding windows static library usually ends with. lib, and Windows itself implements some major system functions in the form of dynamic library modules.

The Windows dynamic library is loaded into the virtual space of the process at runtime, and the memory allocated from the virtual address space of the calling process becomes part of the calling process. DLL can only be accessed by the thread of the process. The DLL handle can be used by the calling process, and the call Process Handle can be used by the DLL. The DLL module contains various export functions to provide external services. A dll can have its own data segment but does not have its own stack. It uses the same stack mode as the application that calls it. a dll has only one instance in the memory; DLL implements code encapsulation. The compilation of DLL has nothing to do with the specific programming language and compiler. You can use DLL to implement mixed language programming. Any object created by the code in the DLL function, including variables, is owned by the thread or process that calls it.

Based on different call methods, dynamic library calls can be divided into static call methods and dynamic call methods.

(1) Static call, also known as implicit call. The compiling system completes DLL loading and the encoding for DLL uninstallation at the end of the application. Windows system is responsible for counting the number of DLL calls ), the call method is simple and can meet common requirements. The call method is usually to add the. LIB file generated when a dynamic Connection Library is generated to the project of the application. To use a function in the DLL, you only need to declare it in the source file. The LIB file contains the symbolic name and selectable Identification Number of each DLL export function and the DLL file name, excluding the actual code. The information contained in the Lib file enters the generated application. The called DLL file is loaded into the memory when the application is loaded.

(2) Dynamic calling, that is, the explicit calling method, is implemented by the programmer using the API function to load and uninstall the DLL to call the DLL. It is complicated, but can use the memory more effectively, is an important way to compile large-scale applications. In Windows, functions related to dynamic library calls include:

① LoadLibrary or AfxLoadLibrary of MFC), load the dynamic library.
② GetProcAddress: Get the function to be introduced and convert the symbol name or ID number to the internal DLL address.
③ FreeLibrary or AfxFreeLibrary of MFC) to release the dynamic link library.

Creating a dynamic library in windows is also very convenient and simple. In Visual C ++, you can create a DLL program directly written in C language without using MFC, or create a DLL program based on the MFC class library. Each DLL must have an entry point. In VC ++, DllMain is a default entry function. DllMain is responsible for Initialization and Termination. The dynamic library output function also has two conventions, which are based on the call Convention and name modification convention. The functions defined by the DLL program are divided into internal functions and export functions. The functions exported by the dynamic library are called by other program modules. You can use the following methods to export functions:

① Use the EXPORT part of the module definition file to specify the function or variable to be input.
② Use the modifier _ declspec (dllexport) provided by MFC ).
③ Use the/EXPORT command line to output related functions in the command line mode.

In a windows dynamic library, you sometimes need to write a module definition file (. DEF), which is a text file consisting of module statements used to describe the DLL attributes.

Linux shared object technology

In Linux, many Shared Object technologies are used. Although they correspond to dynamic libraries in Windows, they are not called dynamic libraries. The corresponding shared object file uses. so as the suffix. For convenience, this concept is not specifically distinguished in this article. There are many shared objects ending with so in the/lib of the Linux System and the/usr/X11R6/lib directory of the standard graphic interface. Similarly, in Linux, there is also a static function library call method, and the corresponding suffix ends with.. Linux uses the shared object technology to facilitate sharing between programs, save space occupied by programs, and increase program scalability and flexibility. Linux also allows developers to replace system modules with the modules in their libraries through LD-PRELOAD variables.

Like Windows systems, it is easier to create and use dynamic libraries in Linux. You can add the-shared option when compiling the source program of the function library. In this way, the generated execution program is the dynamic link library. Generally, such a program is suffixed with so. In the process of Linux dynamic library program design, the process is usually to write user interface files, usually. h file, compile the actual function file. c or. cpp is the suffix, and then write the makefile file. This can be avoided for smaller dynamic library programs, but this design makes the program more reasonable.

After the dynamic Connection Library is compiled and generated, it can be called in the program. In Linux, you can use multiple calling methods, the same as the Windows System directory (.. \ system32, etc.), you can copy the dynamic library file to the/lib directory or create a symbolic connection in the/lib directory for all users to use. The following describes functions frequently used in Linux to call dynamic libraries. However, when using a dynamic library, the source program must contain the dlfcn. h header file, which defines the prototype of the function used to call the dynamic link library.

(1) _ open the Dynamic Link Library: dlopen, function prototype void * dlopen (const char * filename, int flag );
Dlopen is used to open the dynamic link library with the specified name (filename) and return the operation handle.

(2) function execution address: dlsym. The prototype of the function is void * dlsym (void * handle, char * symbol );
Dlsym returns the Execution Code address of the function corresponding to the symbol Based on the handle and symbol of the dynamic link library.

(3) Close the Dynamic Link Library: dlclose. The function prototype is int dlclose (void * handle );
Dlclose is used to close the dynamic link library of the specified handle. It will be uninstalled only when the usage count of this dynamic link library is 0.

(4) dynamic library error function: dlerror. The prototype of the function is const char * dlerror (void). When the dynamic link library operation function fails to be executed, dlerror can return error information, if the return value is NULL, the operation function is successfully executed.

After obtaining the function execution address, you can call the functions in the dynamic library according to the function interface declaration provided by the dynamic library in the Use Program of the dynamic library. When compiling the makefile file of the program that calls the dynamic library, you must add the compilation options-rdynamic and-ldl.

In addition to writing and calling dynamic libraries in this way, the Linux operating system also provides a more convenient dynamic library calling method, which also facilitates the calling of other programs, this method is similar to the implicit link in Windows. The dynamic library name is "lib *. so .*". In this naming method, the first * indicates the name of the dynamically linked library, and the second * indicates the version number of the dynamic library, or the version number does not exist. In this call method, you need to maintain the configuration file/etc/ld of the dynamic link library. so. conf to make the dynamic link library used by the system. Usually, the directory name of the dynamic link library is appended to the dynamic link library configuration file. If the file has an X window System release version, the file has/usr/X11R6/lib, which points to the directory of the dynamic link library of the X window System. To make the dynamic link library shared by the system, you also need to run the management command./sbin/ldconfig of the dynamic link library. When compiling the referenced dynamic library, you can use the-l or-L option in gcc or directly reference the required dynamic link library for compilation. In Linux, you can use the ldd command to check the program dependency shared library.

  1. Comprehensive Analysis of Linux dynamic library features and Creation
  2. Detailed analysis of Linux dynamic library usage
  3. The top 10 Linux versions in China are described in detail)
  4. Linux Regular Expression 1)
  5. Reasons for choosing the path to Linux Authentication

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.