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.

9
  • linked during compilation? or run time? Commented Oct 3, 2016 at 8: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). Commented Oct 3, 2016 at 8: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. Commented Oct 3, 2016 at 8:49
  • @aneeshjose linked during compilation. I shoud've mentioned that i also want to avoid using dlopen() linking. Commented Oct 3, 2016 at 8:50
  • @aneeshjose that's not really a plugin then... Commented Oct 3, 2016 at 9:00

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.