"Go" Analysis of Linux and Windows dynamic libraries

Source: Internet
Author: User

Original address: http://www.cnblogs.com/chio/archive/2008/11/13/1333119.html

Abstract: Dynamic link library technology implementation and design programs commonly used techniques in the Windows and Linux Department

System has the concept of dynamic library, the use of dynamic library can effectively reduce program size, save space, improve efficiency, increase the scalability of the program, easy to modular management. However, due to the different formats, dynamic libraries of different operating systems need to be migrated in a dynamic library program when different operating system calls are required. This paper analyzes and compares two operating system dynamic library technologies, and gives the method and experience of porting the dynamic libraries compiled by Visual C + + to Linux. Recommended creation of Linux dynamic libraries read: "Creation and use of Linux static/dynamic link Libraries"
1. Introduction
Dynamic Library (Abbr,dll) technology is a technique commonly used in programming. Its purpose is to reduce the size of the program, save space, improve efficiency, with high flexibility. Using dynamic library technology makes it easier to upgrade software versions. Unlike a static library, a function inside a dynamic library is not part of the execution program itself, but is loaded on demand as execution requires, and its execution code can be shared among multiple programs at the same time.
In the Windows and Linux operating systems, the software can be designed in this way, but they are called and programmed in different ways. In this paper, we first analyze the dynamic library invocation methods and programming methods commonly used in these two kinds of operating systems, then analyze and compare the differences between the two methods, finally, according to the experience of the actual transplant program, introduce the method of porting the Windows Dynamic Library of VC + + to Linux.
2. Dynamic Library Technology
2.1 Windows Dynamic Library Technology
Dynamic link Library is an important technical means to realize Windows Application sharing resources, saving memory space and improving usage efficiency. Common dynamic libraries contain external functions and resources, and some dynamic libraries contain only resources, such as Windows Font resource files, called resource dynamic link libraries. Typically, dynamic libraries are suffixed with. Dll,.drv,. fon, and so on. The corresponding Windows static library usually ends in. lib, and Windows itself implements some of the main system features in the form of dynamic library modules.
The Windows dynamic Library is loaded into the virtual space of the process at run time by the system, 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 the threads of the process. The handle to the 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 that are used to provide services to the outside world. A DLL can have its own data segment, but not its own stack, using the same stack pattern as the application that invokes it; a DLL has only one instance in memory; DLL is code encapsulation, DLL is programmed with specific programming language and compiler, and can be programmed by DLL. Any object (including variables) created by code in a DLL function is owned by the thread or process that called it.
Depending on the calling method, the call to the dynamic library can be divided into static call mode and dynamic invocation mode.
(1) Static calls, also known as implicit calls, are compiled by the system to load the DLLs and the DLLs unloaded at the end of the application (the Windows system is responsible for counting the number of DLL calls), and the invocation is simple enough to meet the usual requirements. The usual invocation method is to generate a dynamic connection library. LIB file is added to the application's project, and when you want to use a function in a DLL, you only need to declare it in the source file. LIB file contains the symbol name and optional identification number of each DLL export function and DLL file name, and does not contain the actual code. LIB file contains information into the generated application, and the called DLL file is loaded into memory while the application loads.
(2) Dynamic invocation, that is, explicit invocation mode, is used by programmers to load and unload DLLs with API functions to achieve the purpose of calling the DLL, more complex, but more efficient use of memory, is the important way to prepare large applications. In Windows systems, functions related to dynamic library invocation include:
①loadlibrary (or MFC's AfxLoadLibrary), load the dynamic library.
②getprocaddress, gets the function to be introduced to convert the symbol name or identification number to the DLL internal address.
③freelibrary (or MFC's AfxFreeLibrary), release the dynamic-link library.
Creating a dynamic library in Windows is also easy and easy. 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 the MFC class library. 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 two kinds of conventions for dynamic library output functions, which are based on the calling convention and the name Decoration Convention. DLL program defined functions are divided into internal functions and export functions, dynamic library exported functions for other program module calls. There are usually several ways to export a function:
The ① uses the export portion of the module definition file to specify the function or variable to be entered.
② uses the decorated symbol _declspec (dllexport) provided by MFC.
③ uses the/export command line to output the function in the command line mode.
In Windows dynamic libraries, it is sometimes necessary to write a module definition file (. DEF), which is a text file that consists of module statements that describe the properties of a DLL.
2.2 Linux Shared Object technology

In the Linux operating system, many shared object technologies are used, although it corresponds to a dynamic library in Windows, but it is not called a dynamic library. The corresponding shared object file is suffixed with. So, for convenience, in this article, the concept is not specifically differentiated. There are many shared objects that end with so in the/lib of the Linux system and the/usr/x11r6/lib of the standard graphical interface. Similarly, under Linux, there is also a static function library such a call method, the corresponding suffix with. A end. Linux uses this shared object technology to facilitate inter-program sharing, saving program space, and increasing the scalability and flexibility of the program. Linux can also use the Ld-preload variable 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 generated executable 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 user interface files, usually. h files, write the actual function file, with. C or. cpp suffix, and then write the makefile file. This is not the case for smaller, dynamic libraries, but the design makes the program more reasonable.
After compiling the dynamic connection library, you can make calls in the program. In Linux, you can use a variety of invocation methods, like the Windows System directory (.. "System32, etc.), you can copy the dynamic library files to the/lib directory or create a symbolic connection in the/lib directory for all users. The following describes the functions that Linux often uses to invoke dynamic libraries, but when using a dynamic library, 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);
The Dlopen is used to open the dynamic-link library of the specified name (filename) and returns an action handle.
(2) Take function execution address: Dlsym, function prototype: 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 operation handle (handle) and symbol.
(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, and only if the use count of this dynamic-link library is 0 o'clock is it actually uninstalled by the system.
(4) Dynamic library error function: Dlerror, function prototype: const char *dlerror (void); When the dynamic link library operation function fails, Dlerror can return an error message, and a return value of NULL indicates that the operation function executed successfully.
After taking the function execution address, it is possible to invoke the function in the dynamic library according to the function interface declaration provided by the dynamic Library in the usage program of the dynamic library. When writing a makefile file for a program that invokes a dynamic library, you need to include the compile options-rdynamic and-LDL.
In addition to writing and invoking dynamic libraries in this way, the Linux operating system provides a more convenient way to call dynamic libraries and facilitates other program calls, similar to the implicit linkage 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 method of invocation, you need to maintain the dynamic-link library's profile/etc/ld.so.conf to make the dynamic-link library available to the system, typically appending the directory name of the dynamic-link library to the dynamic-link library configuration file. As with the X Window Windows system release, the file has/usr/x11r6/lib, which points to the directory where the dynamic link library of the X Window System is located. To enable the dynamic link library to be shared with 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 with the –l or-l option or directly referencing the required dynamic-link library. Inside Linux, you can use the LDD command to check your program's dependencies on shared libraries.
3. Comparison and analysis of two system dynamic libraries

Windows and Linux use Dynamic link library technology to be basically consistent, but because of the different operating systems, they are not the same in many ways, the following aspects are elaborated.
(1) The dynamic Library program is written in the Windows system executable file format is the PE format, the dynamic library needs a DllMain function as the initialized population, usually in the declaration of the exported function need to have the _declspec (dllexport) keyword. The execution file of GCC compiled under Linux is the ELF format by default, does not need to initialize the entrance, and does not need to make special declaration to the function, writing is more convenient.
(2) Dynamic Library compilation, under the Windows system, there is a convenient debugging and compiling environment, usually do not have to write makefile files themselves, but under Linux, you need to write your own makefile files, therefore, must master a certain makefile writing skills, in addition, Generally, Linux compilation rules are relatively strict.
(3) For dynamic library invocation, both Windows and Linux can make explicit or implicit calls to the dynamic libraries under which they are made, but the specific invocation methods are 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 the command line dumpbin and VC + + tools in the depends program. In Linux systems, the NM is often used to view the output function, or you can use LDD to view the shared object files that are implicitly linked by the program.
(5) Depending on the operating system, these two dynamic libraries run on their own operating systems and cannot be used across platforms. Therefore, for dynamic libraries that implement the same functionality, different versions of the dynamic libraries must be available 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, you will typically choose to complete the initial development in the debug environment provided by VC + + in Windows, after all, VC + + provides a graphical editing and debugging interface more convenient than VI and GCC. After the test is completed, the program of the dynamic library is migrated. Usually gcc default compiler rule is stricter than VC + + default compiler rules, even if there is no warning error under VC + + program in the GCC debugging will also appear many warning errors, you can use the-w option in GCC to turn off warning errors.
Here are the rules and experiences that you need to follow to transplant a program.
(1) Try not to change the original dynamic library header file order. Typically, the order of the header files is quite related in the C + + language. In addition, although the C + + language is case sensitive, Linux must be the same as the header file when it contains the header file, because the Ext2 file system is case-sensitive to the file name, otherwise it will not compile correctly, and the header file case can be compiled correctly under Windows.
(2) header files unique to different systems. In Windows systems, the Windows.h header file is typically included, and Winsock is included if the underlying communication function is called. h header file. So when porting to a Linux system, comment out the header files that are unique to these Windows systems, as well as the constant definitions of some Windows systems, and add the supported header files for Linux to the underlying communication.
(3) data type. VC + + has many unique data types, such as __int16,__int32,true,socket, and GCC compilers do not support them. It is common practice to copy the statements 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 socket int.
(4) keywords. VC + + has many standard C in the keyword not used, such as bool,byte,dword,__asm, etc., usually in order to facilitate the transplant, try not to use them, if it is unavoidable can be used #ifdef and #endif for Linux and Windows to write two versions.
(5) Modification of function prototypes. In general, if you write a dynamic library in the standard C + + language, you basically do not have to rewrite the function, but for the system call function, because of the difference between the two systems, need to change the function call mode, such as in the Linux network communication Dynamic Library, with close () function to close 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 can be opened and fopen functions, the use of these two functions can be referenced in reference to the literature [2].
(6) Preparation of makefile. Under Windows is usually the VC + + compiler is responsible for debugging, but GCC need to write makefile files themselves, you can also refer to VC + + generated makefile files. For dynamic library porting, the-shared option is required to compile the dynamic library. For a program that takes a mathematical function, such as a power series, to invoke a dynamic library is to add-LM.
(7) Other areas to be aware of
① programming structure analysis, for the transplant of the human written dynamic library program, program structure analysis is an essential step, usually in the dynamic library program, does not include interface and other operations, so relatively easy.
② in Linux, the permissions on files or directories are divided into owners, groups, and others. So when accessing the file, pay attention to whether the file is read or write operations, if the file is written, you should pay attention to modify the file or directory permissions, or you cannot write to the file.
③ pointer usage, 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 under Linux than Windows compiled strict. The same structure cannot pass the value in the function, if the function is to pass the value of the structure, the structure of the function must be defined as the structure pointer.
④ path identifier, under Linux is "/", under Windows is "" ", note that Windows and Linux are different for dynamic library search paths.
⑤ programming and debugging techniques. There are different debugging techniques for different debugging environments, which are not described in more detail here.
5. Concluding remarks
This paper systematically analyzes the implementation and usage of Windows and Linux dynamic libraries, and compares the differences between these two methods from the aspects of program writing, compiling, calling and the dependence of operating system, and gives the VC + + in accordance with the actual program transplant experience. The development of Windows Dynamic Library to Linux under the method and the 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 migration, due to the design of the system and other aspects, may be transplanted to be more important than the above complex, In this paper, through summing up and then for the different operating System program porting provides a deliberate experience and skills.

"Go" Analysis of Linux and Windows dynamic libraries

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.