Analysis and Comparison of Windows and Linux Dynamic Link Libraries

Source: Internet
Author: User

Abstract: The concept of dynamic library exists in Windows and Linux systems. Dynamic library can effectively reduce the program size and save space, improve efficiency, increase program scalability, and facilitate modular management. However, because the dynamic libraries of different operating systems have different formats, dynamic library programs need to be transplanted when calling different operating systems. This article analyzes and compares the dynamic library technology of the two operating systems, and provides the methods and experience of porting the dynamic library compiled by Visual C to Linux.

1. Introduction

Dynamic library (DLL) is a frequently used technology in programming. It aims to reduce the program size, save space, improve efficiency, and have high flexibility. It is easier to upgrade the software version by using the dynamic library technology. Unlike the static Link Library, the functions in the dynamic library are not part of the execution process, but are loaded as needed, its Execution Code can be shared among multiple programs at the same time.

This method can be used in both Windows and Linux operating systems for software design, but their calling methods and programming methods are different. This article first analyzes the dynamic library calling methods and programming methods commonly used in these two operating systems, and then analyzes and compares the differences between the two methods, finally, based on the actual porting experience, this article introduces how to port the Windows dynamic library compiled by VC to Linux.

2. Dynamic library technology

2.1 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 added to the virtual space of the Process during running, 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 (including variables) created by the code in the DLL function 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 loads the DLL and the encoding of the DLL uninstallation when the application ends (the number of DLL calls in Windows). The Calling method is simple and can meet the general 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 is imported into 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) to load dynamic libraries.
② 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 defined functions of 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.
2.2 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. Phase
The shared object file should be suffixed with. So. For convenience, this concept is not specifically distinguished in this article. /Lib in Linux and/usr/x11r6 in the standard graphic interface
/Lib and other directories, there are many shared objects ending with so. Similarly, in Linux, there is also a static function library call method, and the corresponding suffix ends with.. Linux uses this shared pair

Technology to facilitate sharing between programs, save space occupied by programs, and increase program scalability and flexibility. Linux also allows developers to use their own libraries through LD-PRELOAD Variables
To replace the system module.

Like Windows systems, it is easier to create and use dynamic libraries in Linux.
Hour
Add the-shared option, and the generated execution program is the dynamic link library. Generally, such a program is suffixed with so. During the Linux dynamic library program design process, the general process is to compile the user

The interface file is usually a. h file. Compile the actual function file with the suffix of. C or. cpp and then write the MAKEFILE file. This is not applicable to small dynamic library programs, but it is designed in this way.
Make the program more reasonable.

After the dynamic Connection Library is compiled and generated, it can be called in the program. In Linux, multiple call methods can be used.
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
Linux calls functions frequently used by the dynamic library, but when using the dynamic library, the source program must contain the dlfcn. h header file, which defines the prototype of the function that calls 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 way to call dynamic libraries and also facilitates the calling of other programs.

The implicit link of the system is similar. 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. So. conf of the dynamic link library to make the dynamic link library used by the system.

Append the name 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. 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
Using GCC? L or-l options 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.

3. Comparison and Analysis of Dynamic libraries of the two systems

Windows and Linux use the dynamic link library technology for the same purpose, but because of the different operating systems, they are still different in many aspects. The following describes the following aspects.

 

(1) Write a dynamic library program. The execution file format in Windows is PE. The dynamic library requires a dllmain function as the initialization population. Generally, the declaration of the export Function

The _ declspec (dllexport) keyword is required. The GCC-compiled execution file in Linux is in ELF format by default, and does not require initialization or function features.
Do not declare it. It is easier to compile it.

(2) dynamic library compilation. in windows, there is a convenient debugging and compilation environment. You do not need to compile it yourself.
Makefile files, but in Linux, you need to write makefile files by yourself. Therefore, you must master certain makefile compiling skills.
Linux compilation rules are relatively strict.

(3) For dynamic library calling, both Windows and Linux can use explicit or implicit calling for the dynamic library, but the specific calling methods are also different.

 

(4) view the dynamic library output function. In Windows, there are many tools and software that can view the functions output in the DLL, such as command line dumpbin and VC tools.
The depends program in. In Linux, nm is usually used to view the output function. You can also use LDD to view the shared object files implicitly linked by the program.

(5) dependency on the operating system. The two dynamic libraries depend on their respective operating systems and cannot be used across platforms. Therefore, dynamic libraries that implement the same functions must provide different dynamic library versions for two different operating systems.


4. Dynamic library migration method

 

If you want to compile a dynamic link library that can be used in both systems, the initial development is usually completed in the debugging environment provided by Windows VC. After all, the graphical editing and

The debugging interface is much more convenient than vi and GCC. After the test is completed, the dynamic library program is transplanted. Generally, the default GCC compilation rules are stricter than the default VC compilation rules, even if there is no
The Program for warning errors may also encounter many warning errors during GCC debugging. You can use the-W option in GCC to disable the warning error.

The following describes the rules and experiences to be followed for program porting.

 
(1) Try not to change the sequence of original dynamic library header files. Usually in C/C, the sequence of header files is quite related. In addition, although C/C is case sensitive
Linux must be the same as the header file, because the ext2 file system is case sensitive to the file name; otherwise, the file cannot be compiled correctly. In Windows, the header file is case sensitive.
Compile.

(2) Unique header files of different systems. In Windows, the windows. h header file is usually included.
Number,
The Winsock.. h header file is included. Therefore, when porting to a Linux system, You Need To comment out the header files unique to these windows systems and constant definitions for some Windows systems.
Note: added header files supported by underlying communication for Linux.

(3) data type. Vc has many unique data types, such
_ Int16 ,__ int32, true, socket, etc. GCC compiler does not support them. The common practice is
The statements defined by the data are copied to a header file, and then the header file is included in Linux. For example, you can change the socket type to int.

(4) keywords. Vc has many keywords not used in standard C, such as bool, byte, DWORD, __asm, and so on. They are usually used as far as possible to facilitate transplantation, if it is unavoidable, you can use # ifdef and # endif to write two versions for Linux and Windows.
 

(5) modify the function prototype. Generally, if you use a standard dynamic library written in C/C language, you basically do not need to rewrite the function. However, because of the differences between the two systems, you need to change

Function calling methods, such as using the close () function to replace the closesocket () function in the Linux Network Communication dynamic library.
. In addition, there is no file handle in Linux. To open the file, you can use open and fopen functions. For the usage of these two functions, see [2].

  
(6) Write makefile. In Windows, the VC compiler is usually responsible for debugging, but GCC needs to write the MAKEFILE file by yourself. You can also refer
Makefile generated by VC. For dynamic library migration, you must add the-shared option when compiling the dynamic library. For programs that use mathematical functions, such as power series, when calling a dynamic library
Add-LM.

(7) other notes

① Program design structure analysis is an essential step for porting the dynamic library program compiled by another user. Generally, the dynamic library program does not contain operations such as interfaces, so it is relatively easy.
② In Linux, permissions on files or directories are divided into owners, groups, and others. Therefore, when accessing a file, pay attention to whether to perform read or write operations on the file. If you perform write operations on the file, pay attention to the permission to modify the file or directory. Otherwise, the file cannot be written.
 

③ When using a pointer, define a pointer to allocate only four bytes of memory to it. If you want to assign a value to the variable pointed to by the pointer, you must use the malloc function to allocate memory or define it as a pointer.
Is a variable, which is stricter than Windows compiling in Linux. Similarly, a structure cannot contain values in a function. If you want to transmit values in a structure in a function, you must define the structure in the function
Needle.
④ Path identifier, which is "/" in Linux and "\" in windows. Note that dynamic library search paths are different in Windows and Linux.
⑤ Programming and debugging skills. There are different debugging skills for different debugging environments, which are not described here.

5. Conclusion

 

This article systematically analyzes the implementation and usage of dynamic libraries in Windows and Linux, and comprehensively analyzes and compares the differences between the two call methods from programming, compilation, calling, and operating system dependencies.

In the same way, according to the actual program porting experience, the methods for porting the Windows dynamic library compiled by VC to Linux and the issues needing attention are given. At the same time, the program sample piece is given.
During the program porting process, due to the system design and other aspects, the precautions for porting may be far more complex than the above, this article provides some useful experiences and techniques for program porting of different operating systems.
Coincidentally.

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.