mr4th-dynamic-linking/xlist/main.c

39 lines
969 B
C

#include "base.h"
#include "base.c"
// setup plugin_func as a function pointer so that it can be linked at run time
typedef void PLUGIN_Func(void);
PLUGIN_Func *plugin_func = 0;
int main(){
// base layer initialization is automatic and runs before main - base functions begin working right away
base_func();
// to call a function with run-time linking, we must manually load and link it
#if OS_WINDOWS
HMODULE module = LoadLibraryA("plugin.dll");
if (module != 0){
plugin_func = (PLUGIN_Func*)GetProcAddress(module, "plugin_func");
}
#elif OS_LINUX
void *module = dlopen("./plugin.so", RTLD_NOW);
if (module != 0){
plugin_func = (PLUGIN_Func*)dlsym(module, "plugin_func");
}
#endif
// calls to plugin_func only work after the "plugin" loaded successfully
if (plugin_func != 0){
plugin_func();
}
// demonstration: the data structures contained in the 'base' are not duplicated in each binary
base_func();
}