Linux shared library programming

Source: Internet
Author: User
1. it is similar to the dynamic link library in Windows system. a shared library is also available in Linux to support code reuse. * Dll in Windows and * so in Linux. The following details

I. description

Similar to the dynamic link library in Windows, Linux also has a shared library to support code reuse. *. Dll in Windows and *. so in Linux. The following describes how to create and use a Linux shared library.

II. create a shared library

In the mytestso. c file, the code is as follows:

  1. # Include
  2. # Include
  3. Int GetMax (int a, int B)
  4. {
  5. If (a> = B)
  6. Return;
  7. Return B;
  8. }
  9. Int GetInt (char * psztxt)
  10. {
  11. If (0 = psztxt)
  12. Return-1;
  13.  
  14. Return atoi (psztxt );
  15. }

Run the following command to compile:

Gcc-fpic-shared mytestso. c-o mytestso. so

-Fpic: the output object module is generated in the relocated address mode.

After compilation is successful, mytestso. so is in the current directory, and the shared library mytestso. so has been created successfully.

III. use shared libraries

Functions in the shared library can be loaded and executed by the main program, but do not need to be linked to the target file of the main program during compilation. When using functions in the shared library, the main program needs to know the name (string) of the function in advance, and then obtain the start address (function pointer) of the function according to its name ), then you can use the function pointer to use the function.

In the mytest. c file, the code is as follows:

  1. # Include
  2. # Include
  3. Int main (int argc, char * argv [])
  4. {
  5. Void * pdlhandle;
  6. Char * pszerror;
  7.  
  8. Int (* GetMax) (int a, int B );
  9. Int (* GetInt) (char * psztxt );
  10.  
  11. Int a, B;
  12. Char * psztxt = "1024 ";
  13.  
  14. // Open mytestso. so
  15. Pdlhandle = dlopen ("./mytestso. so", RTLD_LAZY );
  16. Pszerror = dlerror ();
  17. If (0! = Pszerror ){
  18. Printf ("% sn", pszerror );
  19. Exit (1 );
  20. }
  21.  
  22. // Get GetMax func
  23. GetMax = dlsym (pdlhandle, "GetMax ");
  24. Pszerror = dlerror ();
  25. If (0! = Pszerror ){
  26. Printf ("% sn", pszerror );
  27. Exit (1 );
  28. }
  29.  
  30. // Get GetInt func
  31. GetInt = dlsym (pdlhandle, "GetInt ");
  32. Pszerror = dlerror ();
  33. If (0! = Pszerror ){
  34. Printf ("% sn", pszerror );
  35. Exit (1 );
  36. }
  37.  
  38. // Call fun
  39. A = 200;
  40. B = 600;
  41. Printf ("max = % dn", GetMax (a, B ));
  42. Printf ("txt = % dn", GetInt (psztxt ));
  43.  
  44. // Close mytestso. so
  45. Dlclose (pdlhandle );
  46. }

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.