mr4th-dynamic-linking/xlist/main.c

39 lines
969 B
C
Raw Permalink Normal View History

#include "base.h"
#include "base.c"
2024-04-23 19:50:20 +00:00
// 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
2024-04-23 19:50:20 +00:00
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);
2024-04-23 19:50:20 +00:00
if (module != 0){
plugin_func = (PLUGIN_Func*)dlsym(module, "plugin_func");
2024-04-23 19:50:20 +00:00
}
#endif
2024-04-23 19:50:20 +00:00
// 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();
}