Archived

This topic is now archived and is closed to further replies.

Golthar

Runtime modular programming

Recommended Posts

Hi all, Can anybody advice me if its possible to make a program modular and have the components (modules) be added in runtime The language of preference is C/C++ Thanks for help

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
This sounds like an OS kind of question to me. On Win32 use the LoadLibrary/FreeLibrary and GetProcAddress API''s.

Share this post


Link to post
Share on other sites
Yes, check the man pages on dlopen() or LoadLibrary() depending on your OS.

Both basically return a library handle that you can then use with dlsymb() or GetProcAddress() to get a pointer on a named function in the library.

ex: (windows)

{
double (myfunction*)( char* c );

HINSTANCE hlib = LoadLibrary( "MyLib.dll" );
if ( !hlib ) exit( EXIT_FAILURE );

myfunction = GetProcAddress( hlib, "MyFunctinInMyLib" );
if ( !myfunction ) exit( EXIT_FAILURE );

double x = myfunction( "Hail Eris! All Hail Discordia!" );
}

Now YOU and YOU only are responsible for having the right type of function pointer (type checking... what''s that?) and ensuring the existence of the name (beware of C++ name mangling, extern "C" is your friend)

Share this post


Link to post
Share on other sites
Thanks for the help :-)

You said, check the manpages, does anybody have an aproach for Linux too?
Thanks for sharing the knowledge I apreciate it :-)

Kindest regards,

Barry

Share this post


Link to post
Share on other sites
Sure *creaking sound* as I open my notebook of things coders-should-be-required-to-know.

Everything you want to know is in "Program Library Howto" by David A. Wheeler at http://www.linuxdoc.org/HOWTO/Program-Library-HOWTO/

Share this post


Link to post
Share on other sites
Thanks!

You have been of great help to me
And yes, these are things programers should know, but not always have to deal with.
This is my first time dealing with it
Thanks for all your help, I apreciate it

Kindest regards,

Barry

Share this post


Link to post
Share on other sites
That''s why I put them in a notebook (a binder actually...) when I encountered them : ANSI codes, scancodes, the man page for make, ld, the standard filesystem, gcc-specific C extensions ...

If there is a single advice I can give you (from programmer to programmer) is to keep one too.

Share this post


Link to post
Share on other sites