Okay, I'm currently on a Linux system but I want something that's very intercompatible. So, my program needs to load plugins for expanded features. The only problem is, I want the user to customize which are loaded in which order and which ones to use/discard. As time goes on, people would make their own and whatnot. According to my knowledge of .SO files (.DLL in windows), each plug would need a unique function name and the program would need to be recompiled if a new plugin gets made. What do I do? What am I missing?
You need a minimal interface that all plugins must implement. For example, extern"C" function_pointer get_function(constchar *);
The application calls this function in the plugin to obtain a function pointer to a function that implements a specific feature. For example,
1 2 3 4 5 6 7 8 9
auto make_snafucated = (make_snafucated_f)get_function("make_snafucated");
make_snafucated();
auto get_all_filters = (get_all_filters_f)get_function("get_all_filters");
auto all_filters = get_all_filters();
for (size_t i = 0; i < all_filters.size; i++){
auto filter = (filter_t)get_function(all_filters.function_names[i]);
if (some_condition(all_filters.properties[i]))
some_data = filter(some_data);
}
No, but each system has its own set of calls to a) dynamically load a library as a module, and b) get a function pointer into the module from a string. A condition for B on Windows is that the function must be exported by the DLL in order to be callable from other modules. On UNIX, non-static functions are exported by default.
For example, on Windows it would be
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
//(Error checking omitted for brevity.)
#include <Windows.h>
struct filter_list{
size_t size;
constchar **function_names;
filter_property *properties;
};
extern"C"typedefvoid (*make_snafucated_f)();
extern"C"typedef filter_list (*get_all_filters_f)();
#define GET_FUNCTION(x) (x##_f)GetProcAddress(module, #f)
auto module = LoadLibrary("PluginFoo.dll");
auto make_snafucated = (make_snafucated_f)GetProcAddress(module, "make_snafucated");
auto get_all_filters = (get_all_filters_f)GetProcAddress(module, "get_all_filters");
FreeLibrary(module);