Write DLL functions for Linux applications

Source: Internet
Author: User
Article title: compile DLL functions for Linux applications. 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.

Why should I write the entire application when I only write plug-ins?

Plug-ins and DLL are usually used to add functions without writing the entire new application. InLinux. Allen Wilson, E-commerce consultant and designer, introduced the dynamic library and showed you how to use the dynamic library to change the application when an application is running.

Internet browser users are very familiar with plug-ins. Download ins from the Web. these ins provide enhanced support for browser audio, video, and special effects. Generally, the plug-in provides new functions for existing applications without changing the original application.

DLL is a program function that is known to the program when designing and building an application. Use the program framework or baseboard when designing the main program of the application. These program frameworks or baseboards selectively mount the required dll at runtime, these dll are located in some files separated from the main program on the disk. This package and dynamic loading provides flexible upgrades, maintenance, and licensing policies.

Thousands of commands and applications are delivered along with Linux, which require at least libc library functions. If the libc function is packaged with each application, thousands of copies of the same function will appear on the disk. Linux builds these applications to use a single system-level copy of the commonly needed system library without wasting disk space. Linux is even better. each process that requires a public system library function uses a single system-level copy to load the copy to the memory at a time and share it with each process.

In Linux, plug-ins and dll are implemented in the form of dynamic libraries. The rest of this article is an example of using the dynamic library to change the application after the application is running.

Linux dynamic link

In Linux, applications are linked to external functions in either of the following two ways *. a) static links, and the library code is included in the executable file of the application; or the library and the shared library (lib *. so) dynamic link. The dynamic link loader maps the dynamic library to the executable memory of the application. Before starting the application, the dynamic link loader maps the required shared target Library to the application memory, or uses the system shared destination to parse the required external references for the application. Now the application can run.

As an example, the following is a small program that demonstrates the default use of dynamic link libraries in Linux:

       
            main()  {   printf(”Hello world  ”);  }                   
       
 

When you use gcc to compile hello. c, an executable file named a. out is created. By running the Linux command ldd a. out (this command prints the dependency between shared libraries), we can see that the required shared libraries are:

 

       
            libc.so.6 => /lib/libc.so.6 (0x4001d000)                     /lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0x40000000)
       
  

Use the same dynamic link loader to map the dll to the application memory after the application program runs. By using the Linux dynamic loader routine, the application controls the dynamic library to be loaded and the function in the library to execute the loading and link and return the address of the required entry point.

Linux dll functions

Linux provides four library functions (dlopen, dlerror, dlsym and dlclose) and an include File (dlfcn. h) and two shared libraries (static library libdl. a and dynamic library libdl. so) to support dynamic link loaders. These library functions are:

◆ Dlopen opens the shared target file, maps it to the memory, and returns a handle.

◆ Dlsym returns a pointer to the requested entry point.

◆ Dlerror returns NULL or a pointer to the ASCII string that describes the recent error.

◆ Dlclose close the handle and cancel the ING of the shared target file.

The dynamic link loader routine dlopen needs to find the shared target file in the file system to open the file and create a handle. There are four ways to specify the file location:

◆ Absolute file path in dlopen call.

◆ In the directory specified in the LD_LIBRARY_PATH environment variable.

◆ In the library list specified in/etc/ld. so. cache.

◆ First in/usr/lib and then in/lib.

DLL Example: small C program and dlTest

The dynamic link loader sample program is a small C program designed to practice dl routines. This program is based on a C program compiled by everyone. it prints "Hello World" to the console. The first printed message is "HeLlO WoRlD ". The test program links to the two functions that print the message again: the first time, all uppercase characters, and the second time, all lowercase characters.

The following is a summary of the program:

Define the dll include file dlfcn. h and the required variables. At least these variables are required:

Handle to the shared library file;

Pointer to the entry point of the ing function;

Pointer to the error string.

Print the initial message "HeLlO WoRlD ".

Use the absolute path "/home/dlTest/UPPERCASE. so" and the RTLD_LAZY option. dlopen opens the shared target file of UPPERCASE dll and returns a handle.

Option RTLD_LAZY delays parsing the dll's external reference until the dll is executed.

Option RTLD_NOW resolves all external references before dlopen returns.

Dlsym returns the address of printUPPERCASE at the entry point.

Call printUPPERCASE and print the modified message "hello world ".

Dlclose closes the handle to UPPERCASE. so and cancels the dll ing from the memory.

[1] [2] Next page

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.