You are not logged in.

#1 2008-11-29 03:20:59

Envil
Member
Registered: 2008-11-18
Posts: 52

(c++) adding plugin functions

Id like to make my program use plugins for certain functions, ideally it would load a .so file and then check if a certain function exists in it,
if yes it uses the function in the plugin otherwise it uses its own function.
Also id need to be able to access all (or atleast some specific ones) functions/global variables of the main program from within the function in the .so file.

Is this doable somehow? if yes how? ^^
Links to some guides etc would be neat as i havent found much usefull stuff about it yet.

Offline

#2 2008-11-29 08:03:24

string
Member
Registered: 2008-11-03
Posts: 286

Re: (c++) adding plugin functions

How portable do you want to be? Check out `man dlopen`.

Offline

#3 2008-11-29 14:58:56

Envil
Member
Registered: 2008-11-18
Posts: 52

Re: (c++) adding plugin functions

It only needs to run on a standart linux system , so portability isnt really an issue.
Gonna poke around with dlopen and see how it works.

Offline

#4 2008-11-29 16:37:45

Envil
Member
Registered: 2008-11-18
Posts: 52

Re: (c++) adding plugin functions

Seems to be exactly what i needed, thanks ^^

Just one more thing, is there any other way to access functions/global vars from the main program other than passing pointers for them to the library files?

Offline

#5 2008-11-29 18:48:55

Cerebral
Forum Fellow
From: Waterloo, ON, CA
Registered: 2005-04-08
Posts: 3,108
Website

Re: (c++) adding plugin functions

I'd strongly suggest passing anything the library needs as a param somehow.  The library really, _really_ should not depend on the main.

Offline

#6 2008-11-29 19:20:37

PJ
Member
From: Sweden
Registered: 2005-10-11
Posts: 602

Re: (c++) adding plugin functions

Since you are going to write it in C++ will you write it with classes? If you are going to do that, I have some code that uses dlopen and is written to handle classes. The code is used in a personal project which I am coding on, it will eventually be published. If you (or anyone else) is interested in the code for the module loading, I could upload it somewhere (I could have done that right now but it's on another computer that I own).

Offline

#7 2008-11-29 20:04:14

Envil
Member
Registered: 2008-11-18
Posts: 52

Re: (c++) adding plugin functions

Sure you can upload it and link it here for anyone else that might be intereseted ^^

I will probably have both classes and normal c functions, im still kinda n00b to c++ so my apps are quite a mess =P

Offline

#8 2008-11-29 21:06:58

PJ
Member
From: Sweden
Registered: 2005-10-11
Posts: 602

Re: (c++) adding plugin functions

Envil wrote:

Sure you can upload it and link it here for anyone else that might be intereseted ^^

Sure! The code lacks documentation at this point but I hope that I can answer any questions. Anyway, there is an example included with the code.
twpamodule.tar.gz

Offline

#9 2008-11-29 22:25:19

PJ
Member
From: Sweden
Registered: 2005-10-11
Posts: 602

Re: (c++) adding plugin functions

Interesting howto to read: http://www.faqs.org/docs/Linux-mini/C++-dlopen.html
My code is inspired of the examples in the howto but I have tried to make mine a bit more reusable.

Offline

#10 2008-11-30 00:34:23

Envil
Member
Registered: 2008-11-18
Posts: 52

Re: (c++) adding plugin functions

Ty for the link ^^

Offline

Board footer

Powered by FluxBB