Analyzing Windows and Linux dynamic libraries _unix Linux

Source: Internet
Author: User
Tags constant definition function prototype mathematical functions

Absrtact: Dynamic link library technology implementation and design procedures commonly used in Windows and Linux systems have the concept of dynamic library, the use of dynamic library can effectively reduce the size of the program, save space, improve efficiency, increase the scalability of the program, easy to modular management. However, dynamic libraries of different operating systems need to be ported by Dynamic Library because of different format and different operating system calls. This paper analyzes and compares two kinds of dynamic library technology, and gives the method and experience of porting the dynamic library compiled by Visual C + + to Linux.

1. Introduction

Dynamic Link Library Abbr,dll technology is a technique commonly used in program design. Its purpose is to reduce the size of the program, save space, improve efficiency, with high flexibility. Using dynamic Library technology is easier for upgrading software versions. Unlike a static library, the functions inside a dynamic library are not part of the execution program itself, but are loaded on demand according to execution, and their execution code can be shared across multiple programs at the same time.

In both Windows and Linux operating systems, software design can be done in this way, but they are not invoked as well as programmed in a different way. Firstly, this paper analyzes the dynamic library invocation method and the programming way in these two operating systems, then analyzes and compares the differences between the two methods, and finally introduces the method of porting the Windows dynamic Library compiled by VC + + to Linux based on the experience of the actual porting program.

2, Dynamic Library technology

2.1 Windows Dynamic Library Technology

Dynamic link Library is an important technical means to realize shared resources, save memory space and improve efficiency of Windows applications. Common dynamic libraries contain external functions and resources, and some dynamic libraries contain only resources, such as Windows Font resource files, which are called resource dynamic link libraries. Dynamic libraries are usually suffixes of. Dll,.drv,. fon, and so on. The corresponding Windows static libraries usually end with. lib, and Windows itself implements some of the major system functions as dynamic library modules.

Windows dynamic libraries are loaded into the process's virtual space at run time, using memory allocated from the virtual address space of the calling process, as part of the calling process. DLLs can also be accessed only by threads of that process. The handle of a DLL can be used by the calling process, and the handle to the calling process can be used by the DLL. DLL modules contain various export functions to provide services to the outside world. DLLs can have their own data segments, but does not have its own stack, using the same stack pattern as the application that invoked it; the DLL implements code encapsulation, and DLL is independent of the specific programming language and compiler, and can be implemented by a DLL for mixed language programming. Any object (including variables) created by code in a DLL function is owned by the thread or process that calls it.

Depending on the mode of invocation, calls to the dynamic library can be divided into static and dynamic invocation methods.

(1) Static calls, also known as implicit invocations, are coded by the compilation system to load the DLL and unload the DLL at the end of the application (the Windows system is responsible for counting the number of DLL calls), simple to invoke, and able to meet the usual requirements. Typically, the invocation is generated when a dynamically connected library is generated. Lib files are added to the project of the application, and when you want to use the functions in the DLL, you only need to declare them in the source file. The Lib file contains the symbol name and selectable identification number of each DLL export function, as well as the DLL file name, and does not contain actual code. The Lib file contains information that goes into the generated application, and the invoked DLL file is loaded into memory at the same time as the application loads.

(2) Dynamic invocation, that is, the explicit invocation method, the programmer uses API functions to load and unload DLLs to achieve the purpose of calling the DLL, more complex, but more efficient use of memory, is the important way to compile large applications. In Windows systems, functions related to dynamic library calls include:

①loadlibrary (or MFC's AfxLoadLibrary) to load dynamic libraries.
②getprocaddress, gets the function to introduce, converts the symbol name or identification number to the internal address of the DLL.
③freelibrary (or MFC's AfxFreeLibrary) to release the dynamic link library.

Creating dynamic libraries in Windows is also convenient and simple. In Visual C + +, you can create DLL programs that are written directly in C without MFC, or you can create DLL programs that are based on MFC class libraries. Each DLL must have an entry point, in VC + +, DllMain is a default entry function. DllMain is responsible for initializing (initialization) and ending (termination) work. There are also two kinds of conventions for dynamic library output functions, which are based on the calling convention and the name Modification Convention. DLL program defined functions are divided into internal functions and export functions, dynamic library exported functions for other program modules to call. There are usually several ways to export a function:

① Specifies the function or variable to be entered using the Export section of the module definition file.
② uses the decorated symbol _declspec (dllexport) provided by MFC.
③ on the command-line, using the/export command line to output related functions.

In Windows dynamic libraries, it is sometimes necessary to write a module definition file (. DEF), which is a text file composed of module statements that describe DLL properties.

2.2 Linux Shared Object technology


In the Linux operating system, a lot of shared object technology is used, although it corresponds to a dynamic library in Windows, but it is not called a dynamic library. The corresponding shared object file has a. So as the suffix, for convenience, in this article, the concept is not specifically differentiated. The/lib of Linux systems and the/usr/x11r6/lib of standard graphical interfaces have many shared objects that end with so. Similarly, in Linux, there is also a static function library this way of invocation, the corresponding suffix to. A end. Linux uses this shared object technology to facilitate sharing between programs, save program space, and increase program scalability and flexibility. Linux can also use Ld-preload variables to allow developers to replace system modules with modules in their own libraries.

As with Windows systems, it is easy to create and use dynamic libraries in Linux, and add the-shared option when compiling the library source program so that the resulting execution program is the dynamic link library. Usually such a program with so suffix, in the Linux dynamic library programming process, usually the process is to write the user's interface file, usually the. h file, write the actual function file, with. C or. cpp suffix, and then write the makefile file. This may not be the case for smaller dynamic library programs, but this design makes the program more reasonable.

After the build of the dynamic connection library, it can then be invoked in the program. In Linux, you can use a variety of calling methods, with Windows System directory (... \system32, etc.), you can copy a dynamic library file to the/lib directory or establish a symbolic connection within the/lib directory for all users. The following describes the functions often used by Linux to invoke dynamic libraries, but when working with dynamic libraries, the source program must contain a Dlfcn.h header file that defines the prototype of the function that invokes the dynamic link library.

(1) _ Open Dynamic link library: Dlopen, function prototype void *dlopen (const char *filename, int flag);
Dlopen is used to open a dynamic-link library with the specified name (filename) and returns an action handle.

(2) Take function address: Dlsym, function prototype is: void *dlsym (void *handle, char *symbol);
Dlsym returns the execution code address of the function corresponding to the symbol, based on the dynamic Link library action handle (handle) and symbol (symbol).

(3) Turn off 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, which is actually uninstalled only if the use count of this dynamic link library is 0 o'clock.

(4) Dynamic library error function: Dlerror, the function prototype is: const char *dlerror (void); When a dynamic-link library operation function fails, Dlerror can return an error message, and the return value is null to indicate that the operation function was executed successfully.

After taking the function execution address, we can invoke the function in the dynamic library according to the function interface declaration provided by the dynamic library in the dynamic library's use program. When writing a makefile file for a program that calls a dynamic library, you need to add the compilation options-rdynamic and-LDL.

In addition to writing and invoking dynamic libraries in this way, the Linux operating system provides a more convenient way of dynamic library invocation and other program calls, similar to the implicit links of Windows systems. Its dynamic library is named "Lib*.so.*". In this naming method, the first * represents the library name of the dynamic-link library, and the second * usually represents the version number of the dynamic library, or it can have no version number. In this invocation, you need to maintain the dynamic-link library's profile/etc/ld.so.conf to allow the dynamic-link library to be used by the system, typically appending the directory name of the dynamic-link library to the dynamic-link library configuration file. If you have an X Window Window System release that has/usr/x11r6/lib in the file, it points to the directory where the dynamic link library of the X Window Window system resides. In order for the dynamic link library to be shared by the system, you also need to run the management commands for the dynamic link library./sbin/ldconfig. When compiling a referenced dynamic library, you can compile it in GCC using the –l or-l option or the dynamic link library that is required for direct reference. Inside Linux, you can use the LDD command to check that the program relies on shared libraries.

3. Comparison and analysis of two kinds of system dynamic libraries


Windows and Linux use Dynamic link library technology The purpose is basically consistent, but because the operating system is different, they in many ways still not the same, below from the following several aspects to explain.

(1) Dynamic Library program writing, in the Windows system executable file format is the PE format, dynamic library needs a DllMain function as the initialization of the population, usually in the declaration of the function of the need to have _declspec (dllexport) keyword. The implementation of GCC compiled by Linux defaults to the ELF format, does not require initialization of the portal, and do not need to make special statements to the function, writing more convenient.

(2) Dynamic Library compilation, under the Windows system, there is a convenient debugging and compiling environment, usually do not have to write their own makefile files, but under Linux, you need to write their own makefile files, therefore, must master a certain makefile writing skills, in addition, Linux compilation rules are usually relatively strict.

(3) Dynamic library invocation, Windows and Linux for its dynamic library can be made explicit call or implicit invocation, but the specific way of invocation is different.

(4) Dynamic library output function view, in Windows, there are many tools and software can be viewed in the DLL output functions, such as command-line dumpbin and VC + + tools in the depends program. NM is typically used in Linux systems to view output functions, or shared object files that are implicitly linked by using the LDD Viewer.

(5) reliance on the operating system, these two dynamic library operations rely on their own operating system, can not be used across platforms. Therefore, for dynamic libraries that implement the same functionality, you must provide a different version of the dynamic library for two different operating systems.

4. Dynamic Library Porting method


If you want to compile a dynamic link library that can be used in both systems, you will usually first choose to complete the initial development in the debugging environment provided by VC + +, after all, the graphical editing and debugging interface provided by VC + + is much more convenient than VI and GCC. After the test is completed, the dynamic Library program is ported. Usually GCC default compilation rules than VC + + default compilation rules strict, even in VC + + without any warning error program in GCC debugging will also appear many warning errors, you can use the-w option in GCC shutdown warning error.

The following are the rules and experiences to follow for a program transplant.

(1) Try not to change the original dynamic library header file order. Typically, the order of header files is quite related in C + + language. In addition, although the C + + language is case-sensitive, Linux must be the same case as the header file when the header file is included, because the Ext2 file system does not compile correctly because the file name is case sensitive, and under Windows, the header file case can be compiled correctly.

(2) header files unique to different systems. In Windows systems, the Windows.h header file is typically included, and if the underlying communication function is invoked, the Winsock is included. h header file. Therefore, when porting to Linux systems, you should annotate these Windows system unique header files and some of the Windows system's constant definition instructions, to increase Linux low-level communication support header files.

(3) data type. VC + + has a number of unique data types, such as __int16,__int32,true,socket, the GCC compiler does not support them. It is common practice to copy statements that are defined in Windows.h and Basetypes.h to a header file, and then include the header file in Linux. For example, change the socket type to an int.

(4) keywords. VC + + has many of the standard C not adopted keywords, such as bool,byte,dword,__asm, and so on, usually in order to transplant convenience, as far as possible not to use them, if you can not avoid using #ifdef and #endif for Linux and Windows to write two versions.
(5) Modification of the function prototype. Usually, if the use of the standard C/E + + language of the dynamic library, basically do not need to rewrite the function, but for system call function, due to the difference between the two systems, need to change the function of the call mode, such as in the Linux network communication Dynamic Library, with close () function to turn off the socket by replacing the closesocket () function under the Windows operating system. In addition, there is no file handle under Linux, to open the file available open and fopen functions, the use of these two functions can be referred to the reference [2].

(6) the preparation of makefile. Under Windows usually by VC + + compiler to be responsible for debugging, but GCC need to write their own makefile files, can also refer to VC + + generated makefile files. For dynamic library porting, you need to add the-shared option when compiling a dynamic library. For the use of mathematical functions, such as power series procedures, in the invocation of dynamic library is required to join the-LM.

(7) Other places to be noted

① programming structure analysis, for the transplant of the Dynamic Library program, program structure analysis is an essential step, usually in the dynamic library program, does not include interfaces, and so on, so relatively easy.
② in Linux, permissions for files or directories are divided into owners, groups, and others. Therefore, when accessing files, pay attention to the file is read or write operation, if it is to write to the file, pay attention to modify the file or directory permissions, otherwise can not write the file.
③ the use of pointers, define a pointer to allocate only four bytes of memory, if you want to assign a value to the variable pointed to by the pointer, you must use the MALLOC function to allocate memory for it or not to define it as a pointer to a variable, which is more stringent than Windows compiled under Linux. The same structure cannot pass a value in a function, and if you want to structure the value in a function, you must define the structure in the function as a structure pointer.
④ path identifier, under Linux is "/", under Windows is "\", note that Windows and Linux on the dynamic Library search path is different.
⑤ programming and debugging techniques. There are different debugging techniques for different debugging environments, which are not described here.

5. Concluding remarks

This paper systematically analyzes the implementation and usage of Windows and Linux dynamic Library, and compares the differences between the two calling modes from program compiling, compiling, calling and operating system dependency, and gives the VC + + based on the experience of the actual program Windows dynamic Library to migrate to Linux under the method and need to pay attention to the problem, at the same time, and give a sample of the program, in fact, in the process of program porting, due to the design of the system and so on, may be transplanted in the aspects of need to pay attention to more complex than above In this paper, the author provides the intentional experience and skill for the transplant of different operating system programs by summarizing and generalizing.

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.