Not at all Arduino-related, hence the Bar Sport, just wondering if one of the resident C guys can fill in some blanks for me on a Linux app I'm working on.
The app in question uses a plugin framework, where there will be a directory of .so files that can be enumerated and loaded at runtime. I've gotten the dlopen() and dlsym() calls to work, no problem there. However, I'm a little unsure how the dynamically linked code fits in. I assume, unlike a shared library that is linked at compile time, there could be differences in namespace. For example:
- If your main executable dynamically loads pluginA.so and pluginB.so, is it reasonable for the entry point (init function) name to be identical across plugins? E.g....
main executable:
void *loadPlugin (char *fname) {
void *lib = dlopen(fname, RTLD_LAZY);
if (lib == NULL) return NULL;
void *sym = dlsym(lib, "plugin_init");
if (sym == NULL) {
dlclose(lib);
return NULL;
}
typedef void (*init_t)(void);
init_t init = (init_t)sym;
(*init)();
return lib;
}
Both pluginA.c and pluginB.c would have this:
void plugin_init() { ... }
Would those conflict, or since they are not compiled together, do their human-readable names mean nothing? (Assuming if that's the case, dlsym() can still resolve the different names since it has the handle to the shared object in question, and doesn't have to resolve the name to an address through the main executable's symbol table?)
Whew, OK... and question two:
- Can pluginA and pluginB call functions in the main application? Obviously, they would have to include a header file with the functions declared, but the actual definition would be in a module they aren't linked to. Does dynamic loading resolve this, or would I need to deliver exported functions (exported from main to plugin) through function pointers?
In other words...
mainapp.c
#include "mainapp.h"
void do_something() { ... }
mainapp.h
void do_something();
plugin
#include "mainapp.h";
void plugin_init() {
...
do_something();
}
I'm fighting with a segfault at the moment (unrelated -- a bad pointer I suspect), but it seems that one of my earlier tests failed because plugin.so had a fwd declaration for do_something(), but had no link to its function body. Could've been the result of other problems, just wondering if that's actually the case, and if so, what's the typical way of handling that sort of thing?