How to create plugins in plain C?

6 views
Skip to first unread message

AJ ONeal

unread,
Feb 1, 2011, 4:36:45 PM2/1/11
to uta...@googlegroups.com
How does one go about creating C plugins?


Example Scenario:

I have an application which generates rgb16 files.
I have a number libraries with a particular interface that output in various formats

This is the interface:
int encode(void* buffer, int width, int height, int depth)

There is a settings.conf which is parsed at startup or SIGHUP that looks like this:

jpeg: rgb16tojpeg.so
png: rgb16topng.so
// bmp: rgb16tobmp.so

By default none of the modules are loaded.
In this case the modules jpeg and png are going to be loaded (and used), but bmp will not.

In this fashion I only load the module into memory if I plan to use it. Otherwise I do not.

More importantly, if I come up with a new format I should be able to put the .so into the plugins directory and update the settings file and voila, the output should suddenly also be encoded in the new format.


Any ideas?

AJ ONeal

Shawn

unread,
Feb 1, 2011, 5:04:22 PM2/1/11
to Utah C Users Group
Check out dlopen().

AJ ONeal

unread,
Feb 1, 2011, 5:19:03 PM2/1/11
to uta...@googlegroups.com
Thanks Shawn,

I was googling the wrong terms previously. What I should have been searching for was "dynamic loading" rather than "plugins".


I found a few good articles with examples using dlopen:
"Dynamic loading and un-loading of shared libraries using libdl" about halfway down this page:

AJ ONeal

unread,
Feb 1, 2011, 6:37:35 PM2/1/11
to uta...@googlegroups.com
I don't quite understand the difference between RTLD_LOCAL and RTLD_GLOBAL.
More importantly, I don't understand the 'recolation' lingo.

Can someone shed some light on the subject?


Here's an example where I don't understand what the difference would be:

Assume the libs
  rgb16tojpeg.so
  rgb16png.so

And each has a function
  int encode(void* buffer, int width, int height, int depth);

pngh = dlopen("rgb16topng.so", RTLD_LAZY | RTLD_GLOBAL);
jpegh = dlopen("rgb16tojpeg.so", RTLD_LAZY | RTLD_LOCAL);

jpegfn = dlsym(jpegh, "encode");
pngfn = dlsym(pngh, "encode");

*jpegfn(data, 320, 240, 16);
*pngfn(data, 320, 240, 16);



However if I were to do
encfn = dlsym(RTLD_DEFAULT, "encode");

then encfn would be the RTLD_GLOBAL loaded "encode"?

AJ ONeal

On Tue, Feb 1, 2011 at 3:04 PM, Shawn <shawnw...@gmail.com> wrote:
Reply all
Reply to author
Forward
0 new messages