Main BLOGGER
Google
WWW THIS BLOG
Thursday, March 03, 2005
 
Dynamic Class Loading for C++ in Linux

void *dlopen(const char *
file, int mode);

void *dlsym(void *handle, char *symbol);
const char *dlerror();
int dlclose(void *handle);
Given that we can use these functions to access functions in a C library, how do we use them to access classes in a C++ library?
1. We must be able to locate the symbols we need in the library.
2. How can we create objects belonging to the classes we load?
3. Finally, how can we access those objects in a useful manner?

The approach in http://www2.linuxjournal.com/article/3687
1. define a base class for all Dynamic Classes such that the dynamic objects can be statically casted (using static_cast()) to instances of base class.
2. In Main(), define a global variable Factory (std::map)
3. In dynamic class, define a proxy class variable, this proxy class variable will register the maker for the dynamic class to the Factory.
4. Using RTLD_NOW flag in dlopen() to instantialize the proxy variable when loading the dynamic class such that the register is automatic.
5. Using gcc/egcs, we would be required to link with the rdynamic option to force the main program to export its symbols to the libraries loaded with dlopen such that the global variable Facotry is accessible by proxy class.



<< Home

Powered by Blogger

Google
WWW THIS BLOG