As we all know, there are a lot of Dynamic Link Libraries in Windows systems (Files suffixed with. dll, DLL is dynamic link library ). Unlike the static function library, the dynamic link library contains functions that are not part of the execution program, but loaded as needed according to the execution program, at the same time, the Execution Code can be shared among multiple execution programs, saving space, improving efficiency, and having high flexibility, which is favored by more and more programmers and users. Is there such a function library in Linux? The answer is yes. There are not only dynamic link libraries in Linux, but also a large number of dynamic link libraries. In the/lib directory, there are many files suffixed with. So. This is the dynamic link library of Linux system applications, but unlike windows, it is called so, that is, shared
Object: shared object. (In Linux, the static function library is. A suffix) X-WINDOW as a standard Linux graphics window interface, it itself uses a lot of dynamic link library (in the/usr/x11r6/lib directory ), to facilitate sharing between programs and save space. The famous Apache Web server also uses a dynamic link library to expand program functions. You only need to copy the PHP dynamic link library to its shared directory and modify the configuration so that Apache can support PHP Web pages. If you want to, you can write your own dynamic link library so that Apache can support your custom webpage format. This is the benefit of dynamic links.
1. Create a dynamic link library in Linux
In Linux, creating a dynamic link library is a simple task. You only need to add the-shared option when compiling the function library source program, so that the generated execution program is a dynamic link library. In a sense, the dynamic link library is also an execution program. According to general rules, the program name should be suffixed with. So. The following is an example.
I want to write two functions, one for querying the current date getdate, the other for querying the current time gettime, and saving these two functions in the dynamic link library my. So. To do this, you need to do the following work.
1.1 compile the user interface file datetime. h with the following content (the number before each line is the row number ):
----------------------------------------------------------------------
1/* datetime. h: Written by Yu Yiqi in the vertical and horizontal software production center, 2001-06-28 .*/
2
3 # ifndef _ datetime_h
4
5 # DEFINE _ datetime_h
6
7/* Date structure */
8 typedef struct
9 {
10 int year;
11 int mon;
12 INT Day;
13} datetype;
14
15/* Time Structure */
16 typedef struct
17 {
18 char hour;
19 char min;
20 char sec;
21} timetype;
22
23/* function prototype Description */
24
25 # ifdef shared
26 int (* getdate) (datetype * D );
27 # else
28 int getdate (datetype * D );
29 # endif
30
31 # ifdef shared
32 int (* gettime) (timetype * t );
33 # else
34 int gettime (timetype * t );
35 # endif
36
37 # endif
38
----------------------------------------------------------------------
In this user interface file, the date and time structures are defined first, and then the prototype of the function is defined. The prototype of a dynamic function is different from that of a static function. A dynamic function should be in the form of a (* function name) to reference its pointer. To reference the dynamic function description in the file, you should define the shared macro to use it.
1.2 compile getdate. C. The source code is as follows:
----------------------------------------------------------------------
1/* getdate. C: Written by Yu Yiqi in the vertical and horizontal software production center, 2001-06-28 .*/
2
3 # include "time. H"
4 # include "datetime. H"
5
6 int getdate (datetype * D)
7 {
8 long ti;
9 struct TM * TM;
10
11 time (& Ti );
12 TM = localtime (& Ti );
13 d-> year = TM-> tm_year + 1900;
14 d-> MON = TM-> tm_mon + 1;
15 d-> day = TM-> tm_mday;
16}
17
----------------------------------------------------------------------
In the getdate function, call time to obtain the system time in seconds, then use the localtime function to convert the time structure, and finally adjust the date correctly.
1.3 compile gettime. C. The source code is as follows:
----------------------------------------------------------------------
1/* gettime. C: compiled by Yu Yiqi, the vertical and horizontal software production center, 2001-06-28 .*/
2
3 # include "time. H"
4 # include "datetime. H"
5
6 int gettime (timetype * t)
7 {
8 long ti;
9 struct TM * TM;
10
11 time (& Ti );
12 TM = localtime (& Ti );
13 T-> hour = TM-> tm_hour;
14 t-> min = TM-> tm_min;
15 t-> sec = TM-> tm_sec;
16}
17
----------------------------------------------------------------------
The gettime function is similar to the getdate function. First, use the time function to obtain the system time in seconds, then use the localtime function to convert the time structure, and finally return the current time (no adjustment is required ).
1.4 compile the Maintenance File makefile-lib with the following content:
----------------------------------------------------------------------
1 # makefile-Lib: Prepared by Yu Yiqi, the vertical and horizontal software production center, 2001-06-28.
2
3 All: My. So
4
5 src = getdate. c gettime. c
6
7 TGT = $ (SRC:. c =. O)
8
9 $ (SRC): datetime. h
10 @ touch $ @
11
12%. O: %. c
13 CC-C $?
14
15 # dynamic function library (my. So) generation
16 My. So: $ (TGT)
17 CC-shared-o $ @ $ (TGT)
18
----------------------------------------------------------------------
The purpose of writing maintenance documents is to facilitate programmers to maintain programs, especially to maintain large engineering projects. A good programmer should learn to write and maintain the makefile skillfully. After the dependency between files is defined, once the source file changes, you only need to make it. The maintenance code of the target file will be automatically executed, so as to automatically update the target file, which reduces the workload. Note: Each line of maintenance code must start with a tab (skip key). If it is not, make will fail.
Line 3 of this maintenance file is a comment line starting with #. Line 3 of the file defines all the function libraries to be maintained, line 3 defines the relevant source program files, and line 3 defines the target file; line 9-10 indicates that all source programs depend on datetime. h header file with corresponding maintenance code, that is, touch it and update the source file time; line 12-13 defines. O files depend on the corresponding. c file, and specify the maintenance code, that is, compile it with CC; line 16-17 defines the shared library my. so dependent target file. Maintain the-shared compilation option in the code to generate dynamic link library my. so.
1.5 run the make-F makefile-lib command
After make runs, the dynamic link library my. So is generated and can be called in the program. If you want to allow all users to use the database, log on to the system as the root user and copy the database to the/lib directory (command: CP my. so/LIB), or create a symbolic connection under the/lib directory (command: ln-s 'pwd'/My. so/LIB ).
2. Use of Dynamic Link Libraries in Linux
2.1 important dlfcn. h header files
When using a dynamic link library in Linux, the source program must contain the dlfcn. h header file, which defines the prototype of the function that calls the dynamic link library. The following describes these functions in detail.
2.1.1 dlerror
Prototype: const char * dlerror (void );
If the function fails to be executed in the dynamic link library, dlerror returns an error message. If the returned value is null, the operation is successful.
2.1.2 dlopen
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.
Filename: if the name does not start with "/", it is not an absolute path name and will be searched in the following order.
(1) ld_library value in the user environment variable;
(2) Dynamic Link buffer file/etc/lD. So. Cache
(3) directory/lib,/usr/lib
Flag indicates when to resolve undefined symbols (called ). There are two values:
1) rtld_lazy: indicates that it is resolved when the function code of the dynamic link library is executed.
2) rtld_now: indicates that all undefined symbols are resolved before dlopen returns. Once not resolved, dlopen returns an error.
If a dlopen call fails, the return value is null. Otherwise, the return value is the operation handle.
2.1.3 dlsym: Obtain the function execution address
Prototype: 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. This address can contain parameters to execute corresponding functions.
For example, program code: void (* Add) (int x, int y);/* describes the dynamic function to be called add */
Add = dlsym ("XXX. So", "add");/* Open the XXX. So shared library and obtain the Add function address */
Add (89,369);/* call the Add function with two parameters 89 and 369 */
2.1.4 dlclose: Close the dynamic link library
Prototype: 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.
2.2 Use the dynamic link library function in the program
2.2.1 program example
The following program loads the dynamic link library my. So and uses getdate and gettime to get the current date and time and then output the data.
----------------------------------------------------------------------
1 /************************************/
2/* file name: Dy. C */
3/* Function Description: Dynamic Link Library application demo program */
4/* programming: Landscape software production center Yu Yiqi */
5/* Preparation Time */
6 /************************************/
7
8 # include "stdio. H"/* contains the standard input/output file */
9
10 # include "dlfcn. H"/* contains the dynamic link function interface file */
11 # define sofile "./My. So"/* specify the dynamic link library name */
12
13 # define shared/* define macro and confirm sharing to reference dynamic functions */
14 # include "datetime. H"/* include user interface files */
15
16 main ()
17 {
18 datetype D;
19 timetype T;
20 void * DP;