I need to write a plugin system which works with statically linked modules on Linux. I do not want the core (main function) to explicitly call the init function for the module.
The closest analogy which I can think of, for what I want to accomplish, is the Linux kernel. There it is possible to have a unknown number of modules/plugins compiled and linked statically, but the modules are initiated as would be if they were loaded dynamically.
I have this
core/main.c:
int main(void) { return 0; }
core/pluginregistrar.h:
#ifndef PLUGIN_REGISTRAR_H
#define PLUGIN_REGISTRAR_H
#include <stdio.h>
#define module_init(pInitor) \
static inline funInitor __inittest(void) \
{ fprintf(stdout, "test\n"); return pInitor; }
int init_module(void) __attribute__((alias(#pInitor)));
typedef void (*funInitor)(void);
#endif
adap1/main.c:
#include "pluginregistrar.h"
void adap1Init(void) { fprintf(stdout, "test1\n"); }
module_init(adap1Init);
adap2/main.c:
#include "pluginregistrar.h"
void adap2Init(void) { fprintf(stdout, "test2\n"); }
module_init(adap2Init);
so far, but I have no idea on how to get the core to actually initiate the modules which have done a module_init.
Can anyone here give me a pointer? (no pun intended)
EDIT: I changed core/main.c to
extern int init_module(void);
int main(void) {
init_module();
return 0;
}
and it not shows the call of the "adaptation" which was first in the library list given to the linker.