0

I am trying to implement somewhat of a plugin system in a C program. The plugins are to be compiled as shared libraries and linked during compilation.

Let's say I have a singly linked list struct definition:

struct plugin_iface
{
    int data,
    struct plugin_iface* next
};

Each plugin creates a global instance of this structure and all those instances have the same name:

struct plugin_iface IfaceList = 
{
    .data = 42,
    .next = &IfaceList
} // Defined in the global scope in each plugin

As I would expect, next points to its very parent - inside this plugin. It is worth mentioning that such a scheme builds and links without an error. If I do

extern struct plugin_iface IfaceList;

in main.c, it addresses the instance in the plugin which was built and linked the latest.

Now what I would like to achieve is to have all those variable instances to form a linked list. Each node of that list would represent an interface to one plugin. Is that even possible in C? I want to avoid any configure-time header file generation.

ashpool
  • 179
  • 1
  • 8
  • linked during compilation? or run time? – aneesh jose Oct 03 '16 at 08:41
  • 2
    I would imitate a registration scheme similar to that Linux has with its device drivers. A plugin creates its own `struct plugin_iface` and passes it to some entity, which manages those, possibly using a linked list. That way, the plugin isn't bothered with the implementation details of the linked list and can just do `register_plugin(&plugin_iface)`. – cadaniluk Oct 03 '16 at 08:44
  • @Downvoter The problem with that approach is that the code in the plugin must be executed in order for it to be able to call the register function. And we cannot simply call a function from each plugin, since we do not know their names while we are in `main`. – ashpool Oct 03 '16 at 08:49
  • @aneeshjose linked during compilation. I shoud've mentioned that i also want to avoid using `dlopen()` linking. – ashpool Oct 03 '16 at 08:50
  • @aneeshjose that's not really a plugin then... – Ben Wainwright Oct 03 '16 at 09:00
  • @BenWainwright yep I know. Shoud've probably called it a module. – ashpool Oct 03 '16 at 09:03
  • @aneeshjose I would suggest editing your question to make it clearer to those trying to answer – Ben Wainwright Oct 03 '16 at 09:06
  • @BenWainwright you mean to edit my comment or actual question asked by Thurisaz? – aneesh jose Oct 03 '16 at 09:22
  • This can not be done with standard C. This can be done with compiler extensions. With GCC (or clang) you can use the constructor attribute on functions to make them call module registration. If you were doing static linking instead of dynamic, see this question and answer: http://stackoverflow.com/questions/11840651/how-can-i-implement-a-dynamic-dispatch-table-in-c/11844418#11844418 – Art Oct 03 '16 at 09:30

1 Answers1

1

I would suggest not solving this issue using the linker and C language 'implicitly'.

Instead, dedicate a plugins folder in your distribution, and detect, load and initialize any shared libraries that are placed there at runtime.

orlp
  • 112,504
  • 36
  • 218
  • 315