Create and use a Linux dynamic link library. so file

Source: Internet
Author: User
Article Title: create and use a Linux dynamic link library. so file. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.

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 a large number of dynamic link libraries in LINUX. In the/lib directory, there are many. so is a file with a suffix. This is the dynamic link library of LINUX system applications. It is called so, Shared Object, and Shared Object, but different from WINDOWS. (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 well-known 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] [3] [4] Next page

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.