Explicit call of Linux dynamic library

Source: Internet
Author: User

After ten years of development, the Linux system has already caught up with Microsoft. As a result, many people start to learn Linux, and you may encounter explicit calls from dynamic libraries, here we will introduce the explicit call solution of the dynamic library. Here we will introduce it to you.

Explicit calling means that the library file name appears in the code, and you need to open and manage the library file on your own. The main points are:

(1) include the header file of the dlfcn. h System

(2) Use the dlopen function to open the library file and specify the open mode.

The first parameter of dllope is the name of the shared library, and the specified shared library will be searched in the following location.

① Environment variable LD_LIBRARY_PATH lists all directories separated by semicolons.

② The list of databases found in the file/etc/ld. so. cache is refreshed by the ldconfig command.

③ Directory usr/lib.

④ Directory/lib.

⑤ Current directory.

The second parameter is how to open the shared library. There are two values

① RTLD_NOW: load all functions in the shared library to the memory.

② RTLD_LAZY: pushes back the function loading operation in the shared library until a function is loaded when dlsym () is called.

(3) Use the dlerror () function to test whether the function is successfully opened and handle the error;

(4) use dlsym to obtain the function address and store it in a function pointer.

Then, use the obtained function pointer to call the function.

When the ghost program ends, use dlclose to close the opened dynamic library to prevent resource leakage.

⑺ Use the ldconfig tool to add the dynamic library path to the system library list.

1. Compile the test file

 
 
  1. // Main. c test the program explicitly called by the dynamic library
  2. # Include<Dlfcn. h>// System header file used for dynamic Library Management
  3. # Include "myalib. h" // include the function header file. Otherwise, an error is reported during compilation.
  4. Int main (int argc, char * argv [])
  5. {
  6. // Declare the function pointer of the corresponding function
  7. Void (* pTest )();
  8. // Load the dynamic library
  9. Void *PdlHandle=Dlopen("Libtest. so", RTLD_LAZY );
  10. // Handle errors
  11. If (PdlHandle= NULL ){
  12. Printf ("Failed load library \ n ");
  13. Return-1;
  14. }
  15. Char *PszErr=Dlerror();
  16. If (pszErr! = NULL)
  17. {
  18. Printf ("% s \ n", pszErr );
  19. Return-1;
  20. }
  21. // Obtain the function address
  22. PTest=Dlsym(PdlHandle, "test ");
  23. PszErr=Dlerror();
  24. If (pszErr! = NULL)
  25. {
  26. Printf ("% s \ n", pszErr );
  27. Dlclose (pdlHandle );
  28. Return-1;
  29. }
  30. // Implement function call
  31. (* PTest )();
  32. // Close the dynamic library when the program ends
  33. Dlclose (pdlHandle );
  34. Return 0;
  35. }

2. Compile the test file

Use the-ldl option to indicate that the generated object module must use the shared library.

Gcc-o main-ldl main. c

A main file is generated after execution.

3. Execute the test program

Run./main

Output

Test

It indicates the operation is successful. This is an introduction to explicit calling of Linux dynamic libraries.

  1. Configuration notes: configure the DNS server in Linux
  2. How to display Chinese Characters in RedHat Linux 5
  3. How to disable SELinux in Redhat Enterprise Linux
  4. Install the KDE package in Linux
  5. Redhat Linux Remote Desktop configuration

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.