Use of Dynamic Link Library in Linux-dlopen dlsym dlclose dlerror

Source: Internet
Author: User
Tags export class

Source: http://www.eifr.com/article.php? Id = 1766 & page = 2

In Linux, dlopen, dlsym, and dlclose are equivalent to loadlibrary, getprocaddress, and freelibrary on windows. They can dynamically load dynamic libraries at runtime and use the export functions. But the limitation is that, in this way, only global functions can be exported, rather than class methods. Therefore, a large number of global functions will be designed to encapsulate the functions implemented by C ++ classes in a dynamic library. For example (pseudocode)

class A{public:      int run(...){...};}

Export functions and usage may be:

// Export the function handle_type aopen (parameters ...); int Arun (handle_type handle, parameters ...); void aclose (handle_type handle); // It may be: handle_type ahandle =-1; ahandle = aopen (....); // obtain the handle, that is, instantiate an A Object Arun (ahandle,...) in aopen ,....); // The handle is also used as a parameter, that is, the instance aclose (ahandle) generated by aopen is found inside the handle; // The previously generated object is released through the handle // internal principle: the dynamic database maintains a table (the handle corresponds to Class A one-to-one). // each time aopen generates a new a object and a new ahandle, and puts it into the // internal table, when calling the method of this a object in the future, you must first find the object in the internal ing table through the // handle.

This is somewhat depressing. It would be nice to use classes directly like dynamic connections. For example, new A () can be used directly when a. So is obtained. However, this is not acceptable. dlopen can only import functions and is a C-style name type.

C ++ dlopen mini howtoIs to export the"Number of createa letters for creating classesAndRelease class function releaseaTo use classes in the dynamic library, which is the same as the factory pattern in the pattern design.

The specific method is:
1. Prepare a header file, add a pure virtual parent class abase, and declare createa, releasea;
2. In the source code for generating a. So, write the business class ahello, inherit abase, and export the two functions createa and releasea mentioned above;
3. a. so code, use dlopen to open the so file, dlsym import createa, releasea, call createa to return the abase pointer type is actually ahello type instance, then you can use this ahello class instance;
4. Call raleasea to release the ahello instance.

Note:
1. The base class is required. Otherwise, how does the compiler know the implementation of ahello during compilation?
2. to import a function, add the extern "C" to prevent the export name from being modified.

In this way, you can use the dynamic library *. So of an export class to dynamically import dlopen as long as you get its parent class declaration. You can implement the following calls.

int main(){    void *dl  = load_so("./a.so");    test_so(dl);    dl  = load_so("./b.so");    test_so(dl);    dl  = load_so("./c.so");    test_so(dl);    return 0;}

Sample Code: exportclass

Refer:
1. http://www.faqs.org/docs/Linux-mini/C++-dlopen.html
2. http://www.codeproject.com/KB/cpp/howto_export_cpp_classes.aspx (not carefully read, may not be related)

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.