This article introduces the basic concepts and main advantages of the shared object library (SO) on the Linux platform in this paper. Apply SO in Delphi for Linux to apply the DLL of DLL in Delphi for Windows, in the programming instance, Linux The consisting of the SO library file of the platform, the function of the SO library file, special compilation instructions, use Delphi for Linux to create SO programming rules, use the previous Linux system settings, and use implicit or explicit links in Delphi for Linux Methods and use the basic methods, experiences and techniques of SO functions, and explore and analyze the problems that can appear in the application SO. Shared Object Library Basic Concept DELPHI for Linux is a Linux platform-oriented visual development tool, which is Borland, is a good application development tool on the Linux platform. Delphi for Linux also known as Kylix. When you develop Linux applications with Kylix, you may use a large number of SO files that have been used in the Linux operating system itself. SO is a special run file that contains several methods, objects, and resources, which cannot be run directly, but can be dynamically called by Kylix applications or other executable files. The SO file extension is. SO, compile the front source file extension as .dpr. Examples of this article are quo compiled in Red Hat Linux 7.3 and Kylix 3.0, and can run normally. Figure 1 is a graphical diagram of the hierarchical relationship between the Kylix main program and the SO library. It can be seen from the SO library and there are several advantages.
Figure 1 Hierarchical relationship diagram of Kylix main program and SO library
◆ Multiple Kylix programs or multiple unit files can share a SO library file through the interface. On the other hand, a Kylix program can use multiple SO library files through multiple interfaces. In this way, SO becomes a shared resource that realizes real "resource sharing", which greatly narrows the execution code of the Kylix application, enhancing the reusability of software. ◆ When designing the SO file as a public call module of the Kylix application, only the SO library file and compile SO are required for their independent application, software upgrades, without changing and recompiling the Kylix application main program. ◆ Not only can Kylix written SO libraries, but also use common languages such as C or C , as long as the specific interface specification is followed. The creation of the shared object library 1. The structure of the SO library file and the internal structure of the SO library file and the Kylix standard unit file are basically the same, and there is also a declaration, implementation, and initialization. One of the differences between the SO library is only the method (including functions and procedures) collections that can be called by other programs. The second library program is bound by the library keyword instead of Project. The project file is started; the library is included with an Exports statement that lists the export functions and procedures to be provided to the outside. The following is a simple example of the SO library file code to explain it.
Library myfirstso;
Uses
Sysutils, Classes; {Delphi for Windows Reference Class Library for Windows}
Function add (A: char; b: char): integer; cdecl; overload;
Begin
Result: = ORD (A) ORD (B);
END;
Function add (A: Integer; B: Integer): Integer; cdecl; overload;
Begin
Result: = a b;
END;
Function Double (N: Integer): Integer; cdeclbegin
Result: = n * 2;
END;
Exports
Add (A: Integer; B: Integer),
Add (A: char; b: char) name 'addchar',
Double;
2. The function overloaded SO library in the SO library file can also use the overload function (ie, multiple functions use the same name, different parameters). Use the overloaded function to declare the Overload instruction. Kylix can export an overload function with the original name, indicating its parameter table in the Exports clause. To export multiple overload functions, you must specify a different name in the Exports clause to distinguish it to distinguish it. This can be seen from the above example myfirstso, and the add is a heavy-duty function, distinguishes to the call, and one with the original function declaration Add export, the other with addchar exported. 3. The special compilation of the SO library compiles compiled SO libraries run files using the lib prefix and .so extension. Considering the actual naming rules and version and support symbolic chains, Kylix introduced several special compilation instructions in the Object Pascal language, which does not make sense in Delphi. The execution file generated after the library source file Myfirstso.dpr is libmyfirstso.so. ◆ $ SOPREFIX changes the name prefix, default is lib (normal library) or BPL (Kylix package). Different libraries are used by the prefix because Linux libraries are single extension (.so). ◆ $ SOSUFFIX adds text, specify versions or other information between library names and extensions. ◆ $ SOVERSION increases the version number after the extension. ◆ $ Soname indicates the relevant symbolic name and is automatically generated by the compiler. For example, the following code generates library libsimple.so.2.0.1 and symbolic link libsimple.so.2.
Library Simple;
Uses
Sysutils, Classes;
// Function definition 省 省 省
{$ SOVERSION '2.0.1'}
{$ SONAME 'LIBSIMPLE.SO.2'}
When using the Kylix application using the SO library, use the Kylix application: one is implicit linking, also known as static loading; the other is explicit linking, also known as Dynamic loading. The following is the technology of the use of these two links, and the technique of adding form objects into the SO library. 1. After using the system settings to customize the SO library, the Kylix application is called when the Kylix application is called because Kylix can't find a new library, and the system must be related to the system. This is different from the DLL library in Delphi for Windows, and the DLL library is only available to the compiled DLL file in the Delphi main program. The steps are as follows: ◆ Put the compiled SO library file in Linux system library / lib or / usr / lib, or add the path where the custom SO library file is added to the Linux system library path shell variable ld_library_path. ◆ Under root users (root), refresh the library buffer with the ldconfig command. ◆ Use the LDD command to Kylix execution file to see the SO library associated with the program. 2. Implicit link Implicit link refers to loading the SO library file into the application when the application starts execution. Implementing implicit links is not difficult, just add the declaration statement of the library function in the application and the external definition clause of the library, the library function can be used as the general partial function. For example, to use the Add function in libmyfirstso.so, just add the following statement in the application: Function Add (A: Integer; B: Integer): integer; cdecl; external 'libmyfirstso.so'; 3. Explicit link Explicit link is that the application can load SO library files at any time during execution, or you can uninstall the SO library file at any time and switch to SO library at runtime. These are implicit links that cannot be done. Explicit links have greater flexibility compared to implicit links. In Kylix, you want to dynamically load the library and call the export function to use the Delphi simulation code or the natural Linux method. Both methods are introduced below. (1) Dynamically loaded with Delphi simulation code to dynamically load the DLL in Windows is done with the SAFELOADLibrary function provided by Windows API functions - LoadLibrary or Delphi. After finding the library, the program calls the Windows API function-GetProcAddress search DLL export function. If you find a match, return the requested function pointer and convert this function pointer into an appropriate type and call. Call freeELibrary after use, release the library from the memory. Use the Pascal RTL simulation function in Kylix to implement SO library dynamic loading. The following example only lists the related parts of the dynamic link in the Kylix application, not the full Kylix unit file code. Unit Dynaform;
Interface
Uses
SYSUTILS, CLASS, QCONTROLS, QFORMS
Type
TFORM1 = Class (TFORM)
Button1: tbutton;
Procedure Button1Click (Sender: TOBJECT);
END;
Var Form1: TFORM1;
IMPLEMENTATION
{$ R * .xfm}
TYPE TComputeInteger = function (x: integer; y: integer): integer; cdecl; // call library function interface type definition
Procedure TFORM1.BUTTON1CLICK (Sender: TOBJECT);
Var handle: thandle;
Compute: Tcomputeinteger;
Begin
Handle: = loadingLibrary ('libmyfirststso.so'); // Dynamic loading library
IF Handle <> 0 Then // Find library
Begin
Compute: = tComputeinteger (GetProcadDress (Handle, 'Add');
/ / Search Library Function Add and return function pointer
If Assigned (Compute) THEN
ShowMessage (INTTOSTR (Compute (10, 20)); // Use library functions
Freelibrary (Handle); // Release Base
end
Else
ShowMessage ('Library Not Found');
END;
(2) Dynamic loading with Linux natural code can also use the low-level Linux functions in the libc system unit, which can use more parameters to better control the system. The Linux functions used are DLOPEN (open and loaded), DLSYM (Search Library Functions), DLCLOSE (release library). Therefore, the code of the curved library in the above example becomes:
Procedure TFORM1.BUTTON1CLICK (Sender: TOBJECT);
Var Handle: Pointer;
Compute: Tcomputeinteger;
Begin
Handle: = DLOPEN ('libmyfirststso.so'); // Dynamic loading library
IF Handle <> nil dam find library
Begin
Compute: = Tcomputeinteger (DLSYM (Handle, 'Add');
/ / Search Library Function Add and return function pointer
If Assigned (Compute) THEN
ShowMessage (INTTOSTR (Compute (10, 20)); // Use library functions
DLClose (Handle); // Release Base
end
Else
ShowMessage ('Library Not Found');
END;
(3) In addition to the library containing functions and procedures, the SO library is included in the shared object, in addition to the library containing functions and procedures, which can be a dialog or other form. After generating a new library object, as long as the reference to the form unit file is added to the declaration section of the library source file, then the export function of the form is generated and used in the form unit file. The following example implements the Kylix main program to activate the Modal dialog box by calling the SO library form processing function to select the color and update the application main form color. The steps are as follows: ◆ Create a form unit file scrollf with a specific function, form the form object for FormScroll. The following code is only used for instructions, not a complete program.
UNIT SCROLLF;
Interface
Uses
SYSUTILS, CLASS, QCONTROLS, QFORMS; TYPE
TFORMSCROLL = Class (TFORM) // Object and method definition 省 省 省
END;
Var Formscroll: TFormscroll;
◆ The implementation of the form unit file Scrollf is written using the export function GetColor using the form Formscroll. Its function is to activate the dialog object Formroll to select the color and return the color value. code show as below:
Function getColor (Col: longint): longint; cdecl;
VAR
Formscroll: tFormscroll;
Begin
Result: = COL; / / Function Returns the default value
Try
Formscroll: = tFormscroll.create (Application);
Try
Formscroll.selectedColor: = COL; // Initialization Color
If Formscroll.ShowModal = MROK THEN / / Display dialog
Result: = formscroll.selectedColor; // Return color value
Finally
Formscroll.free;
END;
Except
ON E: Exception DO
Messagedlg ('Error In Formdll:' E.MESSAGE, MTERROR, [MBOK], 0);
END;
END;
◆ In the defined section of the Form file Scrollf, add the declaration of the export function getColor. code show as below:
Function getColor (Col: longint): longint; cdecl;
◆ Increase the reference to the form unit scrollf in the defined section of the library source file FormSo.dpr. code show as below:
Library Formso;
Uses
Scrollf in 'scrollf.pas' {formscroll};
Exports
GetColor;
End.
◆ Compile library fileso.dpr, generate SO library execution file libformso.so. Now you can invoke the form class function getColor in the library libformso.so in the Kylix application in the Kylix application. Issues should pay attention to the benefits of SO libraries to developers, but due to their differences from general Kylix programs, if it is used as the latter, it may bring some problems. Several questions that may occur in use are listed below: (1) A large number of references to the CLX graphic class library in the SO library will bring adverse effects on the SO library. (2) If the library and the main execution file are not run, two copies of the CLX code and data are obtained. For example, it is possible to get two different global Application objects, and the Application object in the library cannot initialize correctly. (3) If the library needs to call the CLX graphic or CLX control class object, it is recommended to use a package (Package) - a special shared library; if the library is dominated by non-graphics, if the numerical calculation It is more convenient to use SO libraries. Mastering and proficiently using shared object library technology will help to develop more features, better reuse, and more flexible applications. In this paper, the functions and methods of use of shared object library (SO) are introduced by comparison of SO and DLL. I hope that by this article, the reader can develop Linux-based applications using the SO library created.