ashpool
ashpool

Reputation: 253

Global linked variables using one name

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.

Upvotes: 0

Views: 61

Answers (1)

orlp
orlp

Reputation: 117926

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.

Upvotes: 1

Related Questions