Unix Programming: Sharing Libraries with Libraries

Working in C, on top of unix, I download and use the shared library somewhat as follows:

...

handle = dlopen("nameOfLib");

...

libInit();

...

libGoToState1();


libGoToState2();


....

libTerminate();

...

dlclose(handle);

...

I would like my application to allow "plugins" that take the form of dynamically loaded libraries that adhere to this API.

The tricky part is that I want to load the plug-in after the call libInit(), and I want the plug-in to be able to call libGoToSomeOtherState()by changing the state of the library, but using the same “session” as the application that downloaded it.

Any thoughts on how I need to code are appreciated.

In particular, what needs to be done in the .c files for the plugin and the main program so that they can share the library instance, state and all?

+3
4

, , - , , ( libInit(), ), .

+1

A plugin can call dlsym(RTLD_DEFAULT, "libGoToSomeOtherState")to obtain the address of the function that it wants to call, or you can directly pass this pointer to the plugin initialization procedure (possibly in the table of such pointers).

+1
source

As a brief hint, I would suggest using callbacks: either pass the library handler to the plugin, or use the function pointers inside the plugin to call the original library functions.

0
source

Source: https://habr.com/ru/post/1729422/


All Articles